diff --git a/client/client_test.go b/client/client_test.go index ec6c935..6e3a9e9 100644 --- a/client/client_test.go +++ b/client/client_test.go @@ -2163,10 +2163,10 @@ func testPresaving(t *testing.T, tester shellTester) { userSecret := installHishtory(t, tester, "") manuallySubmitHistoryEntry(t, userSecret, testutils.MakeFakeHistoryEntry("table_sizing aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")) - // Enable beta-mode since presaving is behind that feature flag - require.Equal(t, "false", strings.TrimSpace(tester.RunInteractiveShell(t, `hishtory config-get beta-mode`))) - tester.RunInteractiveShell(t, `hishtory config-set beta-mode true`) - require.Equal(t, "true", strings.TrimSpace(tester.RunInteractiveShell(t, `hishtory config-get beta-mode`))) + // Enable the presaving feature + require.Equal(t, "true", strings.TrimSpace(tester.RunInteractiveShell(t, `hishtory config-get presaving`))) + tester.RunInteractiveShell(t, `hishtory config-set presaving true`) + require.Equal(t, "true", strings.TrimSpace(tester.RunInteractiveShell(t, `hishtory config-get presaving`))) // Start a command that will take a long time to execute in the background, so // we can check that it was recorded even though it never finished. diff --git a/client/cmd/configGet.go b/client/cmd/configGet.go index c3d47ae..8589930 100644 --- a/client/cmd/configGet.go +++ b/client/cmd/configGet.go @@ -61,6 +61,17 @@ var getEnableAiCompletion = &cobra.Command{ }, } +var getPresavingCmd = &cobra.Command{ + Use: "presaving", + Short: "Enable 'presaving' of shell entries that never finish running", + Long: "If enabled, there is a slight risk of duplicate history entries. If disabled, non-terminating history entries will not be recorded.", + Run: func(cmd *cobra.Command, args []string) { + ctx := hctx.MakeContext() + config := hctx.GetConf(ctx) + fmt.Println(config.EnablePresaving) + }, +} + var getBetaModeCmd = &cobra.Command{ Use: "beta-mode", Short: "Enable beta-mode to opt-in to unreleased features", @@ -121,4 +132,5 @@ func init() { configGetCmd.AddCommand(getBetaModeCmd) configGetCmd.AddCommand(getHighlightMatchesCmd) configGetCmd.AddCommand(getEnableAiCompletion) + configGetCmd.AddCommand(getPresavingCmd) } diff --git a/client/cmd/configSet.go b/client/cmd/configSet.go index ab6ca06..cf1fedc 100644 --- a/client/cmd/configSet.go +++ b/client/cmd/configSet.go @@ -87,6 +87,23 @@ var setEnableAiCompletionCmd = &cobra.Command{ lib.CheckFatalError(hctx.SetConfig(config)) }, } +var setPresavingCmd = &cobra.Command{ + Use: "presaving", + Short: "Enable 'presaving' of shell entries that never finish running", + Long: "If enabled, there is a slight risk of duplicate history entries. If disabled, non-terminating history entries will not be recorded.", + 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.EnablePresaving = (val == "true") + lib.CheckFatalError(hctx.SetConfig(config)) + }, +} var setHighlightMatchesCmd = &cobra.Command{ Use: "highlight-matches", @@ -138,4 +155,5 @@ func init() { configSetCmd.AddCommand(setBetaModeCommand) configSetCmd.AddCommand(setHighlightMatchesCmd) configSetCmd.AddCommand(setEnableAiCompletionCmd) + configSetCmd.AddCommand(setPresavingCmd) } diff --git a/client/cmd/install.go b/client/cmd/install.go index 2e2309b..4285d2c 100644 --- a/client/cmd/install.go +++ b/client/cmd/install.go @@ -218,6 +218,10 @@ func handleUpgradedFeatures() error { // highlighting is not yet configured, so enable it config.HighlightMatches = true } + if !strings.Contains(string(configContents), "enable_presaving") { + // Presaving is not yet configured, so enable it + config.HighlightMatches = true + } if !strings.Contains(string(configContents), "ai_completion") { // AI completion is not yet configured, disable it for upgrades since this is a new feature config.AiCompletion = false @@ -569,6 +573,7 @@ func setup(userSecret string, isOffline bool) error { // TODO: Set config.HighlightMatches = true here, so that we enable highlighting by default config.AiCompletion = true config.IsOffline = isOffline + config.EnablePresaving = true err := hctx.SetConfig(&config) if err != nil { return fmt.Errorf("failed to persist config to disk: %w", err) diff --git a/client/cmd/saveHistoryEntry.go b/client/cmd/saveHistoryEntry.go index f2e56bb..483d570 100644 --- a/client/cmd/saveHistoryEntry.go +++ b/client/cmd/saveHistoryEntry.go @@ -142,7 +142,7 @@ func presaveHistoryEntry(ctx context.Context) { if !config.IsEnabled { return } - if !config.BetaMode { + if !config.EnablePresaving { return } @@ -195,7 +195,7 @@ func saveHistoryEntry(ctx context.Context) { db := hctx.GetDb(ctx) // Drop any entries from pre-saving since they're no longer needed - if config.BetaMode { + if config.EnablePresaving { lib.CheckFatalError(deletePresavedEntries(ctx, entry, false)) } @@ -220,7 +220,7 @@ func saveHistoryEntry(ctx context.Context) { } } - if config.BetaMode { + if config.EnablePresaving { db.Commit() } } diff --git a/client/hctx/hctx.go b/client/hctx/hctx.go index 2b55ade..c5ed605 100644 --- a/client/hctx/hctx.go +++ b/client/hctx/hctx.go @@ -199,6 +199,8 @@ type ClientConfig struct { HighlightMatches bool `json:"highlight_matches"` // Whether to enable AI completion AiCompletion bool `json:"ai_completion"` + // Whether to enable presaving + EnablePresaving bool `json:"enable_presaving"` } type CustomColumnDefinition struct { diff --git a/client/lib/lib.go b/client/lib/lib.go index a60ee73..426dcd8 100644 --- a/client/lib/lib.go +++ b/client/lib/lib.go @@ -840,7 +840,8 @@ func retryingSearch(ctx context.Context, db *gorm.DB, query string, limit int, c if err != nil { return nil, err } - if hctx.GetConf(ctx).BetaMode { + if hctx.GetConf(ctx).EnablePresaving { + // Sort by StartTime when presaving is enabled, since presaved entries may not have an end time tx = tx.Order("start_time DESC") } else { tx = tx.Order("end_time DESC")