2017-12-15 14:29:21 +00:00
|
|
|
package tview
|
|
|
|
|
|
|
|
import (
|
|
|
|
"math"
|
2017-12-15 22:03:01 +00:00
|
|
|
"regexp"
|
2018-01-11 15:13:01 +00:00
|
|
|
"strings"
|
2019-07-08 08:34:06 +00:00
|
|
|
"sync"
|
2018-01-11 15:13:01 +00:00
|
|
|
"unicode/utf8"
|
2017-12-15 14:29:21 +00:00
|
|
|
|
2020-10-18 12:15:57 +00:00
|
|
|
"github.com/gdamore/tcell/v2"
|
2017-12-15 14:29:21 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// InputField is a one-line box (three lines if there is a title) where the
|
2018-10-18 06:34:08 +00:00
|
|
|
// user can enter text. Use SetAcceptanceFunc() to accept or reject input,
|
|
|
|
// SetChangedFunc() to listen for changes, and SetMaskCharacter() to hide input
|
|
|
|
// from onlookers (e.g. for password input).
|
2018-01-07 15:39:06 +00:00
|
|
|
//
|
2018-10-18 06:34:08 +00:00
|
|
|
// The following keys can be used for navigation and editing:
|
|
|
|
//
|
|
|
|
// - Left arrow: Move left by one character.
|
|
|
|
// - Right arrow: Move right by one character.
|
|
|
|
// - Home, Ctrl-A, Alt-a: Move to the beginning of the line.
|
|
|
|
// - End, Ctrl-E, Alt-e: Move to the end of the line.
|
|
|
|
// - Alt-left, Alt-b: Move left by one word.
|
|
|
|
// - Alt-right, Alt-f: Move right by one word.
|
|
|
|
// - Backspace: Delete the character before the cursor.
|
|
|
|
// - Delete: Delete the character after the cursor.
|
|
|
|
// - Ctrl-K: Delete from the cursor to the end of the line.
|
|
|
|
// - Ctrl-W: Delete the last word before the cursor.
|
|
|
|
// - Ctrl-U: Delete the entire line.
|
2018-01-11 15:13:01 +00:00
|
|
|
//
|
2018-01-07 15:39:06 +00:00
|
|
|
// See https://github.com/rivo/tview/wiki/InputField for an example.
|
2017-12-15 14:29:21 +00:00
|
|
|
type InputField struct {
|
2017-12-16 21:48:26 +00:00
|
|
|
*Box
|
2017-12-15 14:29:21 +00:00
|
|
|
|
|
|
|
// The text that was entered.
|
|
|
|
text string
|
|
|
|
|
|
|
|
// The text to be displayed before the input area.
|
|
|
|
label string
|
|
|
|
|
2018-03-15 16:14:14 +00:00
|
|
|
// The text to be displayed in the input area when "text" is empty.
|
|
|
|
placeholder string
|
|
|
|
|
2021-11-09 14:42:28 +00:00
|
|
|
// The label style.
|
|
|
|
labelStyle tcell.Style
|
2017-12-15 14:29:21 +00:00
|
|
|
|
2021-11-09 14:42:28 +00:00
|
|
|
// The style of the input area with input text.
|
|
|
|
fieldStyle tcell.Style
|
2017-12-15 14:29:21 +00:00
|
|
|
|
2021-11-09 14:42:28 +00:00
|
|
|
// The style of the input area with placeholder text.
|
|
|
|
placeholderStyle tcell.Style
|
2018-03-15 16:14:14 +00:00
|
|
|
|
2018-04-09 19:07:03 +00:00
|
|
|
// The screen width of the label area. A value of 0 means use the width of
|
|
|
|
// the label text.
|
|
|
|
labelWidth int
|
|
|
|
|
2018-01-17 20:17:59 +00:00
|
|
|
// The screen width of the input area. A value of 0 means extend as much as
|
2017-12-15 14:29:21 +00:00
|
|
|
// possible.
|
2018-01-17 20:17:59 +00:00
|
|
|
fieldWidth int
|
2017-12-15 14:29:21 +00:00
|
|
|
|
2018-01-11 15:13:01 +00:00
|
|
|
// A character to mask entered text (useful for password fields). A value of 0
|
|
|
|
// disables masking.
|
|
|
|
maskCharacter rune
|
|
|
|
|
2018-10-18 06:34:08 +00:00
|
|
|
// The cursor position as a byte index into the text string.
|
|
|
|
cursorPos int
|
|
|
|
|
2019-07-08 08:34:06 +00:00
|
|
|
// An optional autocomplete function which receives the current text of the
|
|
|
|
// input field and returns a slice of strings to be displayed in a drop-down
|
|
|
|
// selection.
|
|
|
|
autocomplete func(text string) []string
|
|
|
|
|
|
|
|
// The List object which shows the selectable autocomplete entries. If not
|
|
|
|
// nil, the list's main texts represent the current autocomplete entries.
|
|
|
|
autocompleteList *List
|
|
|
|
autocompleteListMutex sync.Mutex
|
|
|
|
|
2022-02-15 17:00:49 +00:00
|
|
|
// The styles of the autocomplete entries.
|
|
|
|
autocompleteStyles struct {
|
|
|
|
main tcell.Style
|
|
|
|
selected tcell.Style
|
|
|
|
background tcell.Color
|
|
|
|
}
|
|
|
|
|
2017-12-15 14:29:21 +00:00
|
|
|
// An optional function which may reject the last character that was entered.
|
|
|
|
accept func(text string, ch rune) bool
|
|
|
|
|
2018-01-10 09:40:51 +00:00
|
|
|
// An optional function which is called when the input has changed.
|
|
|
|
changed func(text string)
|
|
|
|
|
2017-12-15 14:29:21 +00:00
|
|
|
// An optional function which is called when the user indicated that they
|
2017-12-15 22:03:01 +00:00
|
|
|
// are done entering text. The key which was pressed is provided (tab,
|
|
|
|
// shift-tab, enter, or escape).
|
2017-12-15 14:29:21 +00:00
|
|
|
done func(tcell.Key)
|
2018-04-19 19:34:03 +00:00
|
|
|
|
|
|
|
// A callback function set by the Form class and called when the user leaves
|
|
|
|
// this form item.
|
|
|
|
finished func(tcell.Key)
|
2020-03-27 17:41:44 +00:00
|
|
|
|
|
|
|
fieldX int // The x-coordinate of the input field as determined during the last call to Draw().
|
|
|
|
offset int // The number of bytes of the text string skipped ahead while drawing.
|
2017-12-15 14:29:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewInputField returns a new input field.
|
|
|
|
func NewInputField() *InputField {
|
2022-02-15 17:00:49 +00:00
|
|
|
i := &InputField{
|
2021-11-09 14:42:28 +00:00
|
|
|
Box: NewBox(),
|
|
|
|
labelStyle: tcell.StyleDefault.Foreground(Styles.SecondaryTextColor),
|
|
|
|
fieldStyle: tcell.StyleDefault.Background(Styles.ContrastBackgroundColor).Foreground(Styles.PrimaryTextColor),
|
|
|
|
placeholderStyle: tcell.StyleDefault.Background(Styles.ContrastBackgroundColor).Foreground(Styles.ContrastSecondaryTextColor),
|
2017-12-15 14:29:21 +00:00
|
|
|
}
|
2022-02-15 17:00:49 +00:00
|
|
|
i.autocompleteStyles.main = tcell.StyleDefault.Foreground(Styles.PrimitiveBackgroundColor)
|
|
|
|
i.autocompleteStyles.selected = tcell.StyleDefault.Background(Styles.PrimaryTextColor).Foreground(Styles.PrimitiveBackgroundColor)
|
|
|
|
i.autocompleteStyles.background = Styles.MoreContrastBackgroundColor
|
|
|
|
return i
|
2017-12-15 14:29:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetText sets the current text of the input field.
|
|
|
|
func (i *InputField) SetText(text string) *InputField {
|
|
|
|
i.text = text
|
2018-11-10 08:17:39 +00:00
|
|
|
i.cursorPos = len(text)
|
2018-01-10 09:40:51 +00:00
|
|
|
if i.changed != nil {
|
|
|
|
i.changed(text)
|
|
|
|
}
|
2017-12-15 14:29:21 +00: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-09 19:07:03 +00: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-15 16:14:14 +00: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
|
|
|
|
}
|
|
|
|
|
2021-11-09 14:42:28 +00:00
|
|
|
// SetLabelColor sets the text color of the label.
|
2017-12-15 14:29:21 +00:00
|
|
|
func (i *InputField) SetLabelColor(color tcell.Color) *InputField {
|
2021-11-09 14:42:28 +00:00
|
|
|
i.labelStyle = i.labelStyle.Foreground(color)
|
2017-12-15 14:29:21 +00:00
|
|
|
return i
|
|
|
|
}
|
|
|
|
|
2021-11-09 14:42:28 +00:00
|
|
|
// SetLabelStyle sets the style of the label.
|
|
|
|
func (i *InputField) SetLabelStyle(style tcell.Style) *InputField {
|
|
|
|
i.labelStyle = style
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetLabelStyle returns the style of the label.
|
|
|
|
func (i *InputField) GetLabelStyle() tcell.Style {
|
|
|
|
return i.labelStyle
|
|
|
|
}
|
|
|
|
|
2017-12-15 14:29:21 +00:00
|
|
|
// SetFieldBackgroundColor sets the background color of the input area.
|
|
|
|
func (i *InputField) SetFieldBackgroundColor(color tcell.Color) *InputField {
|
2021-11-09 14:42:28 +00:00
|
|
|
i.fieldStyle = i.fieldStyle.Background(color)
|
2017-12-15 14:29:21 +00:00
|
|
|
return i
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetFieldTextColor sets the text color of the input area.
|
|
|
|
func (i *InputField) SetFieldTextColor(color tcell.Color) *InputField {
|
2021-11-09 14:42:28 +00:00
|
|
|
i.fieldStyle = i.fieldStyle.Foreground(color)
|
2017-12-15 14:29:21 +00:00
|
|
|
return i
|
|
|
|
}
|
|
|
|
|
2021-11-09 14:42:28 +00:00
|
|
|
// SetFieldStyle sets the style of the input area (when no placeholder is
|
|
|
|
// shown).
|
|
|
|
func (i *InputField) SetFieldStyle(style tcell.Style) *InputField {
|
|
|
|
i.fieldStyle = style
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetFieldStyle returns the style of the input area (when no placeholder is
|
|
|
|
// shown).
|
|
|
|
func (i *InputField) GetFieldStyle() tcell.Style {
|
|
|
|
return i.fieldStyle
|
|
|
|
}
|
|
|
|
|
2018-04-09 19:07:03 +00:00
|
|
|
// SetPlaceholderTextColor sets the text color of placeholder text.
|
|
|
|
func (i *InputField) SetPlaceholderTextColor(color tcell.Color) *InputField {
|
2021-11-09 14:42:28 +00:00
|
|
|
i.placeholderStyle = i.placeholderStyle.Foreground(color)
|
2018-03-15 16:14:14 +00:00
|
|
|
return i
|
|
|
|
}
|
|
|
|
|
2021-11-09 14:42:28 +00:00
|
|
|
// SetPlaceholderStyle sets the style of the input area (when a placeholder is
|
|
|
|
// shown).
|
|
|
|
func (i *InputField) SetPlaceholderStyle(style tcell.Style) *InputField {
|
|
|
|
i.placeholderStyle = style
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
|
2021-11-09 16:26:30 +00:00
|
|
|
// GetPlaceholderStyle returns the style of the input area (when a placeholder
|
2021-11-09 14:42:28 +00:00
|
|
|
// is shown).
|
|
|
|
func (i *InputField) GetPlaceholderStyle() tcell.Style {
|
|
|
|
return i.placeholderStyle
|
|
|
|
}
|
|
|
|
|
2022-02-15 17:00:49 +00:00
|
|
|
// SetAutocompleteStyles sets the colors and style of the autocomplete entries.
|
|
|
|
// For details, see List.SetMainTextStyle(), List.SetSelectedStyle(), and
|
|
|
|
// Box.SetBackgroundColor().
|
|
|
|
func (i *InputField) SetAutocompleteStyles(background tcell.Color, main, selected tcell.Style) *InputField {
|
|
|
|
i.autocompleteStyles.background = background
|
|
|
|
i.autocompleteStyles.main = main
|
|
|
|
i.autocompleteStyles.selected = selected
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
|
2017-12-18 19:04:52 +00:00
|
|
|
// SetFormAttributes sets attributes shared by all form items.
|
2018-04-09 19:07:03 +00:00
|
|
|
func (i *InputField) SetFormAttributes(labelWidth int, labelColor, bgColor, fieldTextColor, fieldBgColor tcell.Color) FormItem {
|
|
|
|
i.labelWidth = labelWidth
|
2017-12-18 19:04:52 +00:00
|
|
|
i.backgroundColor = bgColor
|
2021-11-09 14:42:28 +00:00
|
|
|
i.SetLabelColor(labelColor).
|
|
|
|
SetFieldTextColor(fieldTextColor).
|
|
|
|
SetFieldBackgroundColor(fieldBgColor)
|
2017-12-18 19:04:52 +00:00
|
|
|
return i
|
|
|
|
}
|
|
|
|
|
2018-01-17 20:17:59 +00: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 14:29:21 +00:00
|
|
|
return i
|
|
|
|
}
|
|
|
|
|
2018-01-17 20:17:59 +00:00
|
|
|
// GetFieldWidth returns this primitive's field width.
|
|
|
|
func (i *InputField) GetFieldWidth() int {
|
|
|
|
return i.fieldWidth
|
2018-01-16 19:45:54 +00:00
|
|
|
}
|
|
|
|
|
2018-01-11 15:13:01 +00: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
|
|
|
|
}
|
|
|
|
|
2019-07-08 08:34:06 +00:00
|
|
|
// SetAutocompleteFunc sets an autocomplete callback function which may return
|
|
|
|
// strings to be selected from a drop-down based on the current text of the
|
|
|
|
// input field. The drop-down appears only if len(entries) > 0. The callback is
|
|
|
|
// invoked in this function and whenever the current text changes or when
|
|
|
|
// Autocomplete() is called. Entries are cleared when the user selects an entry
|
|
|
|
// or presses Escape.
|
|
|
|
func (i *InputField) SetAutocompleteFunc(callback func(currentText string) (entries []string)) *InputField {
|
|
|
|
i.autocomplete = callback
|
|
|
|
i.Autocomplete()
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
|
|
|
|
// Autocomplete invokes the autocomplete callback (if there is one). If the
|
|
|
|
// length of the returned autocomplete entries slice is greater than 0, the
|
|
|
|
// input field will present the user with a corresponding drop-down list the
|
|
|
|
// next time the input field is drawn.
|
|
|
|
//
|
|
|
|
// It is safe to call this function from any goroutine. Note that the input
|
|
|
|
// field is not redrawn automatically unless called from the main goroutine
|
|
|
|
// (e.g. in response to events).
|
|
|
|
func (i *InputField) Autocomplete() *InputField {
|
|
|
|
i.autocompleteListMutex.Lock()
|
|
|
|
defer i.autocompleteListMutex.Unlock()
|
|
|
|
if i.autocomplete == nil {
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
|
|
|
|
// Do we have any autocomplete entries?
|
|
|
|
entries := i.autocomplete(i.text)
|
|
|
|
if len(entries) == 0 {
|
|
|
|
// No entries, no list.
|
|
|
|
i.autocompleteList = nil
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make a list if we have none.
|
|
|
|
if i.autocompleteList == nil {
|
|
|
|
i.autocompleteList = NewList()
|
|
|
|
i.autocompleteList.ShowSecondaryText(false).
|
2022-02-15 17:00:49 +00:00
|
|
|
SetMainTextStyle(i.autocompleteStyles.main).
|
|
|
|
SetSelectedStyle(i.autocompleteStyles.selected).
|
2019-07-08 08:34:06 +00:00
|
|
|
SetHighlightFullLine(true).
|
2022-02-15 17:00:49 +00:00
|
|
|
SetBackgroundColor(i.autocompleteStyles.background)
|
2019-07-08 08:34:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Fill it with the entries.
|
|
|
|
currentEntry := -1
|
2021-01-11 18:45:19 +00:00
|
|
|
suffixLength := 9999 // I'm just waiting for the day somebody opens an issue with this number being too small.
|
2019-07-08 08:34:06 +00:00
|
|
|
i.autocompleteList.Clear()
|
|
|
|
for index, entry := range entries {
|
|
|
|
i.autocompleteList.AddItem(entry, "", 0, nil)
|
2021-01-11 18:45:19 +00:00
|
|
|
if strings.HasPrefix(entry, i.text) && len(entry)-len(i.text) < suffixLength {
|
2019-07-08 08:34:06 +00:00
|
|
|
currentEntry = index
|
2021-01-11 18:45:19 +00:00
|
|
|
suffixLength = len(i.text) - len(entry)
|
2019-07-08 08:34:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the selection if we have one.
|
|
|
|
if currentEntry >= 0 {
|
|
|
|
i.autocompleteList.SetCurrentItem(currentEntry)
|
|
|
|
}
|
|
|
|
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
|
2017-12-15 14:29:21 +00:00
|
|
|
// SetAcceptanceFunc sets a handler which may reject the last character that was
|
|
|
|
// entered (by returning false).
|
|
|
|
//
|
2018-10-18 06:34:08 +00:00
|
|
|
// This package defines a number of variables prefixed with InputField which may
|
2017-12-15 14:29:21 +00:00
|
|
|
// 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 09:40:51 +00: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 14:29:21 +00: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-15 22:03:01 +00:00
|
|
|
// - KeyBacktab: Move to the previous field.
|
2017-12-15 14:29:21 +00:00
|
|
|
func (i *InputField) SetDoneFunc(handler func(key tcell.Key)) *InputField {
|
|
|
|
i.done = handler
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
|
2018-04-19 19:34:03 +00:00
|
|
|
// SetFinishedFunc sets a callback invoked when the user leaves this form item.
|
2017-12-18 19:04:52 +00:00
|
|
|
func (i *InputField) SetFinishedFunc(handler func(key tcell.Key)) FormItem {
|
2018-04-19 19:34:03 +00:00
|
|
|
i.finished = handler
|
|
|
|
return i
|
2017-12-18 19:04:52 +00:00
|
|
|
}
|
|
|
|
|
2017-12-15 14:29:21 +00:00
|
|
|
// Draw draws this primitive onto the screen.
|
|
|
|
func (i *InputField) Draw(screen tcell.Screen) {
|
2020-11-17 18:33:25 +00:00
|
|
|
i.Box.DrawForSubclass(screen, i)
|
2017-12-15 14:29:21 +00:00
|
|
|
|
|
|
|
// Prepare
|
2017-12-21 17:08:53 +00:00
|
|
|
x, y, width, height := i.GetInnerRect()
|
|
|
|
rightLimit := x + width
|
2017-12-15 14:29:21 +00:00
|
|
|
if height < 1 || rightLimit <= x {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Draw label.
|
2021-11-09 17:56:15 +00:00
|
|
|
_, labelBg, _ := i.labelStyle.Decompose()
|
2018-04-09 19:07:03 +00:00
|
|
|
if i.labelWidth > 0 {
|
|
|
|
labelWidth := i.labelWidth
|
|
|
|
if labelWidth > rightLimit-x {
|
|
|
|
labelWidth = rightLimit - x
|
|
|
|
}
|
2021-11-09 17:56:15 +00:00
|
|
|
printWithStyle(screen, i.label, x, y, 0, labelWidth, AlignLeft, i.labelStyle, labelBg == tcell.ColorDefault)
|
2018-04-09 19:07:03 +00:00
|
|
|
x += labelWidth
|
|
|
|
} else {
|
2021-11-09 17:56:15 +00:00
|
|
|
_, drawnWidth, _, _ := printWithStyle(screen, i.label, x, y, 0, rightLimit-x, AlignLeft, i.labelStyle, labelBg == tcell.ColorDefault)
|
2018-04-09 19:07:03 +00:00
|
|
|
x += drawnWidth
|
|
|
|
}
|
2017-12-15 14:29:21 +00:00
|
|
|
|
|
|
|
// Draw input area.
|
2020-03-27 17:41:44 +00:00
|
|
|
i.fieldX = x
|
2018-01-17 20:17:59 +00:00
|
|
|
fieldWidth := i.fieldWidth
|
2021-11-09 14:42:28 +00:00
|
|
|
text := i.text
|
2021-11-09 17:56:15 +00:00
|
|
|
inputStyle := i.fieldStyle
|
2021-11-09 14:42:28 +00:00
|
|
|
placeholder := text == "" && i.placeholder != ""
|
2021-11-09 17:56:15 +00:00
|
|
|
if placeholder {
|
|
|
|
inputStyle = i.placeholderStyle
|
|
|
|
}
|
|
|
|
_, inputBg, _ := inputStyle.Decompose()
|
2018-01-17 16:13:36 +00:00
|
|
|
if fieldWidth == 0 {
|
|
|
|
fieldWidth = math.MaxInt32
|
2017-12-15 14:29:21 +00:00
|
|
|
}
|
2018-01-17 16:13:36 +00:00
|
|
|
if rightLimit-x < fieldWidth {
|
|
|
|
fieldWidth = rightLimit - x
|
2017-12-15 14:29:21 +00:00
|
|
|
}
|
2021-11-09 17:56:15 +00:00
|
|
|
if inputBg != tcell.ColorDefault {
|
|
|
|
for index := 0; index < fieldWidth; index++ {
|
|
|
|
screen.SetContent(x+index, y, ' ', nil, inputStyle)
|
2021-11-09 14:42:28 +00:00
|
|
|
}
|
2017-12-15 14:29:21 +00:00
|
|
|
}
|
2017-12-15 22:03:01 +00:00
|
|
|
|
2018-10-18 06:34:08 +00:00
|
|
|
// Text.
|
|
|
|
var cursorScreenPos int
|
2021-11-09 14:42:28 +00:00
|
|
|
if placeholder {
|
2018-10-18 06:34:08 +00:00
|
|
|
// Draw placeholder text.
|
2021-11-09 17:56:15 +00:00
|
|
|
printWithStyle(screen, Escape(i.placeholder), x, y, 0, fieldWidth, AlignLeft, i.placeholderStyle, true)
|
2018-10-18 06:34:08 +00:00
|
|
|
i.offset = 0
|
2017-12-15 22:03:01 +00:00
|
|
|
} else {
|
2018-05-03 06:05:11 +00:00
|
|
|
// Draw entered text.
|
|
|
|
if i.maskCharacter > 0 {
|
|
|
|
text = strings.Repeat(string(i.maskCharacter), utf8.RuneCountInString(i.text))
|
|
|
|
}
|
2019-03-19 11:12:40 +00:00
|
|
|
if fieldWidth >= stringWidth(text) {
|
2018-10-18 06:34:08 +00:00
|
|
|
// We have enough space for the full text.
|
2021-11-09 17:56:15 +00:00
|
|
|
printWithStyle(screen, Escape(text), x, y, 0, fieldWidth, AlignLeft, i.fieldStyle, true)
|
2018-10-18 06:34:08 +00:00
|
|
|
i.offset = 0
|
2022-07-30 14:59:31 +00:00
|
|
|
iterateString(text, func(main rune, comb []rune, textPos, textWidth, screenPos, screenWidth, boundaries int) bool {
|
2018-10-18 06:34:08 +00:00
|
|
|
if textPos >= i.cursorPos {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
cursorScreenPos += screenWidth
|
|
|
|
return false
|
|
|
|
})
|
2018-05-03 06:05:11 +00:00
|
|
|
} else {
|
2018-10-18 06:34:08 +00:00
|
|
|
// The text doesn't fit. Where is the cursor?
|
|
|
|
if i.cursorPos < 0 {
|
|
|
|
i.cursorPos = 0
|
|
|
|
} else if i.cursorPos > len(text) {
|
|
|
|
i.cursorPos = len(text)
|
|
|
|
}
|
|
|
|
// Shift the text so the cursor is inside the field.
|
|
|
|
var shiftLeft int
|
|
|
|
if i.offset > i.cursorPos {
|
|
|
|
i.offset = i.cursorPos
|
2019-03-19 11:12:40 +00:00
|
|
|
} else if subWidth := stringWidth(text[i.offset:i.cursorPos]); subWidth > fieldWidth-1 {
|
2018-10-18 06:34:08 +00:00
|
|
|
shiftLeft = subWidth - fieldWidth + 1
|
|
|
|
}
|
|
|
|
currentOffset := i.offset
|
2022-07-30 14:59:31 +00:00
|
|
|
iterateString(text, func(main rune, comb []rune, textPos, textWidth, screenPos, screenWidth, boundaries int) bool {
|
2018-10-18 06:34:08 +00:00
|
|
|
if textPos >= currentOffset {
|
|
|
|
if shiftLeft > 0 {
|
|
|
|
i.offset = textPos + textWidth
|
|
|
|
shiftLeft -= screenWidth
|
|
|
|
} else {
|
|
|
|
if textPos+textWidth > i.cursorPos {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
cursorScreenPos += screenWidth
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
})
|
2021-11-09 17:56:15 +00:00
|
|
|
printWithStyle(screen, Escape(text[i.offset:]), x, y, 0, fieldWidth, AlignLeft, i.fieldStyle, true)
|
2018-01-17 16:13:36 +00:00
|
|
|
}
|
2017-12-15 14:29:21 +00:00
|
|
|
}
|
|
|
|
|
2019-07-08 08:34:06 +00:00
|
|
|
// Draw autocomplete list.
|
|
|
|
i.autocompleteListMutex.Lock()
|
|
|
|
defer i.autocompleteListMutex.Unlock()
|
|
|
|
if i.autocompleteList != nil {
|
|
|
|
// How much space do we need?
|
|
|
|
lheight := i.autocompleteList.GetItemCount()
|
|
|
|
lwidth := 0
|
|
|
|
for index := 0; index < lheight; index++ {
|
|
|
|
entry, _ := i.autocompleteList.GetItemText(index)
|
|
|
|
width := TaggedStringWidth(entry)
|
|
|
|
if width > lwidth {
|
|
|
|
lwidth = width
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// We prefer to drop down but if there is no space, maybe drop up?
|
|
|
|
lx := x
|
|
|
|
ly := y + 1
|
|
|
|
_, sheight := screen.Size()
|
|
|
|
if ly+lheight >= sheight && ly-2 > lheight-ly {
|
|
|
|
ly = y - lheight
|
|
|
|
if ly < 0 {
|
|
|
|
ly = 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ly+lheight >= sheight {
|
|
|
|
lheight = sheight - ly
|
|
|
|
}
|
|
|
|
i.autocompleteList.SetRect(lx, ly, lwidth, lheight)
|
|
|
|
i.autocompleteList.Draw(screen)
|
|
|
|
}
|
|
|
|
|
2017-12-15 14:29:21 +00:00
|
|
|
// Set cursor.
|
2020-11-17 18:33:25 +00:00
|
|
|
if i.HasFocus() {
|
2018-10-18 06:34:08 +00:00
|
|
|
screen.ShowCursor(x+cursorScreenPos, y)
|
2018-04-09 19:07:03 +00:00
|
|
|
}
|
2017-12-15 14:29:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// InputHandler returns the handler for this primitive.
|
2017-12-18 19:04:52 +00:00
|
|
|
func (i *InputField) InputHandler() func(event *tcell.EventKey, setFocus func(p Primitive)) {
|
2018-03-19 20:25:30 +00:00
|
|
|
return i.WrapInputHandler(func(event *tcell.EventKey, setFocus func(p Primitive)) {
|
2018-01-10 09:40:51 +00:00
|
|
|
// Trigger changed events.
|
|
|
|
currentText := i.text
|
|
|
|
defer func() {
|
2019-07-08 08:34:06 +00:00
|
|
|
if i.text != currentText {
|
|
|
|
i.Autocomplete()
|
|
|
|
if i.changed != nil {
|
|
|
|
i.changed(i.text)
|
|
|
|
}
|
2018-01-10 09:40:51 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2018-10-18 06:34:08 +00:00
|
|
|
// Movement functions.
|
|
|
|
home := func() { i.cursorPos = 0 }
|
|
|
|
end := func() { i.cursorPos = len(i.text) }
|
|
|
|
moveLeft := func() {
|
|
|
|
iterateStringReverse(i.text[:i.cursorPos], func(main rune, comb []rune, textPos, textWidth, screenPos, screenWidth int) bool {
|
|
|
|
i.cursorPos -= textWidth
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
}
|
|
|
|
moveRight := func() {
|
2022-07-30 14:59:31 +00:00
|
|
|
iterateString(i.text[i.cursorPos:], func(main rune, comb []rune, textPos, textWidth, screenPos, screenWidth, boundaries int) bool {
|
2018-10-18 06:34:08 +00:00
|
|
|
i.cursorPos += textWidth
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
}
|
|
|
|
moveWordLeft := func() {
|
|
|
|
i.cursorPos = len(regexp.MustCompile(`\S+\s*$`).ReplaceAllString(i.text[:i.cursorPos], ""))
|
|
|
|
}
|
|
|
|
moveWordRight := func() {
|
|
|
|
i.cursorPos = len(i.text) - len(regexp.MustCompile(`^\s*\S+\s*`).ReplaceAllString(i.text[i.cursorPos:], ""))
|
|
|
|
}
|
|
|
|
|
2018-10-29 16:30:58 +00:00
|
|
|
// Add character function. Returns whether or not the rune character is
|
|
|
|
// accepted.
|
|
|
|
add := func(r rune) bool {
|
|
|
|
newText := i.text[:i.cursorPos] + string(r) + i.text[i.cursorPos:]
|
2018-11-18 21:15:59 +00:00
|
|
|
if i.accept != nil && !i.accept(newText, r) {
|
|
|
|
return false
|
2018-10-29 16:30:58 +00:00
|
|
|
}
|
|
|
|
i.text = newText
|
|
|
|
i.cursorPos += len(string(r))
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2021-01-11 18:45:19 +00:00
|
|
|
// Change the autocomplete selection.
|
|
|
|
autocompleteSelect := func(offset int) {
|
|
|
|
count := i.autocompleteList.GetItemCount()
|
|
|
|
newEntry := i.autocompleteList.GetCurrentItem() + offset
|
|
|
|
if newEntry >= count {
|
|
|
|
newEntry = 0
|
|
|
|
} else if newEntry < 0 {
|
|
|
|
newEntry = count - 1
|
|
|
|
}
|
|
|
|
i.autocompleteList.SetCurrentItem(newEntry)
|
|
|
|
currentText, _ = i.autocompleteList.GetItemText(newEntry) // Don't trigger changed function twice.
|
|
|
|
currentText = stripTags(currentText)
|
|
|
|
i.SetText(currentText)
|
|
|
|
}
|
|
|
|
|
2019-07-08 08:34:06 +00:00
|
|
|
// Finish up.
|
|
|
|
finish := func(key tcell.Key) {
|
|
|
|
if i.done != nil {
|
|
|
|
i.done(key)
|
|
|
|
}
|
|
|
|
if i.finished != nil {
|
|
|
|
i.finished(key)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-15 14:29:21 +00:00
|
|
|
// Process key event.
|
2019-07-08 08:34:06 +00:00
|
|
|
i.autocompleteListMutex.Lock()
|
|
|
|
defer i.autocompleteListMutex.Unlock()
|
2017-12-15 14:29:21 +00:00
|
|
|
switch key := event.Key(); key {
|
|
|
|
case tcell.KeyRune: // Regular character.
|
2018-11-05 10:09:49 +00:00
|
|
|
if event.Modifiers()&tcell.ModAlt > 0 {
|
2018-10-18 06:34:08 +00:00
|
|
|
// We accept some Alt- key combinations.
|
|
|
|
switch event.Rune() {
|
|
|
|
case 'a': // Home.
|
|
|
|
home()
|
|
|
|
case 'e': // End.
|
|
|
|
end()
|
|
|
|
case 'b': // Move word left.
|
|
|
|
moveWordLeft()
|
|
|
|
case 'f': // Move word right.
|
|
|
|
moveWordRight()
|
2019-03-24 18:21:52 +00:00
|
|
|
default:
|
|
|
|
if !add(event.Rune()) {
|
|
|
|
return
|
|
|
|
}
|
2018-11-05 10:09:49 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Other keys are simply accepted as regular characters.
|
|
|
|
if !add(event.Rune()) {
|
2019-03-24 18:21:52 +00:00
|
|
|
return
|
2017-12-15 14:29:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
case tcell.KeyCtrlU: // Delete all.
|
|
|
|
i.text = ""
|
2018-10-18 06:34:08 +00:00
|
|
|
i.cursorPos = 0
|
|
|
|
case tcell.KeyCtrlK: // Delete until the end of the line.
|
|
|
|
i.text = i.text[:i.cursorPos]
|
2017-12-15 22:03:01 +00:00
|
|
|
case tcell.KeyCtrlW: // Delete last word.
|
2018-10-18 06:34:08 +00:00
|
|
|
lastWord := regexp.MustCompile(`\S+\s*$`)
|
|
|
|
newText := lastWord.ReplaceAllString(i.text[:i.cursorPos], "") + i.text[i.cursorPos:]
|
|
|
|
i.cursorPos -= len(i.text) - len(newText)
|
|
|
|
i.text = newText
|
|
|
|
case tcell.KeyBackspace, tcell.KeyBackspace2: // Delete character before the cursor.
|
|
|
|
iterateStringReverse(i.text[:i.cursorPos], func(main rune, comb []rune, textPos, textWidth, screenPos, screenWidth int) bool {
|
|
|
|
i.text = i.text[:textPos] + i.text[textPos+textWidth:]
|
|
|
|
i.cursorPos -= textWidth
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
if i.offset >= i.cursorPos {
|
|
|
|
i.offset = 0
|
|
|
|
}
|
2021-02-17 08:38:35 +00:00
|
|
|
case tcell.KeyDelete, tcell.KeyCtrlD: // Delete character after the cursor.
|
2022-07-30 14:59:31 +00:00
|
|
|
iterateString(i.text[i.cursorPos:], func(main rune, comb []rune, textPos, textWidth, screenPos, screenWidth, boundaries int) bool {
|
2018-10-18 06:34:08 +00:00
|
|
|
i.text = i.text[:i.cursorPos] + i.text[i.cursorPos+textWidth:]
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
case tcell.KeyLeft:
|
|
|
|
if event.Modifiers()&tcell.ModAlt > 0 {
|
|
|
|
moveWordLeft()
|
|
|
|
} else {
|
|
|
|
moveLeft()
|
|
|
|
}
|
2021-02-17 08:38:35 +00:00
|
|
|
case tcell.KeyCtrlB:
|
|
|
|
moveLeft()
|
2018-10-18 06:34:08 +00:00
|
|
|
case tcell.KeyRight:
|
|
|
|
if event.Modifiers()&tcell.ModAlt > 0 {
|
|
|
|
moveWordRight()
|
|
|
|
} else {
|
|
|
|
moveRight()
|
2017-12-15 14:29:21 +00:00
|
|
|
}
|
2021-02-17 08:38:35 +00:00
|
|
|
case tcell.KeyCtrlF:
|
|
|
|
moveRight()
|
2018-10-18 06:34:08 +00:00
|
|
|
case tcell.KeyHome, tcell.KeyCtrlA:
|
|
|
|
home()
|
|
|
|
case tcell.KeyEnd, tcell.KeyCtrlE:
|
|
|
|
end()
|
2021-01-11 18:45:19 +00:00
|
|
|
case tcell.KeyEnter:
|
2019-07-08 08:34:06 +00:00
|
|
|
if i.autocompleteList != nil {
|
2021-01-11 18:45:19 +00:00
|
|
|
autocompleteSelect(0)
|
2019-07-08 08:34:06 +00:00
|
|
|
i.autocompleteList = nil
|
|
|
|
} else {
|
|
|
|
finish(key)
|
2017-12-15 14:29:21 +00:00
|
|
|
}
|
2021-01-11 18:45:19 +00:00
|
|
|
case tcell.KeyEscape:
|
2019-07-08 08:34:06 +00:00
|
|
|
if i.autocompleteList != nil {
|
2021-01-11 18:45:19 +00:00
|
|
|
i.autocompleteList = nil
|
|
|
|
} else {
|
|
|
|
finish(key)
|
|
|
|
}
|
|
|
|
case tcell.KeyTab:
|
|
|
|
if i.autocompleteList != nil {
|
|
|
|
autocompleteSelect(0)
|
|
|
|
} else {
|
|
|
|
finish(key)
|
|
|
|
}
|
|
|
|
case tcell.KeyDown:
|
|
|
|
if i.autocompleteList != nil {
|
|
|
|
autocompleteSelect(1)
|
2019-07-08 08:34:06 +00:00
|
|
|
} else {
|
|
|
|
finish(key)
|
|
|
|
}
|
|
|
|
case tcell.KeyUp, tcell.KeyBacktab: // Autocomplete selection.
|
|
|
|
if i.autocompleteList != nil {
|
2021-01-11 18:45:19 +00:00
|
|
|
autocompleteSelect(-1)
|
2019-07-08 08:34:06 +00:00
|
|
|
} else {
|
|
|
|
finish(key)
|
2018-04-19 19:34:03 +00:00
|
|
|
}
|
2017-12-15 14:29:21 +00:00
|
|
|
}
|
2018-01-14 20:29:34 +00:00
|
|
|
})
|
2017-12-15 14:29:21 +00:00
|
|
|
}
|
2019-11-04 06:30:25 +00:00
|
|
|
|
|
|
|
// MouseHandler returns the mouse handler for this primitive.
|
2020-02-14 02:09:09 +00:00
|
|
|
func (i *InputField) MouseHandler() func(action MouseAction, event *tcell.EventMouse, setFocus func(p Primitive)) (consumed bool, capture Primitive) {
|
|
|
|
return i.WrapMouseHandler(func(action MouseAction, event *tcell.EventMouse, setFocus func(p Primitive)) (consumed bool, capture Primitive) {
|
2020-03-27 17:41:44 +00:00
|
|
|
x, y := event.Position()
|
|
|
|
_, rectY, _, _ := i.GetInnerRect()
|
|
|
|
if !i.InRect(x, y) {
|
2020-02-14 02:09:09 +00:00
|
|
|
return false, nil
|
2020-01-24 20:40:34 +00:00
|
|
|
}
|
2020-03-27 17:41:44 +00:00
|
|
|
|
2019-11-04 06:30:25 +00:00
|
|
|
// Process mouse event.
|
2022-09-02 15:49:12 +00:00
|
|
|
if y == rectY {
|
|
|
|
if action == MouseLeftDown {
|
|
|
|
setFocus(i)
|
|
|
|
consumed = true
|
|
|
|
} else if action == MouseLeftClick {
|
|
|
|
// Determine where to place the cursor.
|
|
|
|
if x >= i.fieldX {
|
|
|
|
if !iterateString(i.text[i.offset:], func(main rune, comb []rune, textPos int, textWidth int, screenPos int, screenWidth, boundaries int) bool {
|
|
|
|
if x-i.fieldX < screenPos+screenWidth {
|
|
|
|
i.cursorPos = textPos + i.offset
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}) {
|
|
|
|
i.cursorPos = len(i.text)
|
2020-03-27 17:41:44 +00:00
|
|
|
}
|
|
|
|
}
|
2022-09-02 15:49:12 +00:00
|
|
|
consumed = true
|
2020-03-27 17:41:44 +00:00
|
|
|
}
|
2019-11-04 06:30:25 +00:00
|
|
|
}
|
2020-03-27 17:41:44 +00:00
|
|
|
|
|
|
|
return
|
2019-11-04 06:30:25 +00:00
|
|
|
})
|
|
|
|
}
|