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

258 lines
6.4 KiB
Go
Raw 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
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
}
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 {
2016-02-09 16:21:57 +00:00
i := InputModal{title: title, x: x, y: y, width: width, height: height, fg: fg, bg: bg, bordered: true}
i.input = CreateInputField(i.x+2, i.y+3, i.width-2, 2, i.fg, i.bg)
2015-10-21 16:36:00 +00:00
i.showHelp = true
i.input.bordered = true
i.isVisible = true
i.inputSelected = true
return &i
}
2016-02-09 16:21:57 +00:00
// GetID returns this control's ID
func (i *InputModal) GetID() string { return i.id }
// SetID sets this control's ID
func (i *InputModal) SetID(newID string) {
i.id = newID
}
2015-10-21 16:36:00 +00:00
// GetTitle Return the title of the modal
func (i *InputModal) GetTitle() string { return i.title }
2015-10-21 16:36:00 +00:00
// SetTitle Sets the title of the modal to s
func (i *InputModal) SetTitle(s string) {
i.title = s
}
2015-10-21 16:36:00 +00:00
// GetText Return the text of the modal
func (i *InputModal) GetText() string { return i.text }
2015-10-21 16:36:00 +00:00
// SetText Set the text of the modal to s
func (i *InputModal) SetText(s string) {
i.text = s
}
2015-10-21 16:36:00 +00:00
// GetX Return the x position of the modal
func (i *InputModal) GetX() int { return i.x }
2015-10-21 16:36:00 +00:00
// SetX set the x position of the modal to x
func (i *InputModal) SetX(x int) {
i.x = x
}
2015-10-21 16:36:00 +00:00
// GetY Return the y position of the modal
func (i *InputModal) GetY() int { return i.y }
2015-10-21 16:36:00 +00:00
// SetY Set the y position of the modal to y
func (i *InputModal) SetY(y int) {
i.y = y
}
2015-10-21 16:36:00 +00:00
// GetWidth Return the width of the modal
func (i *InputModal) GetWidth() int { return i.width }
2015-10-21 16:36:00 +00:00
// SetWidth Set the width of the modal to width
func (i *InputModal) SetWidth(width int) {
i.width = width
}
2015-10-21 16:36:00 +00:00
// GetHeight Return the height of the modal
func (i *InputModal) GetHeight() int { return i.height }
2015-10-21 16:36:00 +00:00
// SetHeight Set the height of the modal to height
func (i *InputModal) SetHeight(height int) {
i.height = height
}
// SetMultiline returns whether this is a multiline modal
func (i *InputModal) SetMultiline(m bool) {
i.input.multiline = m
}
// IsMultiline returns whether this is a multiline modal
func (i *InputModal) IsMultiline() bool {
return i.input.multiline
}
2016-02-09 16:21:57 +00:00
// IsBordered returns whether this control is bordered or not
func (i *InputModal) IsBordered() bool {
return i.bordered
}
// SetBordered sets whether we render a border around the frame
func (i *InputModal) SetBordered(b bool) {
i.bordered = b
}
// IsTabSkipped returns whether this control has it's tabskip flag set
func (i *InputModal) IsTabSkipped() bool {
return i.tabSkip
}
// SetTabSkip sets the tabskip flag for this control
func (i *InputModal) SetTabSkip(b bool) {
i.tabSkip = b
}
2015-10-21 16:36:00 +00:00
// HelpIsShown Returns whether the modal is showing it's help text or not
func (i *InputModal) HelpIsShown() bool { return i.showHelp }
// ShowHelp Set the "Show Help" flag
func (i *InputModal) ShowHelp(b bool) {
2015-10-21 16:36:00 +00:00
i.showHelp = b
}
2016-02-09 16:21:57 +00:00
// GetFgColor returns the foreground color
func (i *InputModal) GetFgColor() termbox.Attribute { return i.fg }
2015-10-21 16:36:00 +00:00
2016-02-09 16:21:57 +00:00
// SetFgColor sets the foreground color
func (i *InputModal) SetFgColor(fg termbox.Attribute) {
i.fg = fg
}
2016-02-09 16:21:57 +00:00
// GetBgColor returns the background color
func (i *InputModal) GetBgColor() termbox.Attribute { return i.bg }
2015-10-21 16:36:00 +00:00
2016-02-09 16:21:57 +00:00
// SetBgColor sets the current background color
func (i *InputModal) SetBgColor(bg termbox.Attribute) {
i.bg = bg
}
2015-10-21 16:36:00 +00:00
// Show Sets the visibility flag to true
func (i *InputModal) Show() {
2015-10-21 16:36:00 +00:00
i.isVisible = true
}
2015-10-21 16:36:00 +00:00
// Hide Sets the visibility flag to false
func (i *InputModal) Hide() {
2015-10-21 16:36:00 +00:00
i.isVisible = false
}
// IsVisible returns the isVisible flag
func (i *InputModal) IsVisible() bool {
return i.isVisible
}
2015-10-21 16:36:00 +00:00
// SetDone Sets the flag that tells whether this modal has completed it's purpose
func (i *InputModal) SetDone(b bool) {
2015-10-21 16:36:00 +00:00
i.isDone = b
}
2015-10-21 16:36:00 +00:00
// IsDone Returns the "isDone" flag
func (i *InputModal) IsDone() bool {
2015-10-21 16:36:00 +00:00
return i.isDone
}
// IsAccepted Returns whether the modal has been accepted
func (i *InputModal) IsAccepted() bool {
return i.isAccepted
}
2015-10-21 16:36:00 +00:00
// GetValue Return the current value of the input
func (i *InputModal) GetValue() string { return i.input.GetValue() }
2015-10-21 16:36:00 +00:00
// SetValue Sets the value of the input to s
func (i *InputModal) SetValue(s string) {
i.input.SetValue(s)
}
// SetInputWrap sets whether the input field will wrap long text or not
func (i *InputModal) SetInputWrap(b bool) {
i.input.SetWrap(b)
}
2015-10-21 16:36:00 +00:00
// Clear Resets all non-positional parameters of the modal
func (i *InputModal) Clear() {
i.title = ""
i.text = ""
i.input.SetValue("")
2015-10-21 16:36:00 +00:00
i.isDone = false
i.isVisible = false
}
2016-02-09 16:21:57 +00:00
// HandleEvent Handle the termbox event, return true if it was consumed
func (i *InputModal) HandleEvent(event termbox.Event) bool {
if event.Key == termbox.KeyEnter {
if !i.input.IsMultiline() || !i.inputSelected {
// Done editing
i.isDone = true
i.isAccepted = true
} else {
i.input.HandleEvent(event)
}
return true
} else if event.Key == termbox.KeyTab {
if i.input.IsMultiline() {
i.inputSelected = !i.inputSelected
}
} else if event.Key == termbox.KeyEsc {
// Done editing
i.isDone = true
i.isAccepted = false
return true
}
return i.input.HandleEvent(event)
}
2015-10-21 16:36:00 +00:00
// Draw Draw the modal
func (i *InputModal) Draw() {
2015-10-21 16:36:00 +00:00
if i.isVisible {
// 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)
2015-10-21 16:36:00 +00:00
nextY := i.y + 1
// The title
if i.title != "" {
if len(i.title) > i.width {
diff := i.width - len(i.title)
2015-10-21 16:36:00 +00:00
DrawStringAtPoint(i.title[:len(i.title)+diff-1], i.x+1, nextY, i.fg, i.bg)
} else {
2015-10-21 16:36:00 +00:00
DrawStringAtPoint(i.title, i.x+1, nextY, i.fg, i.bg)
}
2015-10-21 16:36:00 +00:00
nextY++
FillWithChar('-', i.x+1, nextY, i.x+i.width-1, nextY, i.fg, i.bg)
nextY++
}
if i.text != "" {
2015-10-21 16:36:00 +00:00
DrawStringAtPoint(i.text, i.x+1, nextY, i.fg, i.bg)
nextY++
}
2015-10-21 16:36:00 +00:00
i.input.SetY(nextY)
i.input.Draw()
2015-10-21 16:36:00 +00:00
nextY += 3
if i.showHelp {
helpString := " (ENTER) to Accept. (ESC) to Cancel. "
helpX := (i.x + i.width - len(helpString)) - 1
DrawStringAtPoint(helpString, helpX, nextY, i.fg, i.bg)
}
2016-02-09 16:21:57 +00:00
if i.bordered {
// Now draw the border
DrawBorder(i.x, i.y, i.x+i.width, i.y+i.height, i.fg, i.bg)
}
}
}