Add Confirm Modal

Also update inputfield/inputmodal
This commit is contained in:
Brian Buller 2015-05-04 13:52:10 -05:00
parent 0fc0c58c6d
commit cf3fa0559d
3 changed files with 143 additions and 4 deletions

131
termbox_confirmmodal.go Normal file
View File

@ -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)
}
}

View File

@ -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)

View File

@ -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)
}
}