1
0
mirror of https://github.com/br0xen/termbox-util.git synced 2024-11-22 21:43:14 +00:00
termbox-util/termbox_inputmodal.go

202 lines
5.1 KiB
Go
Raw Normal View History

2015-10-21 16:51:22 +00:00
package termboxUtil
2015-05-02 03:15:36 +00:00
import (
"github.com/nsf/termbox-go"
)
2015-10-21 16:51:22 +00:00
// InputModal A modal for text input
2015-05-02 03:15:36 +00:00
type InputModal struct {
title string
text string
2015-05-03 22:11:50 +00:00
input *InputField
2015-05-02 03:15:36 +00:00
x, y, width, height int
2015-10-21 16:51:22 +00:00
showHelp bool
2015-05-02 03:15:36 +00:00
cursor int
bg, fg termbox.Attribute
2015-10-21 16:51:22 +00:00
isDone bool
isVisible bool
2015-05-02 03:15:36 +00:00
}
2015-10-21 16:51:22 +00:00
// CreateInputModal Create an input modal with the given attributes
2015-05-03 22:11:50 +00:00
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)
2015-10-21 16:51:22 +00:00
i.showHelp = true
2015-05-03 22:11:50 +00:00
i.input.bordered = true
2015-05-02 03:15:36 +00:00
return &i
}
2015-10-21 16:51:22 +00:00
// GetTitle Return the title of the modal
2015-05-02 03:15:36 +00:00
func (i *InputModal) GetTitle() string { return i.title }
2015-10-21 16:51:22 +00:00
// SetTitle Sets the title of the modal to s
2015-05-02 03:15:36 +00:00
func (i *InputModal) SetTitle(s string) *InputModal {
i.title = s
return i
}
2015-10-21 16:51:22 +00:00
// GetText Return the text of the modal
2015-05-02 03:15:36 +00:00
func (i *InputModal) GetText() string { return i.text }
2015-10-21 16:51:22 +00:00
// SetText Set the text of the modal to s
2015-05-02 03:15:36 +00:00
func (i *InputModal) SetText(s string) *InputModal {
i.text = s
return i
}
2015-10-21 16:51:22 +00:00
// GetX Return the x position of the modal
2015-05-02 03:15:36 +00:00
func (i *InputModal) GetX() int { return i.x }
2015-10-21 16:51:22 +00:00
// SetX set the x position of the modal to x
2015-05-02 03:15:36 +00:00
func (i *InputModal) SetX(x int) *InputModal {
i.x = x
return i
}
2015-10-21 16:51:22 +00:00
// GetY Return the y position of the modal
2015-05-02 03:15:36 +00:00
func (i *InputModal) GetY() int { return i.y }
2015-10-21 16:51:22 +00:00
// SetY Set the y position of the modal to y
2015-05-02 03:15:36 +00:00
func (i *InputModal) SetY(y int) *InputModal {
i.y = y
return i
}
2015-10-21 16:51:22 +00:00
// GetWidth Return the width of the modal
2015-05-02 03:15:36 +00:00
func (i *InputModal) GetWidth() int { return i.width }
2015-10-21 16:51:22 +00:00
// SetWidth Set the width of the modal to width
2015-05-02 03:15:36 +00:00
func (i *InputModal) SetWidth(width int) *InputModal {
i.width = width
return i
}
2015-10-21 16:51:22 +00:00
// GetHeight Return the height of the modal
2015-05-02 03:15:36 +00:00
func (i *InputModal) GetHeight() int { return i.height }
2015-10-21 16:51:22 +00:00
// SetHeight Set the height of the modal to height
2015-05-02 03:15:36 +00:00
func (i *InputModal) SetHeight(height int) *InputModal {
i.height = height
return i
}
2015-10-21 16:51:22 +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
2015-05-02 03:15:36 +00:00
func (i *InputModal) ShowHelp(b bool) *InputModal {
2015-10-21 16:51:22 +00:00
i.showHelp = b
2015-05-02 03:15:36 +00:00
return i
}
2015-10-21 16:51:22 +00:00
// GetBackground Return the current background color of the modal
2015-05-02 03:15:36 +00:00
func (i *InputModal) GetBackground() termbox.Attribute { return i.bg }
2015-10-21 16:51:22 +00:00
// SetBackground Set the current background color to bg
2015-05-02 03:15:36 +00:00
func (i *InputModal) SetBackground(bg termbox.Attribute) *InputModal {
i.bg = bg
return i
}
2015-10-21 16:51:22 +00:00
// GetForeground Return the current foreground color
2015-05-02 03:15:36 +00:00
func (i *InputModal) GetForeground() termbox.Attribute { return i.fg }
2015-10-21 16:51:22 +00:00
// SetForeground Set the foreground color to fg
2015-05-02 03:15:36 +00:00
func (i *InputModal) SetForeground(fg termbox.Attribute) *InputModal {
i.fg = fg
return i
}
2015-05-05 20:47:09 +00:00
2015-10-21 16:51:22 +00:00
// Show Sets the visibility flag to true
2015-05-05 20:47:09 +00:00
func (i *InputModal) Show() *InputModal {
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 to false
2015-05-05 20:47:09 +00:00
func (i *InputModal) Hide() *InputModal {
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
// SetDone Sets the flag that tells whether this modal has completed it's purpose
2015-05-03 22:11:50 +00:00
func (i *InputModal) SetDone(b bool) *InputModal {
2015-10-21 16:51:22 +00:00
i.isDone = b
2015-05-03 22:11:50 +00:00
return i
}
2015-10-21 16:51:22 +00:00
// IsDone Returns the "isDone" flag
2015-05-03 22:11:50 +00:00
func (i *InputModal) IsDone() bool {
2015-10-21 16:51:22 +00:00
return i.isDone
2015-05-03 22:11:50 +00:00
}
2015-10-21 16:51:22 +00:00
// GetValue Return the current value of the input
func (i *InputModal) GetValue() string { return i.input.GetValue() }
2015-10-21 16:51:22 +00:00
// SetValue Sets the value of the input to s
2015-05-05 00:34:48 +00:00
func (i *InputModal) SetValue(s string) *InputModal {
i.input.SetValue(s)
return i
}
2016-01-01 18:15:09 +00:00
// SetInputWrap sets whether the input field will wrap long text or not
func (i *InputModal) SetInputWrap(b bool) *InputModal {
i.input.SetWrap(b)
return i
}
2015-10-21 16:51:22 +00:00
// Clear Resets all non-positional parameters of the modal
2015-05-03 22:11:50 +00:00
func (i *InputModal) Clear() *InputModal {
i.title = ""
i.text = ""
i.input.SetValue("")
2015-10-21 16:51:22 +00:00
i.isDone = false
i.isVisible = false
2015-05-03 22:11:50 +00:00
return i
}
2015-05-02 03:15:36 +00:00
2015-10-21 16:51:22 +00:00
// HandleKeyPress Handle the termbox event, return true if it was consumed
2015-05-02 03:15:36 +00:00
func (i *InputModal) HandleKeyPress(event termbox.Event) bool {
if event.Key == termbox.KeyEnter {
// Done editing
2015-10-21 16:51:22 +00:00
i.isDone = true
2015-05-03 22:11:50 +00:00
return true
2015-05-02 03:15:36 +00:00
}
2015-10-21 16:51:22 +00:00
return i.input.HandleKeyPress(event)
2015-05-02 03:15:36 +00:00
}
2015-05-04 22:42:35 +00:00
2015-10-21 16:51:22 +00:00
// Draw Draw the modal
2015-05-02 03:15:36 +00:00
func (i *InputModal) Draw() {
2015-10-21 16:51:22 +00:00
if i.isVisible {
2015-05-05 20:47:09 +00:00
// 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:51:22 +00:00
nextY := i.y + 1
2015-05-05 20:47:09 +00:00
// The title
if i.title != "" {
2015-05-18 13:50:16 +00:00
if len(i.title) > i.width {
diff := i.width - len(i.title)
2015-10-21 16:51:22 +00:00
DrawStringAtPoint(i.title[:len(i.title)+diff-1], i.x+1, nextY, i.fg, i.bg)
2015-05-18 13:50:16 +00:00
} else {
2015-10-21 16:51:22 +00:00
DrawStringAtPoint(i.title, i.x+1, nextY, i.fg, i.bg)
2015-05-18 13:50:16 +00:00
}
2015-10-21 16:51:22 +00:00
nextY++
FillWithChar('-', i.x+1, nextY, i.x+i.width-1, nextY, i.fg, i.bg)
nextY++
2015-05-05 20:47:09 +00:00
}
if i.text != "" {
2015-10-21 16:51:22 +00:00
DrawStringAtPoint(i.text, i.x+1, nextY, i.fg, i.bg)
nextY++
2015-05-05 20:47:09 +00:00
}
2015-10-21 16:51:22 +00:00
i.input.SetY(nextY)
2015-05-05 20:47:09 +00:00
i.input.Draw()
2015-10-21 16:51:22 +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)
2015-05-05 20:47:09 +00:00
}
2015-05-18 13:50:16 +00:00
// Now draw the border
DrawBorder(i.x, i.y, i.x+i.width, i.y+i.height, i.fg, i.bg)
}
2015-05-02 03:15:36 +00:00
}