Add some vim-style keybindings to advanced inputfields

This commit is contained in:
Tulir Asokan 2018-03-20 12:14:30 +02:00
parent cce1403822
commit de2a8aee06

View File

@ -68,6 +68,8 @@ type AdvancedInputField struct {
// disables masking. // disables masking.
maskCharacter rune maskCharacter rune
vimBindings bool
// An optional function which may reject the last character that was entered. // An optional function which may reject the last character that was entered.
accept func(text string, ch rune) bool accept func(text string, ch rune) bool
@ -403,32 +405,46 @@ func (field *AdvancedInputField) InputHandler() func(event *tcell.EventKey, setF
break break
} }
leftPart := SubstringBefore(field.text, field.cursorOffset) leftPart := SubstringBefore(field.text, field.cursorOffset)
rightPart := field.text[len(leftPart):] // Take everything after the left part minus the first character.
rightPartRunes := []rune(rightPart) rightPart := string([]rune(field.text[len(leftPart):])[1:])
rightPartRunes = rightPartRunes[1:]
rightPart = string(rightPartRunes)
field.text = leftPart + rightPart field.text = leftPart + rightPart
case tcell.KeyBackspace, tcell.KeyBackspace2: // Delete last character. case tcell.KeyCtrlU:
if field.cursorOffset == 0 { if !field.vimBindings {
break break
} }
if key == tcell.KeyBackspace { // Ctrl+backspace field.text = ""
field.cursorOffset = 0
case tcell.KeyCtrlW:
if !field.vimBindings {
break
}
fallthrough
case tcell.KeyBackspace: // Delete last word
leftPart := SubstringBefore(field.text, field.cursorOffset) leftPart := SubstringBefore(field.text, field.cursorOffset)
rightPart := field.text[len(leftPart):] rightPart := field.text[len(leftPart):]
replacement := lastWord.ReplaceAllString(leftPart, "") replacement := lastWord.ReplaceAllString(leftPart, "")
field.text = replacement + rightPart field.text = replacement + rightPart
field.cursorOffset -= runewidth.StringWidth(leftPart) - runewidth.StringWidth(replacement) field.cursorOffset -= runewidth.StringWidth(leftPart) - runewidth.StringWidth(replacement)
} else { // Just backspace case tcell.KeyBackspace2: // Delete last character
if field.cursorOffset == 0 {
break
}
leftPart := SubstringBefore(field.text, field.cursorOffset) leftPart := SubstringBefore(field.text, field.cursorOffset)
rightPart := field.text[len(leftPart):] rightPart := field.text[len(leftPart):]
// Take everything before the right part minus the last character.
leftPartRunes := []rune(leftPart) leftPartRunes := []rune(leftPart)
leftPartRunes = leftPartRunes[0 : len(leftPartRunes)-1] leftPartRunes = leftPartRunes[0 : len(leftPartRunes)-1]
leftPart = string(leftPartRunes) leftPart = string(leftPartRunes)
// Figure out what character was removed to correctly decrease cursorOffset.
removedChar := field.text[len(leftPart) : len(field.text)-len(rightPart)] removedChar := field.text[len(leftPart) : len(field.text)-len(rightPart)]
field.text = leftPart + rightPart field.text = leftPart + rightPart
field.cursorOffset -= runewidth.StringWidth(removedChar) field.cursorOffset -= runewidth.StringWidth(removedChar)
}
case tcell.KeyTab: // Tab-completion case tcell.KeyTab: // Tab-completion
if field.tabComplete != nil { if field.tabComplete != nil {
oldWidth := runewidth.StringWidth(field.text) oldWidth := runewidth.StringWidth(field.text)