From 253ad7f6b650b7c7a74698764bd42e3b61adda7e Mon Sep 17 00:00:00 2001 From: David Dworken Date: Wed, 11 Oct 2023 18:18:56 -0700 Subject: [PATCH] Bold matches for search queries in TUI for #112. This was previously available behind the beta-mode flag, but will now be enabled by default --- client/client_test.go | 8 ++++---- client/cmd/configGet.go | 11 +++++++++++ client/cmd/configSet.go | 18 ++++++++++++++++++ client/cmd/install.go | 19 +++++++++++-------- client/hctx/hctx.go | 2 ++ client/tui/tui.go | 2 +- 6 files changed, 47 insertions(+), 13 deletions(-) diff --git a/client/client_test.go b/client/client_test.go index 245e67e..f87ef20 100644 --- a/client/client_test.go +++ b/client/client_test.go @@ -1692,7 +1692,6 @@ func testTui_scroll(t testing.TB) { // Assert there are no leaked connections assertNoLeakedConnections(t) - } func testTui_color(t testing.TB) { @@ -1705,6 +1704,7 @@ func testTui_color(t testing.TB) { // Setup defer testutils.BackupAndRestore(t)() tester, _, _ := setupTestTui(t) + tester.RunInteractiveShell(t, ` hishtory config-set highlight-matches false`) // Capture the TUI with full colored output, note that this golden will be harder to undersand // from inspection and primarily servers to detect unintended changes in hishtory's output. @@ -1717,9 +1717,9 @@ func testTui_color(t testing.TB) { out = strings.TrimSpace(strings.Split(out, "hishtory tquery")[1]) testutils.CompareGoldens(t, out, "TestTui-ColoredOutputWithSearch") - // And one more time with beta-mode for highlighting matches - tester.RunInteractiveShell(t, ` hishtory config-set beta-mode true`) - require.Equal(t, "true", strings.TrimSpace(tester.RunInteractiveShell(t, `hishtory config-get beta-mode`))) + // And one more time with highlight-matches + tester.RunInteractiveShell(t, ` hishtory config-set highlight-matches true`) + require.Equal(t, "true", strings.TrimSpace(tester.RunInteractiveShell(t, `hishtory config-get highlight-matches`))) out = captureTerminalOutputComplex(t, TmuxCaptureConfig{tester: tester, complexCommands: []TmuxCommand{{Keys: "hishtory SPACE tquery ENTER"}, {Keys: "ech"}}, includeEscapeSequences: true}) out = strings.TrimSpace(strings.Split(out, "hishtory tquery")[1]) testutils.CompareGoldens(t, out, "TestTui-ColoredOutputWithSearch-BetaMode") diff --git a/client/cmd/configGet.go b/client/cmd/configGet.go index 217b650..af47030 100644 --- a/client/cmd/configGet.go +++ b/client/cmd/configGet.go @@ -30,6 +30,16 @@ var getEnableControlRCmd = &cobra.Command{ }, } +var getHighlightMatchesCmd = &cobra.Command{ + Use: "highlight-matches", + Short: "Whether hishtory highlights matches in the search results", + Run: func(cmd *cobra.Command, args []string) { + ctx := hctx.MakeContext() + config := hctx.GetConf(ctx) + fmt.Println(config.HighlightMatches) + }, +} + var getFilterDuplicateCommandsCmd = &cobra.Command{ Use: "filter-duplicate-commands", Short: "Whether hishtory filters out duplicate commands when displaying your history", @@ -98,4 +108,5 @@ func init() { configGetCmd.AddCommand(getTimestampFormatCmd) configGetCmd.AddCommand(getCustomColumnsCmd) configGetCmd.AddCommand(getBetaModeCmd) + configGetCmd.AddCommand(getHighlightMatchesCmd) } diff --git a/client/cmd/configSet.go b/client/cmd/configSet.go index 8d4b3f4..85bb0fe 100644 --- a/client/cmd/configSet.go +++ b/client/cmd/configSet.go @@ -70,6 +70,23 @@ var setBetaModeCommand = &cobra.Command{ }, } +var setHighlightMatchesCmd = &cobra.Command{ + Use: "highlight-matches", + Short: "Enable highlight-matches to enable highlighting of matches in the search results", + Args: cobra.MatchAll(cobra.ExactArgs(1), cobra.OnlyValidArgs), + ValidArgs: []string{"true", "false"}, + Run: func(cmd *cobra.Command, args []string) { + val := args[0] + if val != "true" && val != "false" { + log.Fatalf("Unexpected config value %s, must be one of: true, false", val) + } + ctx := hctx.MakeContext() + config := hctx.GetConf(ctx) + config.HighlightMatches = (val == "true") + lib.CheckFatalError(hctx.SetConfig(config)) + }, +} + var setDisplayedColumnsCmd = &cobra.Command{ Use: "displayed-columns", Short: "The list of columns that hishtory displays", @@ -101,4 +118,5 @@ func init() { configSetCmd.AddCommand(setDisplayedColumnsCmd) configSetCmd.AddCommand(setTimestampFormatCmd) configSetCmd.AddCommand(setBetaModeCommand) + configSetCmd.AddCommand(setHighlightMatchesCmd) } diff --git a/client/cmd/install.go b/client/cmd/install.go index f55ba65..9e1598f 100644 --- a/client/cmd/install.go +++ b/client/cmd/install.go @@ -185,24 +185,27 @@ func handleDbUpgrades(ctx context.Context) error { } // Handles people running `hishtory update` from an old version of hishtory that -// doesn't support the control-r integration, so that they'll get control-r enabled -// but someone who has it explicitly disabled will keep it that way. +// doesn't support certain config options that we now default to true. This ensures +// that upgrades get them enabled by default, but if someone has it explicitly disabled, +// we keep it that way. func handleUpgradedFeatures() error { configContents, err := hctx.GetConfigContents() if err != nil { // No config, so this is a new install and thus there is nothing to do return nil } - if strings.Contains(string(configContents), "enable_control_r_search") { - // control-r search is already configured, so there is nothing to do - return nil - } - // Enable control-r search config, err := hctx.GetConfig() if err != nil { return err } - config.ControlRSearchEnabled = true + if !strings.Contains(string(configContents), "enable_control_r_search") { + // control-r search is not yet configured, so enable it + config.ControlRSearchEnabled = true + } + if !strings.Contains(string(configContents), "highlight_matches") { + // highlighting is not yet configured, so enable it + config.HighlightMatches = true + } return hctx.SetConfig(&config) } diff --git a/client/hctx/hctx.go b/client/hctx/hctx.go index 805a822..c323de4 100644 --- a/client/hctx/hctx.go +++ b/client/hctx/hctx.go @@ -195,6 +195,8 @@ type ClientConfig struct { // Beta mode, enables unspecified additional beta features // Currently: This enables pre-saving of history entries to better handle long-running commands BetaMode bool `json:"beta_mode"` + // Whether to highlight matches in search results + HighlightMatches bool `json:"highlight_matches"` } type CustomColumnDefinition struct { diff --git a/client/tui/tui.go b/client/tui/tui.go index 20a803c..f0b9a2a 100644 --- a/client/tui/tui.go +++ b/client/tui/tui.go @@ -583,7 +583,7 @@ func makeTable(ctx context.Context, rows []table.Row) (table.Model, error) { Foreground(lipgloss.Color("229")). Background(lipgloss.Color("57")). Bold(false) - if config.BetaMode { + if config.HighlightMatches { MATCH_NOTHING_REGEXP := regexp.MustCompile("a^") s.RenderCell = func(model table.Model, value string, position table.CellPosition) string { var re *regexp.Regexp