Add support for control-A and control-E shortcuts similar to GNU readline

This commit is contained in:
David Dworken 2024-02-19 12:26:44 -08:00
parent 87c2cde688
commit 0a76c875e4
No known key found for this signature in database
3 changed files with 25 additions and 0 deletions

View File

@ -2089,6 +2089,14 @@ func testTui_general(t *testing.T, onlineStatus OnlineStatus) {
out = strings.Split(stripTuiCommandPrefix(t, out), "\n")[0]
testutils.CompareGoldens(t, out, "TestTui-SelectAndCd")
// Test jumping around the cursor via shortcuts
out = captureTerminalOutput(t, tester, []string{
"hishtory SPACE tquery ENTER",
"foo C-a AAA C-e ZZZ",
})
out = strings.Split(stripTuiCommandPrefix(t, out), "\n")[0]
testutils.CompareGoldens(t, out, "TestTui-JumpCursor")
// Test the User column
tester.RunInteractiveShell(t, `hishtory config-add displayed-columns User`)
out = captureTerminalOutput(t, tester, []string{

1
client/testdata/TestTui-JumpCursor vendored Normal file
View File

@ -0,0 +1 @@
Search Query: > AAAfooZZZ

View File

@ -56,6 +56,8 @@ type keyMap struct {
DeleteEntry key.Binding
Help key.Binding
Quit key.Binding
JumpStartOfInput key.Binding
JumpEndOfInput key.Binding
}
var fakeTitleKeyBinding key.Binding = key.NewBinding(
@ -134,6 +136,14 @@ var keys = keyMap{
key.WithKeys("esc", "ctrl+c", "ctrl+d"),
key.WithHelp("esc", "exit hiSHtory "),
),
JumpStartOfInput: key.NewBinding(
key.WithKeys("ctrl+a"),
key.WithHelp("ctrl+a", "jump to the start of the input "),
),
JumpEndOfInput: key.NewBinding(
key.WithKeys("ctrl+e"),
key.WithHelp("ctrl+e", "jump to the end of the input "),
),
}
type SelectStatus int64
@ -341,6 +351,12 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
case key.Matches(msg, keys.Help):
m.help.ShowAll = !m.help.ShowAll
return m, nil
case key.Matches(msg, keys.JumpStartOfInput):
m.queryInput.SetCursor(0)
return m, nil
case key.Matches(msg, keys.JumpEndOfInput):
m.queryInput.SetCursor(len(m.queryInput.Value()))
return m, nil
default:
pendingCommands := tea.Batch()
if m.table != nil {