1
0
mirror of https://github.com/br0xen/termbox-util.git synced 2024-11-22 13:33:15 +00:00

Making menu better

This commit is contained in:
Brian Buller 2016-02-24 21:33:33 -06:00
parent ad7e3807a0
commit 9b7cb328b9

View File

@ -1,10 +1,6 @@
package termboxUtil package termboxUtil
import ( import "github.com/nsf/termbox-go"
"strconv"
"github.com/nsf/termbox-go"
)
// Menu is a menu with a list of options // Menu is a menu with a list of options
type Menu struct { type Menu struct {
@ -195,6 +191,40 @@ func (i *Menu) SelectNextOption() {
} }
} }
// SelectPageUpOption Goes up 'menu height' options
func (i *Menu) SelectPageUpOption() {
idx := i.GetSelectedIndex()
idx -= i.height
if idx < 0 {
idx = 0
}
i.SetSelectedIndex(idx)
return
}
// SelectPageDownOption Goes down 'menu height' options
func (i *Menu) SelectPageDownOption() {
idx := i.GetSelectedIndex()
idx += i.height
if idx >= len(i.options) {
idx = len(i.options) - 1
}
i.SetSelectedIndex(idx)
return
}
// SelectFirstOption Goes to the top
func (i *Menu) SelectFirstOption() {
i.SetSelectedIndex(0)
return
}
// SelectLastOption Goes to the bottom
func (i *Menu) SelectLastOption() {
i.SetSelectedIndex(len(i.options) - 1)
return
}
// SetOptionDisabled Disables the specified option // SetOptionDisabled Disables the specified option
func (i *Menu) SetOptionDisabled(idx int) { func (i *Menu) SetOptionDisabled(idx int) {
if len(i.options) > idx { if len(i.options) > idx {
@ -271,6 +301,10 @@ func (i *Menu) HandleEvent(event termbox.Event) bool {
i.SelectPrevOption() i.SelectPrevOption()
case termbox.KeyArrowDown: case termbox.KeyArrowDown:
i.SelectNextOption() i.SelectNextOption()
case termbox.KeyArrowLeft:
i.SelectPageUpOption()
case termbox.KeyArrowRight:
i.SelectPageDownOption()
} }
if i.vimMode { if i.vimMode {
switch event.Ch { switch event.Ch {
@ -279,6 +313,11 @@ func (i *Menu) HandleEvent(event termbox.Event) bool {
case 'k': case 'k':
i.SelectPrevOption() i.SelectPrevOption()
} }
if event.Key == termbox.KeyCtrlF {
i.SelectPageDownOption()
} else if event.Key == termbox.KeyCtrlB {
i.SelectPageUpOption()
}
} }
if i.GetSelectedIndex() != currentIdx { if i.GetSelectedIndex() != currentIdx {
return true return true
@ -329,25 +368,26 @@ func (i *Menu) Draw() {
} }
// Print the options // Print the options
selIdx := i.GetSelectedIndex() bldHeight := (optionHeight / 2)
printOptions := i.options startIdx := i.GetSelectedIndex()
startOpts := selIdx - (optionHeight / 2) endIdx := i.GetSelectedIndex()
endOpts := selIdx + (optionHeight / 2) for bldHeight > 0 && startIdx >= 1 {
for startOpts < 0 { startIdx--
startOpts++ bldHeight--
endOpts++ }
bldHeight += (optionHeight / 2)
for bldHeight > 0 && endIdx < len(i.options) {
endIdx++
bldHeight--
} }
printOptions = printOptions[startOpts:] for idx := startIdx; idx < endIdx; idx++ { //i.options {
for idx := range printOptions { //i.options {
if i.GetSelectedIndex()-idx >= optionHeight-1 { if i.GetSelectedIndex()-idx >= optionHeight-1 {
// Skip this one // Skip this one
continue continue
} }
currOpt := &printOptions[idx] currOpt := &i.options[idx]
//currOpt := &i.options[idx]
outTxt := currOpt.GetText() outTxt := currOpt.GetText()
outTxt = strconv.Itoa(startOpts) + "/" + strconv.Itoa(selIdx) + "/" + strconv.Itoa(endOpts) + " - " + outTxt
if len(outTxt) >= i.width { if len(outTxt) >= i.width {
outTxt = outTxt[:i.width] outTxt = outTxt[:i.width]
} }