From d147fc7579bf77bf6f3ace669c8ade68be89d1ca Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Sat, 21 Apr 2018 19:41:19 +0300 Subject: Improve tab completion system --- ui/widget/advanced-inputfield.go | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) (limited to 'ui/widget/advanced-inputfield.go') 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 -- cgit v1.2.3