2017-12-15 22:29:21 +08:00
|
|
|
package tview
|
|
|
|
|
|
|
|
import (
|
|
|
|
"math"
|
2017-12-16 06:03:01 +08:00
|
|
|
"regexp"
|
2018-01-11 23:13:01 +08:00
|
|
|
"strings"
|
|
|
|
"unicode/utf8"
|
2017-12-15 22:29:21 +08:00
|
|
|
|
|
|
|
"github.com/gdamore/tcell"
|
2018-01-11 22:45:52 +08:00
|
|
|
runewidth "github.com/mattn/go-runewidth"
|
2017-12-15 22:29:21 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// InputField is a one-line box (three lines if there is a title) where the
|
|
|
|
// user can enter text.
|
2018-01-07 23:39:06 +08:00
|
|
|
//
|
2018-01-11 23:13:01 +08:00
|
|
|
// Use SetMaskCharacter() to hide input from onlookers (e.g. for password
|
|
|
|
// input).
|
|
|
|
//
|
2018-01-07 23:39:06 +08:00
|
|
|
// See https://github.com/rivo/tview/wiki/InputField for an example.
|
2017-12-15 22:29:21 +08:00
|
|
|
type InputField struct {
|
2017-12-17 05:48:26 +08:00
|
|
|
*Box
|
2017-12-15 22:29:21 +08:00
|
|
|
|
|
|
|
// The text that was entered.
|
|
|
|
text string
|
|
|
|
|
|
|
|
// The text to be displayed before the input area.
|
|
|
|
label string
|
|
|
|
|
2018-03-16 00:14:14 +08:00
|
|
|
// The text to be displayed in the input area when "text" is empty.
|
|
|
|
placeholder string
|
|
|
|
|
2017-12-15 22:29:21 +08:00
|
|
|
// The label color.
|
|
|
|
labelColor tcell.Color
|
|
|
|
|
|
|
|
// The background color of the input area.
|
|
|
|
fieldBackgroundColor tcell.Color
|
|
|
|
|
|
|
|
// The text color of the input area.
|
|
|
|
fieldTextColor tcell.Color
|
|
|
|
|
2018-03-16 00:14:14 +08:00
|
|
|
// The text color of the placeholder.
|
|
|
|
placeholderTextColor tcell.Color
|
|
|
|
|
2018-04-10 03:07:03 +08:00
|
|
|
// The screen width of the label area. A value of 0 means use the width of
|
|
|
|
// the label text.
|
|
|
|
labelWidth int
|
|
|
|
|
2018-01-18 04:17:59 +08:00
|
|
|
// The screen width of the input area. A value of 0 means extend as much as
|
2017-12-15 22:29:21 +08:00
|
|
|
// possible.
|
2018-01-18 04:17:59 +08:00
|
|
|
fieldWidth int
|
2017-12-15 22:29:21 +08:00
|
|
|
|
2018-01-11 23:13:01 +08:00
|
|
|
// A character to mask entered text (useful for password fields). A value of 0
|
|
|
|
// disables masking.
|
|
|
|
maskCharacter rune
|
|
|
|
|
2017-12-15 22:29:21 +08:00
|
|
|
// An optional function which may reject the last character that was entered.
|
|
|
|
accept func(text string, ch rune) bool
|
|
|
|
|
2018-01-10 17:40:51 +08:00
|
|
|
// An optional function which is called when the input has changed.
|
|
|
|
changed func(text string)
|
|
|
|
|
2017-12-15 22:29:21 +08:00
|
|
|
// An optional function which is called when the user indicated that they
|
2017-12-16 06:03:01 +08:00
|
|
|
// are done entering text. The key which was pressed is provided (tab,
|
|
|
|
// shift-tab, enter, or escape).
|
2017-12-15 22:29:21 +08:00
|
|
|
done func(tcell.Key)
|
2018-04-20 03:34:03 +08:00
|
|
|
|
|
|
|
// A callback function set by the Form class and called when the user leaves
|
|
|
|
// this form item.
|
|
|
|
finished func(tcell.Key)
|
2017-12-15 22:29:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewInputField returns a new input field.
|
|
|
|
func NewInputField() *InputField {
|
|
|
|
return &InputField{
|
2017-12-17 05:48:26 +08:00
|
|
|
Box: NewBox(),
|
2018-01-10 16:43:32 +08:00
|
|
|
labelColor: Styles.SecondaryTextColor,
|
|
|
|
fieldBackgroundColor: Styles.ContrastBackgroundColor,
|
|
|
|
fieldTextColor: Styles.PrimaryTextColor,
|
2018-03-16 00:14:14 +08:00
|
|
|
placeholderTextColor: Styles.ContrastSecondaryTextColor,
|
2017-12-15 22:29:21 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetText sets the current text of the input field.
|
|
|
|
func (i *InputField) SetText(text string) *InputField {
|
|
|
|
i.text = text
|
2018-01-10 17:40:51 +08:00
|
|
|
if i.changed != nil {
|
|
|
|
i.changed(text)
|
|
|
|
}
|
2017-12-15 22:29:21 +08:00
|
|
|
return i
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetText returns the current text of the input field.
|
|
|
|
func (i *InputField) GetText() string {
|
|
|
|
return i.text
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetLabel sets the text to be displayed before the input area.
|
|
|
|
func (i *InputField) SetLabel(label string) *InputField {
|
|
|
|
i.label = label
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetLabel returns the text to be displayed before the input area.
|
|
|
|
func (i *InputField) GetLabel() string {
|
|
|
|
return i.label
|
|
|
|
}
|
|
|
|
|
2018-04-10 03:07:03 +08:00
|
|
|
// SetLabelWidth sets the screen width of the label. A value of 0 will cause the
|
|
|
|
// primitive to use the width of the label string.
|
|
|
|
func (i *InputField) SetLabelWidth(width int) *InputField {
|
|
|
|
i.labelWidth = width
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
|
2018-03-16 00:14:14 +08:00
|
|
|
// SetPlaceholder sets the text to be displayed when the input text is empty.
|
|
|
|
func (i *InputField) SetPlaceholder(text string) *InputField {
|
|
|
|
i.placeholder = text
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
|
2017-12-15 22:29:21 +08:00
|
|
|
// SetLabelColor sets the color of the label.
|
|
|
|
func (i *InputField) SetLabelColor(color tcell.Color) *InputField {
|
|
|
|
i.labelColor = color
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetFieldBackgroundColor sets the background color of the input area.
|
|
|
|
func (i *InputField) SetFieldBackgroundColor(color tcell.Color) *InputField {
|
|
|
|
i.fieldBackgroundColor = color
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetFieldTextColor sets the text color of the input area.
|
|
|
|
func (i *InputField) SetFieldTextColor(color tcell.Color) *InputField {
|
|
|
|
i.fieldTextColor = color
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
|
2018-04-10 03:07:03 +08:00
|
|
|
// SetPlaceholderTextColor sets the text color of placeholder text.
|
|
|
|
func (i *InputField) SetPlaceholderTextColor(color tcell.Color) *InputField {
|
2018-03-16 00:14:14 +08:00
|
|
|
i.placeholderTextColor = color
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
|
2017-12-19 03:04:52 +08:00
|
|
|
// SetFormAttributes sets attributes shared by all form items.
|
2018-04-10 03:07:03 +08:00
|
|
|
func (i *InputField) SetFormAttributes(labelWidth int, labelColor, bgColor, fieldTextColor, fieldBgColor tcell.Color) FormItem {
|
|
|
|
i.labelWidth = labelWidth
|
2017-12-19 03:04:52 +08:00
|
|
|
i.labelColor = labelColor
|
|
|
|
i.backgroundColor = bgColor
|
|
|
|
i.fieldTextColor = fieldTextColor
|
|
|
|
i.fieldBackgroundColor = fieldBgColor
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
|
2018-01-18 04:17:59 +08:00
|
|
|
// SetFieldWidth sets the screen width of the input area. A value of 0 means
|
|
|
|
// extend as much as possible.
|
|
|
|
func (i *InputField) SetFieldWidth(width int) *InputField {
|
|
|
|
i.fieldWidth = width
|
2017-12-15 22:29:21 +08:00
|
|
|
return i
|
|
|
|
}
|
|
|
|
|
2018-01-18 04:17:59 +08:00
|
|
|
// GetFieldWidth returns this primitive's field width.
|
|
|
|
func (i *InputField) GetFieldWidth() int {
|
|
|
|
return i.fieldWidth
|
2018-01-17 03:45:54 +08:00
|
|
|
}
|
|
|
|
|
2018-01-11 23:13:01 +08:00
|
|
|
// SetMaskCharacter sets a character that masks user input on a screen. A value
|
|
|
|
// of 0 disables masking.
|
|
|
|
func (i *InputField) SetMaskCharacter(mask rune) *InputField {
|
|
|
|
i.maskCharacter = mask
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
|
2017-12-15 22:29:21 +08:00
|
|
|
// SetAcceptanceFunc sets a handler which may reject the last character that was
|
|
|
|
// entered (by returning false).
|
|
|
|
//
|
|
|
|
// This package defines a number of variables Prefixed with InputField which may
|
|
|
|
// be used for common input (e.g. numbers, maximum text length).
|
|
|
|
func (i *InputField) SetAcceptanceFunc(handler func(textToCheck string, lastChar rune) bool) *InputField {
|
|
|
|
i.accept = handler
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
|
2018-01-10 17:40:51 +08:00
|
|
|
// SetChangedFunc sets a handler which is called whenever the text of the input
|
|
|
|
// field has changed. It receives the current text (after the change).
|
|
|
|
func (i *InputField) SetChangedFunc(handler func(text string)) *InputField {
|
|
|
|
i.changed = handler
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
|
2017-12-15 22:29:21 +08:00
|
|
|
// SetDoneFunc sets a handler which is called when the user is done entering
|
|
|
|
// text. The callback function is provided with the key that was pressed, which
|
|
|
|
// is one of the following:
|
|
|
|
//
|
|
|
|
// - KeyEnter: Done entering text.
|
|
|
|
// - KeyEscape: Abort text input.
|
|
|
|
// - KeyTab: Move to the next field.
|
2017-12-16 06:03:01 +08:00
|
|
|
// - KeyBacktab: Move to the previous field.
|
2017-12-15 22:29:21 +08:00
|
|
|
func (i *InputField) SetDoneFunc(handler func(key tcell.Key)) *InputField {
|
|
|
|
i.done = handler
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
|
2018-04-20 03:34:03 +08:00
|
|
|
// SetFinishedFunc sets a callback invoked when the user leaves this form item.
|
2017-12-19 03:04:52 +08:00
|
|
|
func (i *InputField) SetFinishedFunc(handler func(key tcell.Key)) FormItem {
|
2018-04-20 03:34:03 +08:00
|
|
|
i.finished = handler
|
|
|
|
return i
|
2017-12-19 03:04:52 +08:00
|
|
|
}
|
|
|
|
|
2017-12-15 22:29:21 +08:00
|
|
|
// Draw draws this primitive onto the screen.
|
|
|
|
func (i *InputField) Draw(screen tcell.Screen) {
|
|
|
|
i.Box.Draw(screen)
|
|
|
|
|
|
|
|
// Prepare
|
2017-12-22 01:08:53 +08:00
|
|
|
x, y, width, height := i.GetInnerRect()
|
|
|
|
rightLimit := x + width
|
2017-12-15 22:29:21 +08:00
|
|
|
if height < 1 || rightLimit <= x {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Draw label.
|
2018-04-10 03:07:03 +08:00
|
|
|
if i.labelWidth > 0 {
|
|
|
|
labelWidth := i.labelWidth
|
|
|
|
if labelWidth > rightLimit-x {
|
|
|
|
labelWidth = rightLimit - x
|
|
|
|
}
|
|
|
|
Print(screen, i.label, x, y, labelWidth, AlignLeft, i.labelColor)
|
|
|
|
x += labelWidth
|
|
|
|
} else {
|
|
|
|
_, drawnWidth := Print(screen, i.label, x, y, rightLimit-x, AlignLeft, i.labelColor)
|
|
|
|
x += drawnWidth
|
|
|
|
}
|
2017-12-15 22:29:21 +08:00
|
|
|
|
|
|
|
// Draw input area.
|
2018-01-18 04:17:59 +08:00
|
|
|
fieldWidth := i.fieldWidth
|
2018-01-18 00:13:36 +08:00
|
|
|
if fieldWidth == 0 {
|
|
|
|
fieldWidth = math.MaxInt32
|
2017-12-15 22:29:21 +08:00
|
|
|
}
|
2018-01-18 00:13:36 +08:00
|
|
|
if rightLimit-x < fieldWidth {
|
|
|
|
fieldWidth = rightLimit - x
|
2017-12-15 22:29:21 +08:00
|
|
|
}
|
2017-12-16 06:03:01 +08:00
|
|
|
fieldStyle := tcell.StyleDefault.Background(i.fieldBackgroundColor)
|
2018-01-18 00:13:36 +08:00
|
|
|
for index := 0; index < fieldWidth; index++ {
|
2017-12-16 06:03:01 +08:00
|
|
|
screen.SetContent(x+index, y, ' ', nil, fieldStyle)
|
2017-12-15 22:29:21 +08:00
|
|
|
}
|
2017-12-16 06:03:01 +08:00
|
|
|
|
2018-03-16 00:14:14 +08:00
|
|
|
// Draw placeholder text.
|
2018-01-11 23:13:01 +08:00
|
|
|
text := i.text
|
2018-03-16 00:14:14 +08:00
|
|
|
if text == "" && i.placeholder != "" {
|
|
|
|
Print(screen, i.placeholder, x, y, fieldWidth, AlignLeft, i.placeholderTextColor)
|
2017-12-16 06:03:01 +08:00
|
|
|
} else {
|
2018-05-03 14:05:11 +08:00
|
|
|
// Draw entered text.
|
|
|
|
if i.maskCharacter > 0 {
|
|
|
|
text = strings.Repeat(string(i.maskCharacter), utf8.RuneCountInString(i.text))
|
|
|
|
} else {
|
|
|
|
text = Escape(text)
|
|
|
|
}
|
|
|
|
fieldWidth-- // We need one cell for the cursor.
|
|
|
|
if fieldWidth < runewidth.StringWidth(text) {
|
|
|
|
Print(screen, text, x, y, fieldWidth, AlignRight, i.fieldTextColor)
|
|
|
|
} else {
|
|
|
|
Print(screen, text, x, y, fieldWidth, AlignLeft, i.fieldTextColor)
|
2018-01-18 00:13:36 +08:00
|
|
|
}
|
2017-12-15 22:29:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set cursor.
|
2017-12-17 05:48:26 +08:00
|
|
|
if i.focus.HasFocus() {
|
2017-12-15 22:29:21 +08:00
|
|
|
i.setCursor(screen)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// setCursor sets the cursor position.
|
|
|
|
func (i *InputField) setCursor(screen tcell.Screen) {
|
|
|
|
x := i.x
|
|
|
|
y := i.y
|
|
|
|
rightLimit := x + i.width
|
|
|
|
if i.border {
|
|
|
|
x++
|
|
|
|
y++
|
|
|
|
rightLimit -= 2
|
|
|
|
}
|
2018-01-18 04:17:59 +08:00
|
|
|
fieldWidth := runewidth.StringWidth(i.text)
|
|
|
|
if i.fieldWidth > 0 && fieldWidth > i.fieldWidth-1 {
|
|
|
|
fieldWidth = i.fieldWidth - 1
|
2017-12-15 22:29:21 +08:00
|
|
|
}
|
2018-04-10 03:07:03 +08:00
|
|
|
if i.labelWidth > 0 {
|
|
|
|
x += i.labelWidth + fieldWidth
|
|
|
|
} else {
|
|
|
|
x += StringWidth(i.label) + fieldWidth
|
|
|
|
}
|
2017-12-15 22:29:21 +08:00
|
|
|
if x >= rightLimit {
|
|
|
|
x = rightLimit - 1
|
|
|
|
}
|
|
|
|
screen.ShowCursor(x, y)
|
|
|
|
}
|
|
|
|
|
|
|
|
// InputHandler returns the handler for this primitive.
|
2017-12-19 03:04:52 +08:00
|
|
|
func (i *InputField) InputHandler() func(event *tcell.EventKey, setFocus func(p Primitive)) {
|
2018-03-20 04:25:30 +08:00
|
|
|
return i.WrapInputHandler(func(event *tcell.EventKey, setFocus func(p Primitive)) {
|
2018-01-10 17:40:51 +08:00
|
|
|
// Trigger changed events.
|
|
|
|
currentText := i.text
|
|
|
|
defer func() {
|
|
|
|
if i.text != currentText && i.changed != nil {
|
|
|
|
i.changed(i.text)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2017-12-15 22:29:21 +08:00
|
|
|
// Process key event.
|
|
|
|
switch key := event.Key(); key {
|
|
|
|
case tcell.KeyRune: // Regular character.
|
|
|
|
newText := i.text + string(event.Rune())
|
|
|
|
if i.accept != nil {
|
|
|
|
if !i.accept(newText, event.Rune()) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
i.text = newText
|
|
|
|
case tcell.KeyCtrlU: // Delete all.
|
|
|
|
i.text = ""
|
2017-12-16 06:03:01 +08:00
|
|
|
case tcell.KeyCtrlW: // Delete last word.
|
|
|
|
lastWord := regexp.MustCompile(`\s*\S+\s*$`)
|
|
|
|
i.text = lastWord.ReplaceAllString(i.text, "")
|
2017-12-15 22:29:21 +08:00
|
|
|
case tcell.KeyBackspace, tcell.KeyBackspace2: // Delete last character.
|
2018-01-11 22:45:52 +08:00
|
|
|
if len(i.text) == 0 {
|
2017-12-15 22:29:21 +08:00
|
|
|
break
|
|
|
|
}
|
2017-12-26 08:07:30 +08:00
|
|
|
runes := []rune(i.text)
|
|
|
|
i.text = string(runes[:len(runes)-1])
|
2017-12-16 06:03:01 +08:00
|
|
|
case tcell.KeyEnter, tcell.KeyTab, tcell.KeyBacktab, tcell.KeyEscape: // We're done.
|
2017-12-15 22:29:21 +08:00
|
|
|
if i.done != nil {
|
|
|
|
i.done(key)
|
|
|
|
}
|
2018-04-20 03:34:03 +08:00
|
|
|
if i.finished != nil {
|
|
|
|
i.finished(key)
|
|
|
|
}
|
2017-12-15 22:29:21 +08:00
|
|
|
}
|
2018-01-15 04:29:34 +08:00
|
|
|
})
|
2017-12-15 22:29:21 +08:00
|
|
|
}
|