termbox-util/termbox_inputmodal.go

265 lines
6.8 KiB
Go
Raw Permalink Normal View History

2015-10-21 16:36:00 +00:00
package termboxUtil
import (
"github.com/nsf/termbox-go"
)
2015-10-21 16:36:00 +00:00
// InputModal A modal for text input
type InputModal struct {
2016-02-09 16:21:57 +00:00
id string
title string
text string
input *InputField
x, y, width, height int
2015-10-21 16:36:00 +00:00
showHelp bool
cursor int
bg, fg termbox.Attribute
2019-03-25 15:10:25 +00:00
activeFg, activeBg termbox.Attribute
2015-10-21 16:36:00 +00:00
isDone bool
isAccepted bool
2015-10-21 16:36:00 +00:00
isVisible bool
2016-02-09 16:21:57 +00:00
bordered bool
tabSkip bool
inputSelected bool
2019-03-25 15:10:25 +00:00
active bool
}
2015-10-21 16:36:00 +00:00
// CreateInputModal Create an input modal with the given attributes
func CreateInputModal(title string, x, y, width, height int, fg, bg termbox.Attribute) *InputModal {
2019-03-25 15:10:25 +00:00
c := InputModal{title: title, x: x, y: y, width: width, height: height, fg: fg, bg: bg, bordered: true}
c.input = CreateInputField(c.x+2, c.y+3, c.width-2, 2, c.fg, c.bg)
c.showHelp = true
c.input.bordered = true
c.isVisible = true
c.inputSelected = true
return &c
}
2019-03-25 15:10:25 +00:00
func (c *InputModal) SetActiveFgColor(fg termbox.Attribute) { c.activeFg = fg }
func (c *InputModal) SetActiveBgColor(bg termbox.Attribute) { c.activeBg = bg }
func (c *InputModal) SetActive(a bool) { c.active = a }
func (c *InputModal) IsActive() bool { return c.active }
2016-02-09 16:21:57 +00:00
// GetID returns this control's ID
2019-03-25 15:10:25 +00:00
func (c *InputModal) GetID() string { return c.id }
2016-02-09 16:21:57 +00:00
// SetID sets this control's ID
2019-03-25 15:10:25 +00:00
func (c *InputModal) SetID(newID string) {
c.id = newID
2016-02-09 16:21:57 +00:00
}
2015-10-21 16:36:00 +00:00
// GetTitle Return the title of the modal
2019-03-25 15:10:25 +00:00
func (c *InputModal) GetTitle() string { return c.title }
2015-10-21 16:36:00 +00:00
// SetTitle Sets the title of the modal to s
2019-03-25 15:10:25 +00:00
func (c *InputModal) SetTitle(s string) {
c.title = s
}
2015-10-21 16:36:00 +00:00
// GetText Return the text of the modal
2019-03-25 15:10:25 +00:00
func (c *InputModal) GetText() string { return c.text }
2015-10-21 16:36:00 +00:00
// SetText Set the text of the modal to s
2019-03-25 15:10:25 +00:00
func (c *InputModal) SetText(s string) {
c.text = s
}
2015-10-21 16:36:00 +00:00
// GetX Return the x position of the modal
2019-03-25 15:10:25 +00:00
func (c *InputModal) GetX() int { return c.x }
2015-10-21 16:36:00 +00:00
// SetX set the x position of the modal to x
2019-03-25 15:10:25 +00:00
func (c *InputModal) SetX(x int) {
c.x = x
}
2015-10-21 16:36:00 +00:00
// GetY Return the y position of the modal
2019-03-25 15:10:25 +00:00
func (c *InputModal) GetY() int { return c.y }
2015-10-21 16:36:00 +00:00
// SetY Set the y position of the modal to y
2019-03-25 15:10:25 +00:00
func (c *InputModal) SetY(y int) {
c.y = y
}
2015-10-21 16:36:00 +00:00
// GetWidth Return the width of the modal
2019-03-25 15:10:25 +00:00
func (c *InputModal) GetWidth() int { return c.width }
2015-10-21 16:36:00 +00:00
// SetWidth Set the width of the modal to width
2019-03-25 15:10:25 +00:00
func (c *InputModal) SetWidth(width int) {
c.width = width
}
2015-10-21 16:36:00 +00:00
// GetHeight Return the height of the modal
2019-03-25 15:10:25 +00:00
func (c *InputModal) GetHeight() int { return c.height }
2015-10-21 16:36:00 +00:00
// SetHeight Set the height of the modal to height
2019-03-25 15:10:25 +00:00
func (c *InputModal) SetHeight(height int) {
c.height = height
}
// SetMultiline returns whether this is a multiline modal
2019-03-25 15:10:25 +00:00
func (c *InputModal) SetMultiline(m bool) {
c.input.multiline = m
}
// IsMultiline returns whether this is a multiline modal
2019-03-25 15:10:25 +00:00
func (c *InputModal) IsMultiline() bool {
return c.input.multiline
}
2016-02-09 16:21:57 +00:00
// IsBordered returns whether this control is bordered or not
2019-03-25 15:10:25 +00:00
func (c *InputModal) IsBordered() bool {
return c.bordered
2016-02-09 16:21:57 +00:00
}
// SetBordered sets whether we render a border around the frame
2019-03-25 15:10:25 +00:00
func (c *InputModal) SetBordered(b bool) {
c.bordered = b
2016-02-09 16:21:57 +00:00
}
// IsTabSkipped returns whether this control has it's tabskip flag set
2019-03-25 15:10:25 +00:00
func (c *InputModal) IsTabSkipped() bool {
return c.tabSkip
2016-02-09 16:21:57 +00:00
}
// SetTabSkip sets the tabskip flag for this control
2019-03-25 15:10:25 +00:00
func (c *InputModal) SetTabSkip(b bool) {
c.tabSkip = b
2016-02-09 16:21:57 +00:00
}
2015-10-21 16:36:00 +00:00
// HelpIsShown Returns whether the modal is showing it's help text or not
2019-03-25 15:10:25 +00:00
func (c *InputModal) HelpIsShown() bool { return c.showHelp }
2015-10-21 16:36:00 +00:00
// ShowHelp Set the "Show Help" flag
2019-03-25 15:10:25 +00:00
func (c *InputModal) ShowHelp(b bool) {
c.showHelp = b
}
2016-02-09 16:21:57 +00:00
// GetFgColor returns the foreground color
2019-03-25 15:10:25 +00:00
func (c *InputModal) GetFgColor() termbox.Attribute { return c.fg }
2015-10-21 16:36:00 +00:00
2016-02-09 16:21:57 +00:00
// SetFgColor sets the foreground color
2019-03-25 15:10:25 +00:00
func (c *InputModal) SetFgColor(fg termbox.Attribute) {
c.fg = fg
}
2016-02-09 16:21:57 +00:00
// GetBgColor returns the background color
2019-03-25 15:10:25 +00:00
func (c *InputModal) GetBgColor() termbox.Attribute { return c.bg }
2015-10-21 16:36:00 +00:00
2016-02-09 16:21:57 +00:00
// SetBgColor sets the current background color
2019-03-25 15:10:25 +00:00
func (c *InputModal) SetBgColor(bg termbox.Attribute) {
c.bg = bg
}
2015-10-21 16:36:00 +00:00
// Show Sets the visibility flag to true
2019-03-25 15:10:25 +00:00
func (c *InputModal) Show() {
c.isVisible = true
}
2015-10-21 16:36:00 +00:00
// Hide Sets the visibility flag to false
2019-03-25 15:10:25 +00:00
func (c *InputModal) Hide() {
c.isVisible = false
}
// IsVisible returns the isVisible flag
2019-03-25 15:10:25 +00:00
func (c *InputModal) IsVisible() bool {
return c.isVisible
}
2015-10-21 16:36:00 +00:00
// SetDone Sets the flag that tells whether this modal has completed it's purpose
2019-03-25 15:10:25 +00:00
func (c *InputModal) SetDone(b bool) {
c.isDone = b
}
2015-10-21 16:36:00 +00:00
// IsDone Returns the "isDone" flag
2019-03-25 15:10:25 +00:00
func (c *InputModal) IsDone() bool {
return c.isDone
}
// IsAccepted Returns whether the modal has been accepted
2019-03-25 15:10:25 +00:00
func (c *InputModal) IsAccepted() bool {
return c.isAccepted
}
2015-10-21 16:36:00 +00:00
// GetValue Return the current value of the input
2019-03-25 15:10:25 +00:00
func (c *InputModal) GetValue() string { return c.input.GetValue() }
2015-10-21 16:36:00 +00:00
// SetValue Sets the value of the input to s
2019-03-25 15:10:25 +00:00
func (c *InputModal) SetValue(s string) {
c.input.SetValue(s)
}
// SetInputWrap sets whether the input field will wrap long text or not
2019-03-25 15:10:25 +00:00
func (c *InputModal) SetInputWrap(b bool) {
c.input.SetWrap(b)
}
2015-10-21 16:36:00 +00:00
// Clear Resets all non-positional parameters of the modal
2019-03-25 15:10:25 +00:00
func (c *InputModal) Clear() {
c.title = ""
c.text = ""
c.input.SetValue("")
c.isDone = false
c.isVisible = false
}
2016-02-09 16:21:57 +00:00
// HandleEvent Handle the termbox event, return true if it was consumed
2019-03-25 15:10:25 +00:00
func (c *InputModal) HandleEvent(event termbox.Event) bool {
if event.Key == termbox.KeyEnter {
2019-03-25 15:10:25 +00:00
if !c.input.IsMultiline() || !c.inputSelected {
// Done editing
2019-03-25 15:10:25 +00:00
c.isDone = true
c.isAccepted = true
} else {
2019-03-25 15:10:25 +00:00
c.input.HandleEvent(event)
}
return true
} else if event.Key == termbox.KeyTab {
2019-03-25 15:10:25 +00:00
if c.input.IsMultiline() {
c.inputSelected = !c.inputSelected
}
} else if event.Key == termbox.KeyEsc {
// Done editing
2019-03-25 15:10:25 +00:00
c.isDone = true
c.isAccepted = false
return true
}
2019-03-25 15:10:25 +00:00
return c.input.HandleEvent(event)
}
2015-10-21 16:36:00 +00:00
// Draw Draw the modal
2019-03-25 15:10:25 +00:00
func (c *InputModal) Draw() {
if c.isVisible {
// First blank out the area we'll be putting the modal
2019-03-25 15:10:25 +00:00
FillWithChar(' ', c.x, c.y, c.x+c.width, c.y+c.height, c.fg, c.bg)
nextY := c.y + 1
// The title
2019-03-25 15:10:25 +00:00
if c.title != "" {
if len(c.title) > c.width {
diff := c.width - len(c.title)
DrawStringAtPoint(c.title[:len(c.title)+diff-1], c.x+1, nextY, c.fg, c.bg)
} else {
2019-03-25 15:10:25 +00:00
DrawStringAtPoint(c.title, c.x+1, nextY, c.fg, c.bg)
}
2015-10-21 16:36:00 +00:00
nextY++
2019-03-25 15:10:25 +00:00
FillWithChar('-', c.x+1, nextY, c.x+c.width-1, nextY, c.fg, c.bg)
2015-10-21 16:36:00 +00:00
nextY++
}
2019-03-25 15:10:25 +00:00
if c.text != "" {
DrawStringAtPoint(c.text, c.x+1, nextY, c.fg, c.bg)
2015-10-21 16:36:00 +00:00
nextY++
}
2019-03-25 15:10:25 +00:00
c.input.SetY(nextY)
c.input.Draw()
2015-10-21 16:36:00 +00:00
nextY += 3
2019-03-25 15:10:25 +00:00
if c.showHelp {
2015-10-21 16:36:00 +00:00
helpString := " (ENTER) to Accept. (ESC) to Cancel. "
2019-03-25 15:10:25 +00:00
helpX := (c.x + c.width - len(helpString)) - 1
DrawStringAtPoint(helpString, helpX, nextY, c.fg, c.bg)
}
2019-03-25 15:10:25 +00:00
if c.bordered {
2016-02-09 16:21:57 +00:00
// Now draw the border
2019-03-25 15:10:25 +00:00
DrawBorder(c.x, c.y, c.x+c.width, c.y+c.height, c.fg, c.bg)
2016-02-09 16:21:57 +00:00
}
}
}