1
0
mirror of https://github.com/br0xen/termbox-util.git synced 2024-11-25 23:03:13 +00:00

Several changes

Menu is working better
inputfield/modal: maybe allow multiline...
This commit is contained in:
Brian Buller 2016-02-25 10:31:28 -06:00
parent 9b7cb328b9
commit d5803aecc4
3 changed files with 49 additions and 11 deletions

View File

@ -146,18 +146,17 @@ func (i *InputField) HandleEvent(event termbox.Event) bool {
ch = " "
case termbox.KeyTab:
ch = "\t"
/* Multiline is disabled right now
case termbox.KeyEnter:
if i.multiline {
ch = "\n"
}
*/
default:
if KeyIsAlphaNumeric(event) || KeyIsSymbol(event) {
ch = string(event.Ch)
}
}
// TODO: Handle newlines
if i.cursor+len(i.value) == 0 {
i.value = string(ch) + i.value
} else if i.cursor == 0 {

View File

@ -15,9 +15,11 @@ type InputModal struct {
cursor int
bg, fg termbox.Attribute
isDone bool
isAccepted bool
isVisible bool
bordered bool
tabSkip bool
inputSelected bool
}
// CreateInputModal Create an input modal with the given attributes
@ -27,6 +29,7 @@ func CreateInputModal(title string, x, y, width, height int, fg, bg termbox.Attr
i.showHelp = true
i.input.bordered = true
i.isVisible = true
i.inputSelected = true
return &i
}
@ -86,6 +89,16 @@ func (i *InputModal) SetHeight(height int) {
i.height = height
}
// SetMultiline returns whether this is a multiline modal
func (i *InputModal) SetMultiline(m bool) {
i.input.multiline = m
}
// IsMultiline returns whether this is a multiline modal
func (i *InputModal) IsMultiline() bool {
return i.input.multiline
}
// IsBordered returns whether this control is bordered or not
func (i *InputModal) IsBordered() bool {
return i.bordered
@ -140,6 +153,11 @@ func (i *InputModal) Hide() {
i.isVisible = false
}
// IsVisible returns the isVisible flag
func (i *InputModal) IsVisible() bool {
return i.isVisible
}
// SetDone Sets the flag that tells whether this modal has completed it's purpose
func (i *InputModal) SetDone(b bool) {
i.isDone = b
@ -150,6 +168,11 @@ func (i *InputModal) IsDone() bool {
return i.isDone
}
// IsAccepted Returns whether the modal has been accepted
func (i *InputModal) IsAccepted() bool {
return i.isAccepted
}
// GetValue Return the current value of the input
func (i *InputModal) GetValue() string { return i.input.GetValue() }
@ -175,8 +198,22 @@ func (i *InputModal) Clear() {
// HandleEvent Handle the termbox event, return true if it was consumed
func (i *InputModal) HandleEvent(event termbox.Event) bool {
if event.Key == termbox.KeyEnter {
if !i.input.IsMultiline() || !i.inputSelected {
// Done editing
i.isDone = true
i.isAccepted = true
} else {
i.input.HandleEvent(event)
}
return true
} else if event.Key == termbox.KeyTab {
if i.input.IsMultiline() {
i.inputSelected = !i.inputSelected
}
} else if event.Key == termbox.KeyEsc {
// Done editing
i.isDone = true
i.isAccepted = false
return true
}
return i.input.HandleEvent(event)

View File

@ -146,6 +146,7 @@ func (i *Menu) GetSelectedIndex() int {
// SetSelectedIndex sets the selection to setIdx
func (i *Menu) SetSelectedIndex(idx int) {
if len(i.options) > 0 {
if idx < 0 {
idx = 0
} else if idx >= len(i.options) {
@ -153,6 +154,7 @@ func (i *Menu) SetSelectedIndex(idx int) {
}
i.SetSelectedOption(&i.options[idx])
}
}
// SetSelectedOption sets the current selected option to v (if it's valid)
func (i *Menu) SetSelectedOption(v *MenuOption) {