1
0
mirror of https://github.com/br0xen/termbox-util.git synced 2024-11-26 07:03:14 +00:00

Add Show() and Hide()

This commit is contained in:
Brian Buller 2015-05-05 15:47:09 -05:00
parent c5cb0ca9fe
commit c581974cc0
2 changed files with 47 additions and 23 deletions

View File

@ -14,6 +14,7 @@ type ConfirmModal struct {
is_done bool is_done bool
accepted bool accepted bool
value string value string
is_visible bool
} }
func CreateConfirmModal(title string, x, y, width, height int, fg, bg termbox.Attribute) *ConfirmModal { 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 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) IsAccepted() bool { return i.accepted }
func (i *ConfirmModal) Clear() *ConfirmModal { func (i *ConfirmModal) Clear() *ConfirmModal {

View File

@ -13,6 +13,7 @@ type InputModal struct {
cursor int cursor int
bg, fg termbox.Attribute bg, fg termbox.Attribute
is_done bool is_done bool
is_visible bool
} }
func CreateInputModal(title string, x, y, width, height int, fg, bg termbox.Attribute) *InputModal { 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 i.fg = fg
return i 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 { func (i *InputModal) SetDone(b bool) *InputModal {
i.is_done = b i.is_done = b
return i return i
@ -94,6 +105,7 @@ func (i *InputModal) Clear() *InputModal {
i.text = "" i.text = ""
i.input.SetValue("") i.input.SetValue("")
i.is_done = false i.is_done = false
i.is_visible = false
return i return i
} }
@ -108,29 +120,31 @@ func (i *InputModal) HandleKeyPress(event termbox.Event) bool {
} }
func (i *InputModal) Draw() { func (i *InputModal) Draw() {
// First blank out the area we'll be putting the modal if i.is_visible {
FillWithChar(' ', i.x, i.y, i.x+i.width, i.y+i.height, i.fg, i.bg) // First blank out the area we'll be putting the modal
// Now draw the border FillWithChar(' ', i.x, i.y, i.x+i.width, i.y+i.height, i.fg, i.bg)
DrawBorder(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 next_y := i.y + 1
// The title // The title
if i.title != "" { if i.title != "" {
DrawStringAtPoint(i.title, i.x+1, next_y, i.fg, i.bg) DrawStringAtPoint(i.title, i.x+1, next_y, i.fg, i.bg)
next_y += 1 next_y += 1
FillWithChar('-', i.x+1, next_y, i.x+i.width-1, next_y, i.fg, i.bg) FillWithChar('-', i.x+1, next_y, i.x+i.width-1, next_y, i.fg, i.bg)
next_y += 1 next_y += 1
} }
if i.text != "" { if i.text != "" {
DrawStringAtPoint(i.text, i.x+1, next_y, i.fg, i.bg) DrawStringAtPoint(i.text, i.x+1, next_y, i.fg, i.bg)
next_y += 2 next_y += 2
} }
i.input.SetY(next_y) i.input.SetY(next_y)
i.input.Draw() i.input.Draw()
next_y += 3 next_y += 3
if i.show_help { if i.show_help {
help_string := " (ENTER) to Accept. (ESC) to Cancel. " help_string := " (ENTER) to Accept. (ESC) to Cancel. "
help_x := (i.x + i.width - len(help_string)) - 1 help_x := (i.x + i.width - len(help_string)) - 1
DrawStringAtPoint(help_string, help_x, next_y, i.fg, i.bg) DrawStringAtPoint(help_string, help_x, next_y, i.fg, i.bg)
}
} }
} }