mirror of
https://github.com/br0xen/termbox-util.git
synced 2024-11-22 13:33:15 +00:00
Several changes
Menu is working better inputfield/modal: maybe allow multiline...
This commit is contained in:
parent
9b7cb328b9
commit
d5803aecc4
@ -146,18 +146,17 @@ func (i *InputField) HandleEvent(event termbox.Event) bool {
|
|||||||
ch = " "
|
ch = " "
|
||||||
case termbox.KeyTab:
|
case termbox.KeyTab:
|
||||||
ch = "\t"
|
ch = "\t"
|
||||||
/* Multiline is disabled right now
|
case termbox.KeyEnter:
|
||||||
case termbox.KeyEnter:
|
if i.multiline {
|
||||||
if i.multiline {
|
ch = "\n"
|
||||||
ch = "\n"
|
}
|
||||||
}
|
|
||||||
*/
|
|
||||||
default:
|
default:
|
||||||
if KeyIsAlphaNumeric(event) || KeyIsSymbol(event) {
|
if KeyIsAlphaNumeric(event) || KeyIsSymbol(event) {
|
||||||
ch = string(event.Ch)
|
ch = string(event.Ch)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: Handle newlines
|
||||||
if i.cursor+len(i.value) == 0 {
|
if i.cursor+len(i.value) == 0 {
|
||||||
i.value = string(ch) + i.value
|
i.value = string(ch) + i.value
|
||||||
} else if i.cursor == 0 {
|
} else if i.cursor == 0 {
|
||||||
|
@ -15,9 +15,11 @@ type InputModal struct {
|
|||||||
cursor int
|
cursor int
|
||||||
bg, fg termbox.Attribute
|
bg, fg termbox.Attribute
|
||||||
isDone bool
|
isDone bool
|
||||||
|
isAccepted bool
|
||||||
isVisible bool
|
isVisible bool
|
||||||
bordered bool
|
bordered bool
|
||||||
tabSkip bool
|
tabSkip bool
|
||||||
|
inputSelected bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// CreateInputModal Create an input modal with the given attributes
|
// 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.showHelp = true
|
||||||
i.input.bordered = true
|
i.input.bordered = true
|
||||||
i.isVisible = true
|
i.isVisible = true
|
||||||
|
i.inputSelected = true
|
||||||
return &i
|
return &i
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -86,6 +89,16 @@ func (i *InputModal) SetHeight(height int) {
|
|||||||
i.height = height
|
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
|
// IsBordered returns whether this control is bordered or not
|
||||||
func (i *InputModal) IsBordered() bool {
|
func (i *InputModal) IsBordered() bool {
|
||||||
return i.bordered
|
return i.bordered
|
||||||
@ -140,6 +153,11 @@ func (i *InputModal) Hide() {
|
|||||||
i.isVisible = false
|
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
|
// SetDone Sets the flag that tells whether this modal has completed it's purpose
|
||||||
func (i *InputModal) SetDone(b bool) {
|
func (i *InputModal) SetDone(b bool) {
|
||||||
i.isDone = b
|
i.isDone = b
|
||||||
@ -150,6 +168,11 @@ func (i *InputModal) IsDone() bool {
|
|||||||
return i.isDone
|
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
|
// GetValue Return the current value of the input
|
||||||
func (i *InputModal) GetValue() string { return i.input.GetValue() }
|
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
|
// HandleEvent Handle the termbox event, return true if it was consumed
|
||||||
func (i *InputModal) HandleEvent(event termbox.Event) bool {
|
func (i *InputModal) HandleEvent(event termbox.Event) bool {
|
||||||
if event.Key == termbox.KeyEnter {
|
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
|
// Done editing
|
||||||
i.isDone = true
|
i.isDone = true
|
||||||
|
i.isAccepted = false
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
return i.input.HandleEvent(event)
|
return i.input.HandleEvent(event)
|
||||||
|
@ -146,12 +146,14 @@ func (i *Menu) GetSelectedIndex() int {
|
|||||||
|
|
||||||
// SetSelectedIndex sets the selection to setIdx
|
// SetSelectedIndex sets the selection to setIdx
|
||||||
func (i *Menu) SetSelectedIndex(idx int) {
|
func (i *Menu) SetSelectedIndex(idx int) {
|
||||||
if idx < 0 {
|
if len(i.options) > 0 {
|
||||||
idx = 0
|
if idx < 0 {
|
||||||
} else if idx >= len(i.options) {
|
idx = 0
|
||||||
idx = len(i.options) - 1
|
} else if idx >= len(i.options) {
|
||||||
|
idx = len(i.options) - 1
|
||||||
|
}
|
||||||
|
i.SetSelectedOption(&i.options[idx])
|
||||||
}
|
}
|
||||||
i.SetSelectedOption(&i.options[idx])
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetSelectedOption sets the current selected option to v (if it's valid)
|
// SetSelectedOption sets the current selected option to v (if it's valid)
|
||||||
|
Loading…
Reference in New Issue
Block a user