From cf3fa0559d2ae4d6d77d6982146b1cfb2ae161d9 Mon Sep 17 00:00:00 2001 From: Brian Buller Date: Mon, 4 May 2015 13:52:10 -0500 Subject: [PATCH 1/2] Add Confirm Modal Also update inputfield/inputmodal --- termbox_confirmmodal.go | 131 ++++++++++++++++++++++++++++++++++++++++ termbox_inputfield.go | 3 + termbox_inputmodal.go | 13 ++-- 3 files changed, 143 insertions(+), 4 deletions(-) create mode 100644 termbox_confirmmodal.go diff --git a/termbox_confirmmodal.go b/termbox_confirmmodal.go new file mode 100644 index 0000000..632663d --- /dev/null +++ b/termbox_confirmmodal.go @@ -0,0 +1,131 @@ +package termbox_util + +import ( + "github.com/nsf/termbox-go" +) + +type ConfirmModal struct { + title string + text string + x, y, width, height int + show_help bool + cursor int + bg, fg termbox.Attribute + is_done bool + accepted bool + value string +} + +func CreateConfirmModal(title string, x, y, width, height int, fg, bg termbox.Attribute) *ConfirmModal { + i := ConfirmModal{title: title, x: x, y: y, width: width, height: height, fg: fg, bg: bg} + if i.title == "" && i.text == "" { + i.title = "Confirm?" + } + i.show_help = true + return &i +} + +func (i *ConfirmModal) GetTitle() string { return i.title } +func (i *ConfirmModal) SetTitle(s string) *ConfirmModal { + i.title = s + return i +} + +func (i *ConfirmModal) GetText() string { return i.text } +func (i *ConfirmModal) SetText(s string) *ConfirmModal { + i.text = s + return i +} + +func (i *ConfirmModal) GetX() int { return i.x } +func (i *ConfirmModal) SetX(x int) *ConfirmModal { + i.x = x + return i +} +func (i *ConfirmModal) GetY() int { return i.y } +func (i *ConfirmModal) SetY(y int) *ConfirmModal { + i.y = y + return i +} + +func (i *ConfirmModal) GetWidth() int { return i.width } +func (i *ConfirmModal) SetWidth(width int) *ConfirmModal { + i.width = width + return i +} + +func (i *ConfirmModal) GetHeight() int { return i.height } +func (i *ConfirmModal) SetHeight(height int) *ConfirmModal { + i.height = height + return i +} + +func (i *ConfirmModal) HelpIsShown() bool { return i.show_help } +func (i *ConfirmModal) ShowHelp(b bool) *ConfirmModal { + i.show_help = b + return i +} + +func (i *ConfirmModal) GetBackground() termbox.Attribute { return i.bg } +func (i *ConfirmModal) SetBackground(bg termbox.Attribute) *ConfirmModal { + i.bg = bg + return i +} + +func (i *ConfirmModal) GetForeground() termbox.Attribute { return i.fg } +func (i *ConfirmModal) SetForeground(fg termbox.Attribute) *ConfirmModal { + i.fg = fg + return i +} + +func (i *ConfirmModal) IsDone() bool { return i.is_done } +func (i *ConfirmModal) SetDone(b bool) *ConfirmModal { + i.is_done = b + return i +} + +func (i *ConfirmModal) IsAccepted() bool { return i.accepted } + +func (i *ConfirmModal) Clear() *ConfirmModal { + i.title = "" + i.text = "" + i.accepted = false + i.is_done = false + return i +} + +func (i *ConfirmModal) HandleKeyPress(event termbox.Event) bool { + if event.Ch == 'Y' || event.Ch == 'y' { + i.accepted = true + i.is_done = true + return true + } else if event.Ch == 'N' || event.Ch == 'n' { + i.accepted = false + i.is_done = true + return true + } + return false +} +func (i *ConfirmModal) Draw() { + // First blank out the area we'll be putting the modal + FillWithChar(' ', i.x, i.y, i.x+i.width, i.y+i.height, i.fg, i.bg) + // Now draw the border + DrawBorder(i.x, i.y, i.x+i.width, i.y+i.height, i.fg, i.bg) + + next_y := i.y + 1 + // The title + if i.title != "" { + DrawStringAtPoint(i.title, i.x+1, next_y, i.fg, i.bg) + next_y += 1 + FillWithChar('-', i.x+1, next_y, i.x+i.width-1, next_y, i.fg, i.bg) + next_y += 1 + } + if i.text != "" { + DrawStringAtPoint(i.text, i.x+1, next_y, i.fg, i.bg) + next_y += 1 + } + next_y += 3 + if i.show_help { + DrawStringAtPoint("(Y/y) Confirm. (N/n) Reject.", i.x+1, next_y, i.fg, i.bg) + } +} diff --git a/termbox_inputfield.go b/termbox_inputfield.go index 9c1454c..52c258d 100644 --- a/termbox_inputfield.go +++ b/termbox_inputfield.go @@ -69,6 +69,9 @@ func (i *InputField) HandleKeyPress(event termbox.Event) bool { if i.cursor < 0 { i.cursor += 1 } + } else if event.Key == termbox.KeyCtrlU { + // Ctrl+U Clears the Input + i.value = "" } else { if i.cursor+len(i.value) == 0 { i.value = fmt.Sprintf("%s%s", string(event.Ch), i.value) diff --git a/termbox_inputmodal.go b/termbox_inputmodal.go index 0b52635..61f911e 100644 --- a/termbox_inputmodal.go +++ b/termbox_inputmodal.go @@ -13,12 +13,12 @@ type InputModal struct { cursor int bg, fg termbox.Attribute is_done bool - value string } func CreateInputModal(title string, x, y, width, height int, fg, bg termbox.Attribute) *InputModal { i := InputModal{title: title, x: x, y: y, width: width, height: height, fg: fg, bg: bg} i.input = CreateInputField(i.x+1, i.y+3, i.width-2, 2, i.fg, i.bg) + i.show_help = true i.input.bordered = true return &i } @@ -82,9 +82,12 @@ func (i *InputModal) SetDone(b bool) *InputModal { func (i *InputModal) IsDone() bool { return i.is_done } -func (i *InputModal) GetValue() string { - return i.value +func (i *InputModal) GetValue() string { return i.input.GetValue() } +func (i *InputModal) SetValue(s string) *InputModal { + i.input.SetValue(s) + return i } + func (i *InputModal) Clear() *InputModal { i.title = "" i.text = "" @@ -96,7 +99,6 @@ func (i *InputModal) Clear() *InputModal { func (i *InputModal) HandleKeyPress(event termbox.Event) bool { if event.Key == termbox.KeyEnter { // Done editing - i.value = i.input.GetValue() i.is_done = true return true } else { @@ -124,4 +126,7 @@ func (i *InputModal) Draw() { i.input.SetY(next_y) i.input.Draw() next_y += 3 + if i.show_help { + DrawStringAtPoint("(ENTER) to Accept. (ESC) to Cancel.", i.x+1, next_y, i.fg, i.bg) + } } From 6f77695994bcf728ff681d10efe5f1899131a622 Mon Sep 17 00:00:00 2001 From: Brian Buller Date: Mon, 4 May 2015 17:42:35 -0500 Subject: [PATCH 2/2] Some improvements --- termbox_confirmmodal.go | 6 ++++-- termbox_inputmodal.go | 7 +++++-- termbox_util.go | 14 ++++++++++---- 3 files changed, 19 insertions(+), 8 deletions(-) diff --git a/termbox_confirmmodal.go b/termbox_confirmmodal.go index 632663d..61d2483 100644 --- a/termbox_confirmmodal.go +++ b/termbox_confirmmodal.go @@ -124,8 +124,10 @@ func (i *ConfirmModal) Draw() { DrawStringAtPoint(i.text, i.x+1, next_y, i.fg, i.bg) next_y += 1 } - next_y += 3 + next_y += 2 if i.show_help { - DrawStringAtPoint("(Y/y) Confirm. (N/n) Reject.", i.x+1, next_y, i.fg, i.bg) + help_string := " (Y/y) Confirm. (N/n) Reject. " + help_x := (i.x + i.width) - len(help_string) - 1 + DrawStringAtPoint(help_string, help_x, next_y, i.fg, i.bg) } } diff --git a/termbox_inputmodal.go b/termbox_inputmodal.go index 61f911e..62aff5a 100644 --- a/termbox_inputmodal.go +++ b/termbox_inputmodal.go @@ -105,6 +105,7 @@ func (i *InputModal) HandleKeyPress(event termbox.Event) bool { return i.input.HandleKeyPress(event) } } + func (i *InputModal) Draw() { // First blank out the area we'll be putting the modal FillWithChar(' ', i.x, i.y, i.x+i.width, i.y+i.height, i.fg, i.bg) @@ -121,12 +122,14 @@ func (i *InputModal) Draw() { } if i.text != "" { DrawStringAtPoint(i.text, i.x+1, next_y, i.fg, i.bg) - next_y += 1 + next_y += 2 } i.input.SetY(next_y) i.input.Draw() next_y += 3 if i.show_help { - DrawStringAtPoint("(ENTER) to Accept. (ESC) to Cancel.", i.x+1, next_y, i.fg, i.bg) + help_string := " (ENTER) to Accept. (ESC) to Cancel. " + help_x := (i.x + i.width - len(help_string)) - 1 + DrawStringAtPoint(help_string, help_x, next_y, i.fg, i.bg) } } diff --git a/termbox_util.go b/termbox_util.go index 3f6c25b..01b39d3 100644 --- a/termbox_util.go +++ b/termbox_util.go @@ -33,10 +33,16 @@ func FillWithChar(r rune, x1, y1, x2, y2 int, fg termbox.Attribute, bg termbox.A } func DrawBorder(x1, y1, x2, y2 int, fg termbox.Attribute, bg termbox.Attribute) { - FillWithChar('|', x1, y1, x1, y2, fg, bg) - FillWithChar('|', x2, y1, x2, y2, fg, bg) - FillWithChar('-', x1, y1, x2, y1, fg, bg) - FillWithChar('-', x1, y2, x2, y2, fg, bg) + termbox.SetCell(x1, y1, '┌', fg, bg) + FillWithChar('─', x1+1, y1, x2-1, y1, fg, bg) + termbox.SetCell(x2, y1, '┐', fg, bg) + + FillWithChar('|', x1, y1+1, x1, y2-1, fg, bg) + FillWithChar('|', x2, y1+1, x2, y2-1, fg, bg) + + termbox.SetCell(x1, y2, '└', fg, bg) + FillWithChar('─', x1+1, y2, x2-1, y2, fg, bg) + termbox.SetCell(x2, y2, '┘', fg, bg) } func AlignText(txt string, width int, align TextAlignment) string {