Enable control-r in tests + basic test for control-r in zsh

This commit is contained in:
David Dworken 2022-10-21 00:14:28 -07:00
parent 07bf84a4f9
commit c978ee9a2e
4 changed files with 121 additions and 2 deletions

View File

@ -146,6 +146,7 @@ func TestParameterized(t *testing.T) {
t.Run("testMultipleUsers/"+tester.ShellName(), func(t *testing.T) { testMultipleUsers(t, tester) })
t.Run("testConfigGetSet/"+tester.ShellName(), func(t *testing.T) { testConfigGetSet(t, tester) })
t.Run("testTui/"+tester.ShellName(), func(t *testing.T) { testTui(t, tester) })
t.Run("testControlR/"+tester.ShellName(), func(t *testing.T) { testControlR(t, tester) })
// TODO: Add a test for multi-line history entries
}
}
@ -1632,6 +1633,7 @@ func testTui(t *testing.T, tester shellTester) {
db.Create(data.MakeFakeHistoryEntry("ls ~/"))
db.Create(data.MakeFakeHistoryEntry("echo 'aaaaaa bbbb'"))
// TODO: Make the below actually use the correct shell
// Check the initial output when there is no search
out := strings.TrimSpace(tester.RunInteractiveShell(t, `tmux kill-session -t foo || true
tmux -u new-session -d -x 200 -y 50 -s foo
@ -1726,7 +1728,86 @@ func testTui(t *testing.T, tester shellTester) {
}
// TODO: test arrow keys
// TODO: test control-r
}
// TODO: the below, but for fish
func testControlR(t *testing.T, tester shellTester) {
if os.Getenv("GITHUB_ACTION") != "" {
t.Skip()
// TODO: run this on actions. Need to fix the timezone bug, see https://github.com/ddworken/hishtory/actions/runs/3277144800/jobs/5394045156
}
if tester.ShellName() == "bash" {
t.Skip()
// TODO: this currently fails on bash, need to figure this out
}
// Setup
defer shared.BackupAndRestore(t)()
installHishtory(t, tester, "")
// Disable recording so that all our testing commands don't get recorded
_, _ = tester.RunInteractiveShellRelaxed(t, ` hishtory disable`)
_, _ = tester.RunInteractiveShellRelaxed(t, `hishtory config-set enable-control-r true`)
// Insert a few hishtory entries
db := hctx.GetDb(hctx.MakeContext())
e1 := data.MakeFakeHistoryEntry("ls ~/")
e1.CurrentWorkingDirectory = "/etc/"
e1.Hostname = "server"
e1.ExitCode = 127
db.Create(e1)
db.Create(data.MakeFakeHistoryEntry("ls ~/foo/"))
db.Create(data.MakeFakeHistoryEntry("ls ~/bar/"))
db.Create(data.MakeFakeHistoryEntry("echo 'aaaaaa bbbb'"))
db.Create(data.MakeFakeHistoryEntry("echo 'bar' &"))
// And check that the control-r binding brings up the search
shellLocation, err := exec.LookPath(tester.ShellName())
if err != nil {
t.Fatalf("fish is not installed")
}
out := strings.TrimSpace(tester.RunInteractiveShell(t, `export SHELL=`+shellLocation+`
tmux kill-session -t foo || true
tmux -u new-session -d -x 200 -y 50 -s foo
tmux send -t foo echo SPACE '$SHELL' ENTER
sleep 1
tmux send -t foo C-R
sleep 3
tmux capture-pane -p -t foo
tmux kill-session -t foo`))
if !strings.Contains(out, "\n\n\n") {
t.Fatalf("failed to find separator in %#v", out)
}
stripped := strings.Split(out, "\n\n\n")[1]
expected := `Search Query: > ls
Hostname CWD Timestamp Exit Code Command
localhost /tmp/ Oct 17 2022 21:43:11 PDT 2 echo 'bar' &
localhost /tmp/ Oct 17 2022 21:43:11 PDT 2 echo 'aaaaaa bbbb'
localhost /tmp/ Oct 17 2022 21:43:11 PDT 2 ls ~/bar/
localhost /tmp/ Oct 17 2022 21:43:11 PDT 2 ls ~/foo/
server /etc/ Oct 17 2022 21:43:11 PDT 127 ls ~/
`
if diff := cmp.Diff(expected, stripped); diff != "" {
t.Fatalf("hishtory export mismatch (-expected +got):\n%s \n(out=%#v)", diff, out)
}
}
type deviceSet struct {

View File

@ -15,4 +15,18 @@ function __hishtory_on_prompt --on-event fish_prompt
else if [ -n "$_hishtory_command" ]
hishtory saveHistoryEntry fish $_hishtory_exit_code "$_hishtory_command" $_hishtory_start_time
end
end
end
function __hishtory_on_control_r
set -l tmp (mktemp -t fish.XXXXXX)
set -x init_query (commandline -b)
hishtory tquery $init_query > $tmp
set -l res $status
commandline -f repaint
if [ -s $tmp ]
commandline -r (cat $tmp)
end
rm -f $tmp
end
[ "$(hishtory config-get enable-control-r)" = true ] && bind \cr __hishtory_on_control_r

View File

@ -28,3 +28,14 @@ function __hishtory_postcommand() {
}
PROMPT_COMMAND="__hishtory_postcommand; $PROMPT_COMMAND"
export HISTTIMEFORMAT=$HISTTIMEFORMAT
__history_control_r() {
READLINE_LINE=$(hishtory tquery "$READLINE_LINE" | tr -d '\n')
READLINE_POINT=0x7FFFFFFF
}
__hishtory_bind_control_r() {
bind -x '"\C-r": __history_control_r'
}
[ "$(hishtory config-get enable-control-r)" = true ] && __hishtory_bind_control_r

View File

@ -23,3 +23,16 @@ function _hishtory_precmd() {
fi
hishtory saveHistoryEntry zsh $_hishtory_exit_code "$_hishtory_command" $_hishtory_start_time
}
_hishtory_widget() {
BUFFER=$(hishtory tquery $BUFFER | tr -d '\n')
CURSOR=${#BUFFER}
zle reset-prompt
}
_hishtory_bind_control_r() {
zle -N _hishtory_widget
bindkey '^R' _hishtory_widget
}
[ "$(hishtory config-get enable-control-r)" = true ] && _hishtory_bind_control_r