Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Quote initial commands to make it possible to easily use hishtory to find matching entries for already typed commands that contain flags #251

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
10 changes: 5 additions & 5 deletions .github/workflows/go-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,11 @@ jobs:
with:
name: goldens-used-${{ matrix.os }}-${{ matrix.test_shard }}
path: /tmp/goldens-used.txt
- name: Setup tmate session
if: ${{ failure() }}
uses: mxschmitt/action-tmate@v3
with:
limit-access-to-actor: true
# - name: Setup tmate session
# if: ${{ failure() }}
# uses: mxschmitt/action-tmate@v3
# with:
# limit-access-to-actor: true
check-goldens:
runs-on: ubuntu-latest
needs: test
Expand Down
29 changes: 19 additions & 10 deletions client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2114,6 +2114,14 @@ func testTui_search(t *testing.T, onlineStatus OnlineStatus) {
"'\"'foo:bar'\"'",
}))
testutils.CompareGoldens(t, out, "TestTui-SearchColonDoubleQuoted")

// And check that we can quote dashes
require.NoError(t, db.Create(testutils.MakeFakeHistoryEntry("foo --bar")).Error)
out = stripTuiCommandPrefix(t, captureTerminalOutput(t, tester, []string{
"hishtory SPACE tquery ENTER",
"'\"'--bar'\"'",
}))
testutils.CompareGoldens(t, out, "TestTui-SearchQuoteDash")
}

func testTui_general(t *testing.T, onlineStatus OnlineStatus) {
Expand Down Expand Up @@ -2469,8 +2477,8 @@ echo bar`)
tester.RunInteractiveShell(t, `hishtory config-set displayed-columns 'Exit Code' git_remote Command`)
out = tester.RunInteractiveShell(t, `hishtory query -pipefail`)
testutils.CompareGoldens(t, out, fmt.Sprintf("testCustomColumns-query-isAction=%v", testutils.IsGithubAction()))
out = captureTerminalOutput(t, tester, []string{"hishtory SPACE tquery SPACE -pipefail ENTER"})
out = stripRequiredPrefix(t, out, "hishtory tquery -pipefail")
out = captureTerminalOutput(t, tester, []string{"hishtory SPACE tquery SPACE ENTER", "-pipefail"})
out = stripRequiredPrefix(t, out, "hishtory tquery")
testName := "testCustomColumns-tquery-" + tester.ShellName()
if testutils.IsGithubAction() {
testName += "-isAction"
Expand Down Expand Up @@ -2726,8 +2734,8 @@ func TestTimestampFormat(t *testing.T) {
// And check that it is displayed in both the tui and the classic view
out := hishtoryQuery(t, tester, "-pipefail -tablesizing")
testutils.CompareGoldens(t, out, "TestTimestampFormat-query")
out = captureTerminalOutput(t, tester, []string{"hishtory SPACE tquery SPACE -pipefail SPACE -tablesizing ENTER"})
out = stripRequiredPrefix(t, out, "hishtory tquery -pipefail -tablesizing")
out = captureTerminalOutput(t, tester, []string{"hishtory SPACE tquery ENTER", "table_cmd SPACE -tquery"})
out = stripRequiredPrefix(t, out, "hishtory tquery")
testutils.CompareGoldens(t, out, "TestTimestampFormat-tquery")
}

Expand Down Expand Up @@ -2766,7 +2774,7 @@ func TestSortByConsistentTimezone(t *testing.T) {
testutils.CompareGoldens(t, out, "TestSortByConsistentTimezone-query")
out = tester.RunInteractiveShell(t, `hishtory export -pipefail -tablesizing`)
testutils.CompareGoldens(t, out, "TestSortByConsistentTimezone-export")
out = captureTerminalOutput(t, tester, []string{"hishtory SPACE tquery SPACE -pipefail SPACE -tablesizing ENTER"})
out = captureTerminalOutput(t, tester, []string{"hishtory SPACE tquery ENTER", "-pipefail SPACE -tablesizing"})
out = stripTuiCommandPrefix(t, out)
require.Regexp(t, regexp.MustCompile(`Timestamp[\s\S]*Command[\s\S]*Apr 16 2022 01:36:26 PDT[\s\S]*third_entry[\s\S]*Apr 16 2022 01:19:46 PDT[\s\S]*second_entry[\s\S]*Apr 16 2022 01:03:06 PDT[\s\S]*first_entry`), out)
}
Expand Down Expand Up @@ -2827,8 +2835,8 @@ echo foo`)
tester.RunInteractiveShell(t, `hishtory config-set displayed-columns 'Exit Code' Command`)
out = tester.RunInteractiveShell(t, `hishtory query -pipefail`)
testutils.CompareGoldens(t, out, "testRemoveDuplicateRows-query")
out = captureTerminalOutput(t, tester, []string{"hishtory SPACE tquery SPACE -pipefail ENTER"})
out = stripRequiredPrefix(t, out, "hishtory tquery -pipefail")
out = captureTerminalOutput(t, tester, []string{"hishtory SPACE tquery ENTER", "-pipefail"})
out = stripRequiredPrefix(t, out, "hishtory tquery")
testutils.CompareGoldens(t, out, "testRemoveDuplicateRows-tquery")

// And change the config to filter out duplicate rows
Expand All @@ -2843,13 +2851,14 @@ echo foo`)
testutils.CompareGoldens(t, out, "testRemoveDuplicateRows-enabled-query")

// Check tquery
out = captureTerminalOutput(t, tester, []string{"hishtory SPACE tquery SPACE -pipefail ENTER"})
out = stripRequiredPrefix(t, out, "hishtory tquery -pipefail")
out = captureTerminalOutput(t, tester, []string{"hishtory SPACE tquery ENTER", "-pipefail"})
out = stripRequiredPrefix(t, out, "hishtory tquery")
testutils.CompareGoldens(t, out, "testRemoveDuplicateRows-enabled-tquery")

// Check actually selecting it with query
out = captureTerminalOutputWithComplexCommands(t, tester, []TmuxCommand{
{Keys: "hishtory SPACE tquery SPACE -pipefail ENTER", ExtraDelay: 1.0},
{Keys: "hishtory SPACE tquery ENTER", ExtraDelay: 1.0},
{Keys: "-pipefail", ExtraDelay: 1.0},
{Keys: "Down Down"},
{Keys: "ENTER", ExtraDelay: 1.0},
})
Expand Down
2 changes: 1 addition & 1 deletion client/cmd/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ var tqueryCmd = &cobra.Command{
if os.Getenv("HISHTORY_SHELL_NAME") != "" {
shellName = os.Getenv("HISHTORY_SHELL_NAME")
}
lib.CheckFatalError(tui.TuiQuery(ctx, shellName, strings.Join(args, " ")))
lib.CheckFatalError(tui.TuiQuery(ctx, shellName, args))
},
}

Expand Down
13 changes: 10 additions & 3 deletions client/lib/lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -965,7 +965,9 @@ func splitEscaped(query string, separator rune, maxSplit int) []string {
isInSingleQuotedString := false
for i := 0; i < len(runeQuery); i++ {
if (maxSplit < 0 || splits < maxSplit) && runeQuery[i] == separator && !isInSingleQuotedString && !isInDoubleQuotedString {
tokens = append(tokens, string(token))
if string(token) != "" {
tokens = append(tokens, string(token))
}
token = token[:0]
splits++
} else if runeQuery[i] == '\\' && i+1 < len(runeQuery) {
Expand All @@ -982,8 +984,13 @@ func splitEscaped(query string, separator rune, maxSplit int) []string {
} else if runeQuery[i] == '\'' && !isInDoubleQuotedString && !heuristicIgnoreUnclosedQuote(isInSingleQuotedString, '\'', runeQuery, i) {
isInSingleQuotedString = !isInSingleQuotedString
} else {
if (isInSingleQuotedString || isInDoubleQuotedString) && separator == ' ' && runeQuery[i] == ':' {
token = append(token, '\\')
if (isInSingleQuotedString || isInDoubleQuotedString) && separator == ' ' {
if runeQuery[i] == ':' {
token = append(token, '\\')
}
if runeQuery[i] == '-' && len(token) == 0 {
token = append(token, '\\')
}
}
token = append(token, runeQuery[i])
}
Expand Down
5 changes: 5 additions & 0 deletions client/lib/lib_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,11 @@ func TestSplitEscaped(t *testing.T) {
{"foo:bar", ' ', -1, []string{"foo:bar"}},
{"'foo:bar'", ' ', -1, []string{"foo\\:bar"}},
{"\"foo:bar\"", ' ', -1, []string{"foo\\:bar"}},
// Tests for quoting dashes
{"'-foo'", ' ', -1, []string{"\\-foo"}},
{"'--foo'", ' ', -1, []string{"\\--foo"}},
{"bar '--foo'", ' ', -1, []string{"bar", "\\--foo"}},
{"bar 'foo-baz'", ' ', -1, []string{"bar", "foo-baz"}},
}
for _, tc := range testcases {
actual := splitEscaped(tc.input, tc.char, tc.limit)
Expand Down
8 changes: 4 additions & 4 deletions client/testdata/TestTimestampFormat-tquery
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
Search Query: > -pipefail -tablesizing
Search Query: > table_cmd -tquery

┌──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│ Hostname CWD Timestamp Runtime Exit Code Command
│ Hostname CWD Timestamp Runtime Exit Code Command
│──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────│
│ localhost ~/foo/ 2022/Apr/16 01:03 24s 3 table_cmd2
│ localhost /tmp/ 2022/Apr/16 01:03 4s 2 table_cmd1 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa… │
│ localhost ~/foo/ 2022/Apr/16 01:03 24s 3 table_cmd2
│ localhost /tmp/ 2022/Apr/16 01:03 4s 2 table_cmd1 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa… │
│ │
│ │
│ │
Expand Down
27 changes: 27 additions & 0 deletions client/testdata/TestTui-SearchQuoteDash
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
Search Query: > "--bar"

┌────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│ Hostname CWD Timestamp Runtime Exit Code Command │
│────────────────────────────────────────────────────────────────────────────────────────────────────────│
│ localhost /tmp/ Oct 17 2022 21:43:46 PDT 3s 2 foo --bar │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
└────────────────────────────────────────────────────────────────────────────────────────────────────────┘
hiSHtory: Search your shell history • ctrl+h help
2 changes: 1 addition & 1 deletion client/testdata/testCustomColumns-tquery-bash-isAction
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ Search Query: > -pipefail
┌────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│ Exit Code git_remote Command │
│────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────│
│ 0 https://github.com/ddworken/hishtory hishtory tquery │
│ 0 https://github.com/ddworken/hishtory hishtory config-set displayed-columns 'Exit Code' git_remote Command │
│ 0 echo bar │
│ 0 cd / │
Expand All @@ -22,6 +23,5 @@ Search Query: > -pipefail
│ │
│ │
│ │
│ │
└────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
hiSHtory: Search your shell history • ctrl+h help
Loading
Loading