From d5803aecc499185e9ccb225bba6f9c36508cc5f0 Mon Sep 17 00:00:00 2001 From: Brian Buller Date: Thu, 25 Feb 2016 10:31:28 -0600 Subject: [PATCH] Several changes Menu is working better inputfield/modal: maybe allow multiline... --- termbox_inputfield.go | 11 +++++------ termbox_inputmodal.go | 37 +++++++++++++++++++++++++++++++++++++ termbox_menu.go | 12 +++++++----- 3 files changed, 49 insertions(+), 11 deletions(-) diff --git a/termbox_inputfield.go b/termbox_inputfield.go index 6f10961..089ff51 100644 --- a/termbox_inputfield.go +++ b/termbox_inputfield.go @@ -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" - } - */ + 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 { diff --git a/termbox_inputmodal.go b/termbox_inputmodal.go index 50678bc..4268314 100644 --- a/termbox_inputmodal.go +++ b/termbox_inputmodal.go @@ -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) diff --git a/termbox_menu.go b/termbox_menu.go index c38ee9f..700ab04 100644 --- a/termbox_menu.go +++ b/termbox_menu.go @@ -146,12 +146,14 @@ func (i *Menu) GetSelectedIndex() int { // SetSelectedIndex sets the selection to setIdx func (i *Menu) SetSelectedIndex(idx int) { - if idx < 0 { - idx = 0 - } else if idx >= len(i.options) { - idx = len(i.options) - 1 + if len(i.options) > 0 { + if idx < 0 { + idx = 0 + } 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)