diff options
author | Tulir Asokan <tulir@maunium.net> | 2018-04-21 19:41:19 +0300 |
---|---|---|
committer | Tulir Asokan <tulir@maunium.net> | 2018-04-21 19:41:19 +0300 |
commit | d147fc7579bf77bf6f3ace669c8ade68be89d1ca (patch) | |
tree | e19b9409e15feecd9b9317c86408da37fb18a0e4 /ui/widget | |
parent | c3386ba118b1a0f2ae1a31a9787ea5cb8b68396f (diff) |
Improve tab completion system
Diffstat (limited to 'ui/widget')
-rw-r--r-- | ui/widget/advanced-inputfield.go | 25 |
1 files changed, 17 insertions, 8 deletions
diff --git a/ui/widget/advanced-inputfield.go b/ui/widget/advanced-inputfield.go index cbe9dcc..a084dcf 100644 --- a/ui/widget/advanced-inputfield.go +++ b/ui/widget/advanced-inputfield.go @@ -84,7 +84,7 @@ type AdvancedInputField struct { done func(tcell.Key) // An optional function which is called when the user presses tab. - tabComplete func(text string, cursorOffset int) string + tabComplete func(text string, cursorOffset int) } // NewAdvancedInputField returns a new input field. @@ -107,6 +107,20 @@ func (field *AdvancedInputField) SetText(text string) *AdvancedInputField { return field } +// SetTextAndMoveCursor sets the current text of the input field and moves the cursor with the width difference. +func (field *AdvancedInputField) SetTextAndMoveCursor(text string) *AdvancedInputField { + oldWidth := runewidth.StringWidth(field.text) + field.text = text + newWidth := runewidth.StringWidth(field.text) + if oldWidth != newWidth { + field.cursorOffset += newWidth - oldWidth + } + if field.changed != nil { + field.changed(field.text) + } + return field +} + // GetText returns the current text of the input field. func (field *AdvancedInputField) GetText() string { return field.text @@ -212,7 +226,7 @@ func (field *AdvancedInputField) SetDoneFunc(handler func(key tcell.Key)) *Advan return field } -func (field *AdvancedInputField) SetTabCompleteFunc(handler func(text string, cursorOffset int) string) *AdvancedInputField { +func (field *AdvancedInputField) SetTabCompleteFunc(handler func(text string, cursorOffset int)) *AdvancedInputField { field.tabComplete = handler return field } @@ -443,12 +457,7 @@ func (field *AdvancedInputField) RemovePreviousCharacter() { func (field *AdvancedInputField) TriggerTabComplete() bool { if field.tabComplete != nil { - oldWidth := runewidth.StringWidth(field.text) - field.text = field.tabComplete(field.text, field.cursorOffset) - newWidth := runewidth.StringWidth(field.text) - if oldWidth != newWidth { - field.cursorOffset += newWidth - oldWidth - } + field.tabComplete(field.text, field.cursorOffset) return true } return false |