1
0
mirror of https://github.com/br0xen/termbox-util.git synced 2024-11-29 16:13:14 +00:00
termbox-util/termbox_confirmmodal.go

185 lines
4.9 KiB
Go
Raw Normal View History

2015-10-21 16:51:22 +00:00
package termboxUtil
import (
"github.com/nsf/termbox-go"
)
2015-10-21 16:51:22 +00:00
// ConfirmModal is a modal with yes/no (or similar) buttons
type ConfirmModal struct {
title string
text string
x, y, width, height int
2015-10-21 16:51:22 +00:00
showHelp bool
cursor int
bg, fg termbox.Attribute
2015-10-21 16:51:22 +00:00
isDone bool
accepted bool
value string
2015-10-21 16:51:22 +00:00
isVisible bool
}
2015-10-21 16:51:22 +00:00
// CreateConfirmModal Creates a confirmation modal with the specified attributes
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:51:22 +00:00
i.showHelp = true
return &i
}
2015-10-21 16:51:22 +00:00
// GetTitle returns the current title of the modal
func (i *ConfirmModal) GetTitle() string { return i.title }
2015-10-21 16:51:22 +00:00
// SetTitle sets the current title of the modal to s
func (i *ConfirmModal) SetTitle(s string) *ConfirmModal {
i.title = s
return i
}
2015-10-21 16:51:22 +00:00
// GetText returns the current text of the modal
func (i *ConfirmModal) GetText() string { return i.text }
2015-10-21 16:51:22 +00:00
// SetText sets the text of the modal to s
func (i *ConfirmModal) SetText(s string) *ConfirmModal {
i.text = s
return i
}
2015-10-21 16:51:22 +00:00
// GetX returns the current x coordinate of the modal
func (i *ConfirmModal) GetX() int { return i.x }
2015-10-21 16:51:22 +00:00
// SetX sets the current x coordinate of the modal to x
func (i *ConfirmModal) SetX(x int) *ConfirmModal {
i.x = x
return i
}
2015-10-21 16:51:22 +00:00
// GetY returns the current y coordinate of the modal
func (i *ConfirmModal) GetY() int { return i.y }
2015-10-21 16:51:22 +00:00
// SetY sets the current y coordinate of the modal to y
func (i *ConfirmModal) SetY(y int) *ConfirmModal {
i.y = y
return i
}
2015-10-21 16:51:22 +00:00
// GetWidth returns the current width of the modal
func (i *ConfirmModal) GetWidth() int { return i.width }
2015-10-21 16:51:22 +00:00
// SetWidth sets the current modal width to width
func (i *ConfirmModal) SetWidth(width int) *ConfirmModal {
i.width = width
return i
}
2015-10-21 16:51:22 +00:00
// GetHeight returns the current height of the modal
func (i *ConfirmModal) GetHeight() int { return i.height }
2015-10-21 16:51:22 +00:00
// SetHeight set the height of the modal to height
func (i *ConfirmModal) SetHeight(height int) *ConfirmModal {
i.height = height
return i
}
2015-10-21 16:51:22 +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
func (i *ConfirmModal) ShowHelp(b bool) *ConfirmModal {
2015-10-21 16:51:22 +00:00
i.showHelp = b
return i
}
2015-10-21 16:51:22 +00:00
// GetBackground returns the current background color
func (i *ConfirmModal) GetBackground() termbox.Attribute { return i.bg }
2015-10-21 16:51:22 +00:00
// SetBackground sets the background color to bg
func (i *ConfirmModal) SetBackground(bg termbox.Attribute) *ConfirmModal {
i.bg = bg
return i
}
2015-10-21 16:51:22 +00:00
// GetForeground returns the current foreground color
func (i *ConfirmModal) GetForeground() termbox.Attribute { return i.fg }
2015-10-21 16:51:22 +00:00
// SetForeground sets the current foreground color to fg
func (i *ConfirmModal) SetForeground(fg termbox.Attribute) *ConfirmModal {
i.fg = fg
return i
}
2015-10-21 16:51:22 +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
func (i *ConfirmModal) SetDone(b bool) *ConfirmModal {
2015-10-21 16:51:22 +00:00
i.isDone = b
return i
}
2015-10-21 16:51:22 +00:00
// Show sets the visibility flag of the modal to true
2015-05-05 20:47:09 +00:00
func (i *ConfirmModal) Show() *ConfirmModal {
2015-10-21 16:51:22 +00:00
i.isVisible = true
2015-05-05 20:47:09 +00:00
return i
}
2015-10-21 16:51:22 +00:00
// Hide sets the visibility flag of the modal to false
2015-05-05 20:47:09 +00:00
func (i *ConfirmModal) Hide() *ConfirmModal {
2015-10-21 16:51:22 +00:00
i.isVisible = false
2015-05-05 20:47:09 +00:00
return i
}
2015-10-21 16:51:22 +00:00
// IsAccepted returns whether the user accepted the modal
func (i *ConfirmModal) IsAccepted() bool { return i.accepted }
2015-10-21 16:51:22 +00:00
// Clear clears all of the non-positional parameters of the modal
func (i *ConfirmModal) Clear() *ConfirmModal {
i.title = ""
i.text = ""
i.accepted = false
2015-10-21 16:51:22 +00:00
i.isDone = false
return i
}
2015-10-21 16:51:22 +00:00
// HandleKeyPress handles the termbox event and returns whether it was consumed
func (i *ConfirmModal) HandleKeyPress(event termbox.Event) bool {
if event.Ch == 'Y' || event.Ch == 'y' {
i.accepted = true
2015-10-21 16:51:22 +00:00
i.isDone = true
return true
} else if event.Ch == 'N' || event.Ch == 'n' {
i.accepted = false
2015-10-21 16:51:22 +00:00
i.isDone = true
return true
}
return false
}
2015-10-21 16:51:22 +00:00
// Draw draws the modal
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:51:22 +00:00
nextY := i.y + 1
// The title
if i.title != "" {
2015-10-21 16:51:22 +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++
}
if i.text != "" {
2015-10-21 16:51:22 +00:00
DrawStringAtPoint(i.text, i.x+1, nextY, i.fg, i.bg)
nextY++
}
2015-10-21 16:51:22 +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)
}
}