diff --git a/client/client_test.go b/client/client_test.go index b1cb6f7..085d961 100644 --- a/client/client_test.go +++ b/client/client_test.go @@ -155,6 +155,7 @@ func TestParameterized(t *testing.T) { t.Run("testConfigGetSet/"+tester.ShellName(), func(t *testing.T) { testConfigGetSet(t, tester) }) t.Run("testControlR/"+tester.ShellName(), func(t *testing.T) { testControlR(t, tester, tester.ShellName()) }) t.Run("testHandleUpgradedFeatures/"+tester.ShellName(), func(t *testing.T) { testHandleUpgradedFeatures(t, tester) }) + t.Run("testCustomColumns/"+tester.ShellName(), func(t *testing.T) { testCustomColumns(t, tester) }) } t.Run("testControlR/fish", func(t *testing.T) { testControlR(t, bashTester{}, "fish") }) } @@ -1701,6 +1702,7 @@ func TestFish(t *testing.T) { if err != nil { t.Fatalf("fish is not installed") } + // TODO: migrate this to the wrapper function out := tester.RunInteractiveShell(t, ` export SHELL=`+fishLocation+` tmux kill-session -t foo || true tmux -u new-session -d -x 200 -y 50 -s foo @@ -1952,6 +1954,43 @@ func testControlR(t *testing.T, tester shellTester, shellName string) { } } +func testCustomColumns(t *testing.T, tester shellTester) { + // Setup + defer testutils.BackupAndRestore(t)() + installHishtory(t, tester, "") + + // Record a few commands with no custom columns + out := tester.RunInteractiveShell(t, `export FOOBAR='hello' +echo $FOOBAR world +cd / +echo baz`) + if out != "hello world\nbaz\n" { + t.Fatalf("unexpected command output=%#v", out) + } + + // Check that the hishtory is saved correctly + out = tester.RunInteractiveShell(t, `hishtory export | grep -v pipefail`) + compareGoldens(t, out, "testCustomColumns-initHistory") + + // Configure a custom column + tester.RunInteractiveShell(t, `hishtory config-add custom-column git_remote '(git remote -v 2>/dev/null | grep origin 1>/dev/null ) && git remote get-url origin || true'`) + + // Run a few commands, some of which will have a git_remote + out = tester.RunInteractiveShell(t, `echo foo +cd / +echo bar`) + if out != "foo\nbar\n" { + t.Fatalf("unexpected command output=%#v", out) + } + + // And check that it is all recorded correctly + tester.RunInteractiveShell(t, `hishtory config-set displayed-columns 'Exit Code' git_remote Command `) + out = tester.RunInteractiveShell(t, `hishtory query -pipefail`) + compareGoldens(t, out, "testCustomColumns-query") + out = captureTerminalOutput(t, tester, []string{"hishtory SPACE tquery SPACE -pipefail ENTER"}) + compareGoldens(t, out, "testCustomColumns-tquery") +} + type deviceSet struct { deviceMap *map[device]deviceOp currentDevice *device diff --git a/client/lib/goldens/testCustomColumns-initHistory b/client/lib/goldens/testCustomColumns-initHistory new file mode 100644 index 0000000..8d7917f --- /dev/null +++ b/client/lib/goldens/testCustomColumns-initHistory @@ -0,0 +1,4 @@ +export FOOBAR='hello' +echo $FOOBAR world +cd / +echo baz diff --git a/client/lib/goldens/testCustomColumns-query b/client/lib/goldens/testCustomColumns-query new file mode 100644 index 0000000..8ceba6d --- /dev/null +++ b/client/lib/goldens/testCustomColumns-query @@ -0,0 +1,10 @@ +Exit Code git_remote Command +0 git@github.com:ddworken/hishtory.git hishtory config-set displayed-columns 'Exit Code' git_remote Command +0 echo bar +0 cd / +0 git@github.com:ddworken/hishtory.git echo foo +0 git@github.com:ddworken/hishtory.git hishtory config-add custom-column git_remote '(git remote -v 2>/dev/null | grep origin 1>/dev/null ) && git remote get-url origin || true' +0 echo baz +0 cd / +0 echo $FOOBAR world +0 export FOOBAR='hello' diff --git a/client/lib/goldens/testCustomColumns-tquery b/client/lib/goldens/testCustomColumns-tquery new file mode 100644 index 0000000..27d67c5 --- /dev/null +++ b/client/lib/goldens/testCustomColumns-tquery @@ -0,0 +1,31 @@ +bash-5.2$ source /Users/david/.bashrc +bash-5.2$ hishtory tquery -pipefail + + + +Search Query: > -pipefail + +┌─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐ +│ Exit Code git_remote Command │ +│─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────│ +│ 0 git@github.com:ddworken/hishtory.git source /Users/david/.bashrc │ +│ 0 git@github.com:ddworken/hishtory.git hishtory config-set displayed-columns 'Exit Code' git_remote Command │ +│ 0 echo bar │ +│ 0 cd / │ +│ 0 git@github.com:ddworken/hishtory.git echo foo │ +│ 0 git@github.com:ddworken/hishtory.git hishtory config-add custom-column git_remote '(git remote -v 2>/dev/null | grep origin 1>/dev/null ) && git remote get-url origin || … │ +│ 0 echo baz │ +│ 0 cd / │ +│ 0 echo $FOOBAR world │ +│ 0 export FOOBAR='hello' │ +│ │ +│ │ +│ │ +│ │ +│ │ +│ │ +│ │ +│ │ +│ │ +│ │ +└─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘ \ No newline at end of file diff --git a/hishtory.go b/hishtory.go index b1f4144..5da3a02 100644 --- a/hishtory.go +++ b/hishtory.go @@ -166,6 +166,8 @@ func main() { config := hctx.GetConf(ctx) key := os.Args[2] switch key { + case "custom-column": + fallthrough case "custom-columns": columnName := os.Args[3] command := os.Args[4]