2015-10-21 16:24:32 +00:00
|
|
|
package termboxUtil
|
2015-05-18 14:39:31 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/nsf/termbox-go"
|
|
|
|
)
|
|
|
|
|
2015-10-21 16:24:32 +00:00
|
|
|
// ConfirmModal is a modal with yes/no (or similar) buttons
|
2015-05-18 14:39:31 +00:00
|
|
|
type ConfirmModal struct {
|
2016-02-09 16:21:57 +00:00
|
|
|
id string
|
2015-05-18 14:39:31 +00:00
|
|
|
title string
|
|
|
|
text string
|
|
|
|
x, y, width, height int
|
2015-10-21 16:24:32 +00:00
|
|
|
showHelp bool
|
2015-05-18 14:39:31 +00:00
|
|
|
cursor int
|
|
|
|
bg, fg termbox.Attribute
|
2015-10-21 16:24:32 +00:00
|
|
|
isDone bool
|
2015-05-18 14:39:31 +00:00
|
|
|
accepted bool
|
|
|
|
value string
|
2015-10-21 16:24:32 +00:00
|
|
|
isVisible bool
|
2016-02-09 16:21:57 +00:00
|
|
|
bordered bool
|
|
|
|
tabSkip bool
|
2015-05-18 14:39:31 +00:00
|
|
|
}
|
|
|
|
|
2015-10-21 16:24:32 +00:00
|
|
|
// CreateConfirmModal Creates a confirmation modal with the specified attributes
|
2015-05-18 14:39:31 +00:00
|
|
|
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?"
|
|
|
|
}
|
2015-10-21 16:24:32 +00:00
|
|
|
i.showHelp = true
|
2015-05-18 14:39:31 +00:00
|
|
|
return &i
|
|
|
|
}
|
|
|
|
|
2016-02-09 16:21:57 +00:00
|
|
|
// GetID returns this control's ID
|
|
|
|
func (i *ConfirmModal) GetID() string { return i.id }
|
|
|
|
|
|
|
|
// SetID sets this control's ID
|
|
|
|
func (i *ConfirmModal) SetID(newID string) {
|
|
|
|
i.id = newID
|
|
|
|
}
|
|
|
|
|
2015-10-21 16:24:32 +00:00
|
|
|
// GetTitle returns the current title of the modal
|
2015-05-18 14:39:31 +00:00
|
|
|
func (i *ConfirmModal) GetTitle() string { return i.title }
|
2015-10-21 16:24:32 +00:00
|
|
|
|
|
|
|
// SetTitle sets the current title of the modal to s
|
2016-01-26 21:33:25 +00:00
|
|
|
func (i *ConfirmModal) SetTitle(s string) {
|
2015-05-18 14:39:31 +00:00
|
|
|
i.title = s
|
|
|
|
}
|
|
|
|
|
2015-10-21 16:24:32 +00:00
|
|
|
// GetText returns the current text of the modal
|
2015-05-18 14:39:31 +00:00
|
|
|
func (i *ConfirmModal) GetText() string { return i.text }
|
2015-10-21 16:24:32 +00:00
|
|
|
|
|
|
|
// SetText sets the text of the modal to s
|
2016-01-26 21:33:25 +00:00
|
|
|
func (i *ConfirmModal) SetText(s string) {
|
2015-05-18 14:39:31 +00:00
|
|
|
i.text = s
|
|
|
|
}
|
|
|
|
|
2015-10-21 16:24:32 +00:00
|
|
|
// GetX returns the current x coordinate of the modal
|
2015-05-18 14:39:31 +00:00
|
|
|
func (i *ConfirmModal) GetX() int { return i.x }
|
2015-10-21 16:24:32 +00:00
|
|
|
|
|
|
|
// SetX sets the current x coordinate of the modal to x
|
2016-01-26 21:33:25 +00:00
|
|
|
func (i *ConfirmModal) SetX(x int) {
|
2015-05-18 14:39:31 +00:00
|
|
|
i.x = x
|
|
|
|
}
|
2015-10-21 16:24:32 +00:00
|
|
|
|
|
|
|
// GetY returns the current y coordinate of the modal
|
2015-05-18 14:39:31 +00:00
|
|
|
func (i *ConfirmModal) GetY() int { return i.y }
|
2015-10-21 16:24:32 +00:00
|
|
|
|
|
|
|
// SetY sets the current y coordinate of the modal to y
|
2016-01-26 21:33:25 +00:00
|
|
|
func (i *ConfirmModal) SetY(y int) {
|
2015-05-18 14:39:31 +00:00
|
|
|
i.y = y
|
|
|
|
}
|
|
|
|
|
2015-10-21 16:24:32 +00:00
|
|
|
// GetWidth returns the current width of the modal
|
2015-05-18 14:39:31 +00:00
|
|
|
func (i *ConfirmModal) GetWidth() int { return i.width }
|
2015-10-21 16:24:32 +00:00
|
|
|
|
|
|
|
// SetWidth sets the current modal width to width
|
2016-01-26 21:33:25 +00:00
|
|
|
func (i *ConfirmModal) SetWidth(width int) {
|
2015-05-18 14:39:31 +00:00
|
|
|
i.width = width
|
|
|
|
}
|
|
|
|
|
2015-10-21 16:24:32 +00:00
|
|
|
// GetHeight returns the current height of the modal
|
2015-05-18 14:39:31 +00:00
|
|
|
func (i *ConfirmModal) GetHeight() int { return i.height }
|
2015-10-21 16:24:32 +00:00
|
|
|
|
|
|
|
// SetHeight set the height of the modal to height
|
2016-01-26 21:33:25 +00:00
|
|
|
func (i *ConfirmModal) SetHeight(height int) {
|
2015-05-18 14:39:31 +00:00
|
|
|
i.height = height
|
|
|
|
}
|
|
|
|
|
2015-10-21 16:24:32 +00:00
|
|
|
// HelpIsShown returns true or false if the help is displayed
|
|
|
|
func (i *ConfirmModal) HelpIsShown() bool { return i.showHelp }
|
|
|
|
|
|
|
|
// ShowHelp sets whether or not to display the help text
|
2016-01-26 21:33:25 +00:00
|
|
|
func (i *ConfirmModal) ShowHelp(b bool) {
|
2015-10-21 16:24:32 +00:00
|
|
|
i.showHelp = b
|
2015-05-18 14:39:31 +00:00
|
|
|
}
|
|
|
|
|
2016-02-09 16:21:57 +00:00
|
|
|
// GetFgColor returns the foreground color
|
|
|
|
func (i *ConfirmModal) GetFgColor() termbox.Attribute { return i.fg }
|
2015-10-21 16:24:32 +00:00
|
|
|
|
2016-02-09 16:21:57 +00:00
|
|
|
// SetFgColor sets the foreground color
|
|
|
|
func (i *ConfirmModal) SetFgColor(fg termbox.Attribute) {
|
|
|
|
i.fg = fg
|
2015-05-18 14:39:31 +00:00
|
|
|
}
|
|
|
|
|
2016-02-09 16:21:57 +00:00
|
|
|
// GetBgColor returns the background color
|
|
|
|
func (i *ConfirmModal) GetBgColor() termbox.Attribute { return i.bg }
|
2015-10-21 16:24:32 +00:00
|
|
|
|
2016-02-09 16:21:57 +00:00
|
|
|
// SetBgColor sets the current background color
|
|
|
|
func (i *ConfirmModal) SetBgColor(bg termbox.Attribute) {
|
|
|
|
i.bg = bg
|
2015-05-18 14:39:31 +00:00
|
|
|
}
|
|
|
|
|
2015-10-21 16:24:32 +00:00
|
|
|
// IsDone returns whether the user has answered the modal
|
|
|
|
func (i *ConfirmModal) IsDone() bool { return i.isDone }
|
|
|
|
|
|
|
|
// SetDone sets whether the modal has completed it's purpose
|
2016-01-26 21:33:25 +00:00
|
|
|
func (i *ConfirmModal) SetDone(b bool) {
|
2015-10-21 16:24:32 +00:00
|
|
|
i.isDone = b
|
2015-05-18 14:39:31 +00:00
|
|
|
}
|
|
|
|
|
2015-10-21 16:24:32 +00:00
|
|
|
// Show sets the visibility flag of the modal to true
|
2016-01-26 21:33:25 +00:00
|
|
|
func (i *ConfirmModal) Show() {
|
2015-10-21 16:24:32 +00:00
|
|
|
i.isVisible = true
|
2015-05-18 14:39:31 +00:00
|
|
|
}
|
2015-10-21 16:24:32 +00:00
|
|
|
|
|
|
|
// Hide sets the visibility flag of the modal to false
|
2016-01-26 21:33:25 +00:00
|
|
|
func (i *ConfirmModal) Hide() {
|
2015-10-21 16:24:32 +00:00
|
|
|
i.isVisible = false
|
2015-05-18 14:39:31 +00:00
|
|
|
}
|
|
|
|
|
2015-10-21 16:24:32 +00:00
|
|
|
// IsAccepted returns whether the user accepted the modal
|
2015-05-18 14:39:31 +00:00
|
|
|
func (i *ConfirmModal) IsAccepted() bool { return i.accepted }
|
|
|
|
|
2015-10-21 16:24:32 +00:00
|
|
|
// Clear clears all of the non-positional parameters of the modal
|
2016-01-26 21:33:25 +00:00
|
|
|
func (i *ConfirmModal) Clear() {
|
2015-05-18 14:39:31 +00:00
|
|
|
i.title = ""
|
|
|
|
i.text = ""
|
|
|
|
i.accepted = false
|
2015-10-21 16:24:32 +00:00
|
|
|
i.isDone = false
|
2015-05-18 14:39:31 +00:00
|
|
|
}
|
|
|
|
|
2016-02-09 16:21:57 +00:00
|
|
|
// IsBordered returns whether this modal is bordered or not
|
|
|
|
func (i *ConfirmModal) IsBordered() bool {
|
|
|
|
return i.bordered
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetBordered sets whether we render a border around the frame
|
|
|
|
func (i *ConfirmModal) SetBordered(b bool) {
|
|
|
|
i.bordered = b
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsTabSkipped returns whether this modal has it's tabskip flag set
|
|
|
|
func (i *ConfirmModal) IsTabSkipped() bool {
|
|
|
|
return i.tabSkip
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetTabSkip sets the tabskip flag for this control
|
|
|
|
func (i *ConfirmModal) SetTabSkip(b bool) {
|
|
|
|
i.tabSkip = b
|
|
|
|
}
|
|
|
|
|
|
|
|
// HandleEvent handles the termbox event and returns whether it was consumed
|
|
|
|
func (i *ConfirmModal) HandleEvent(event termbox.Event) bool {
|
2016-02-19 14:51:24 +00:00
|
|
|
if event.Ch == 'Y' || event.Ch == 'y' {
|
|
|
|
i.accepted = true
|
|
|
|
i.isDone = true
|
|
|
|
return true
|
|
|
|
} else if event.Ch == 'N' || event.Ch == 'n' {
|
|
|
|
i.accepted = false
|
|
|
|
i.isDone = true
|
|
|
|
return true
|
2015-05-18 14:39:31 +00:00
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2015-10-21 16:24:32 +00:00
|
|
|
|
|
|
|
// Draw draws the modal
|
2015-05-18 14:39:31 +00:00
|
|
|
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)
|
|
|
|
|
2015-10-21 16:24:32 +00:00
|
|
|
nextY := i.y + 1
|
2015-05-18 14:39:31 +00:00
|
|
|
// The title
|
|
|
|
if i.title != "" {
|
2015-10-21 16:24:32 +00:00
|
|
|
DrawStringAtPoint(i.title, i.x+1, nextY, i.fg, i.bg)
|
|
|
|
nextY++
|
|
|
|
FillWithChar('-', i.x+1, nextY, i.x+i.width-1, nextY, i.fg, i.bg)
|
|
|
|
nextY++
|
2015-05-18 14:39:31 +00:00
|
|
|
}
|
|
|
|
if i.text != "" {
|
2015-10-21 16:24:32 +00:00
|
|
|
DrawStringAtPoint(i.text, i.x+1, nextY, i.fg, i.bg)
|
2015-05-18 14:39:31 +00:00
|
|
|
}
|
2015-10-21 16:24:32 +00:00
|
|
|
nextY += 2
|
|
|
|
if i.showHelp {
|
|
|
|
helpString := " (Y/y) Confirm. (N/n) Reject. "
|
|
|
|
helpX := (i.x + i.width) - len(helpString) - 1
|
|
|
|
DrawStringAtPoint(helpString, helpX, nextY, i.fg, i.bg)
|
2015-05-18 14:39:31 +00:00
|
|
|
}
|
|
|
|
}
|