From c581974cc0d13f4da5073ffa44199174b35506dc Mon Sep 17 00:00:00 2001 From: Brian Buller Date: Tue, 5 May 2015 15:47:09 -0500 Subject: [PATCH] Add Show() and Hide() --- termbox_confirmmodal.go | 10 +++++++ termbox_inputmodal.go | 60 +++++++++++++++++++++++++---------------- 2 files changed, 47 insertions(+), 23 deletions(-) diff --git a/termbox_confirmmodal.go b/termbox_confirmmodal.go index 61d2483..fc247c1 100644 --- a/termbox_confirmmodal.go +++ b/termbox_confirmmodal.go @@ -14,6 +14,7 @@ type ConfirmModal struct { is_done bool accepted bool value string + is_visible bool } func CreateConfirmModal(title string, x, y, width, height int, fg, bg termbox.Attribute) *ConfirmModal { @@ -84,6 +85,15 @@ func (i *ConfirmModal) SetDone(b bool) *ConfirmModal { return i } +func (i *ConfirmModal) Show() *ConfirmModal { + i.is_visible = true + return i +} +func (i *ConfirmModal) Hide() *ConfirmModal { + i.is_visible = false + return i +} + func (i *ConfirmModal) IsAccepted() bool { return i.accepted } func (i *ConfirmModal) Clear() *ConfirmModal { diff --git a/termbox_inputmodal.go b/termbox_inputmodal.go index 2f86f68..4928b5a 100644 --- a/termbox_inputmodal.go +++ b/termbox_inputmodal.go @@ -13,6 +13,7 @@ type InputModal struct { cursor int bg, fg termbox.Attribute is_done bool + is_visible bool } func CreateInputModal(title string, x, y, width, height int, fg, bg termbox.Attribute) *InputModal { @@ -75,6 +76,16 @@ func (i *InputModal) SetForeground(fg termbox.Attribute) *InputModal { i.fg = fg return i } + +func (i *InputModal) Show() *InputModal { + i.is_visible = true + return i +} +func (i *InputModal) Hide() *InputModal { + i.is_visible = false + return i +} + func (i *InputModal) SetDone(b bool) *InputModal { i.is_done = b return i @@ -94,6 +105,7 @@ func (i *InputModal) Clear() *InputModal { i.text = "" i.input.SetValue("") i.is_done = false + i.is_visible = false return i } @@ -108,29 +120,31 @@ func (i *InputModal) HandleKeyPress(event termbox.Event) bool { } 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) - // Now draw the border - DrawBorder(i.x, i.y, i.x+i.width, i.y+i.height, i.fg, i.bg) + if i.is_visible { + // 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 += 2 - } - i.input.SetY(next_y) - i.input.Draw() - next_y += 3 - if i.show_help { - 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) + 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 += 2 + } + i.input.SetY(next_y) + i.input.Draw() + next_y += 3 + if i.show_help { + 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) + } } }