Skip to content

Commit

Permalink
Merge branch 'master' into quote-init
Browse files Browse the repository at this point in the history
  • Loading branch information
ddworken authored Oct 20, 2024
2 parents 4d06c7e + c58f28a commit 15bc95c
Show file tree
Hide file tree
Showing 13 changed files with 270 additions and 25 deletions.
17 changes: 15 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,8 @@ hishtory config-set filter-duplicate-commands true

If you don't need the ability to sync your shell history, you can install hiSHtory in offline mode:

```
curl https://hishtory.dev/install.py | HISHTORY_OFFLINE=true python3 -
```sh
curl https://hishtory.dev/install.py | python3 - --offline
```

This disables syncing completely so that the client will not rely on the hiSHtory backend at all. You can also change the syncing status via `hishtory syncing enable` or `hishtory syncing disable`.
Expand Down Expand Up @@ -199,6 +199,19 @@ You can configure a custom timestamp format for hiSHtory via `hishtory config-se

</blockquote></details>

<details>
<summary>Custom rendering</summary><blockquote>

By default, hiHStory tries to render the TUI in a reasonable way that balances terminal space consumption and TUI usability. If you find that you wish to customize this behavior, there are two config options that you can experiment with enabling:

```
hishtory config-set compact-mode true # Renders the TUI in "compact mode" with less whitespace
hishtory config-set full-screen true # Renders the TUI in "full-screen mode" so that it uses the entire terminal
```

</blockquote></details>


<details>
<summary>Web UI for sharing</summary><blockquote>

Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
312
314
3 changes: 3 additions & 0 deletions backend/web/landing/www/install.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ def get_executable_tmpdir():
cmd = tmpFilePath + ' install'
if os.environ.get('HISHTORY_OFFLINE'):
cmd += " --offline"
additional_flags = [flag for flag in sys.argv[1:] if flag.startswith("-") and flag != "-" and flag != "--"]
if additional_flags:
cmd += " " + " ".join(additional_flags)
exitCode = os.system(cmd)
os.remove(tmpFilePath)
if exitCode != 0:
Expand Down
71 changes: 61 additions & 10 deletions client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ func TestParam(t *testing.T) {
t.Run("testTui/ai", wrapTestForSharding(testTui_ai))
t.Run("testTui/defaultFilter", wrapTestForSharding(testTui_defaultFilter))
t.Run("testTui/escaping", wrapTestForSharding(testTui_escaping))
t.Run("testTui/fullscreen", wrapTestForSharding(testTui_fullscreen))

// Assert there are no leaked connections
assertNoLeakedConnections(t)
Expand Down Expand Up @@ -838,13 +839,13 @@ func testHishtoryBackgroundSaving(t *testing.T, tester shellTester) {
// Setup
defer testutils.BackupAndRestore(t)()

// Check that we can find the go binary
_, err := exec.LookPath("go")
// Check that we can find the go binary and use that path to it for consistency
goBinPath, err := exec.LookPath("go")
require.NoError(t, err)

// Test install with an unset HISHTORY_TEST var so that we save in the background (this is likely to be flakey!)
out := tester.RunInteractiveShell(t, `unset HISHTORY_TEST
CGO_ENABLED=0 go build -o /tmp/client
CGO_ENABLED=0 `+goBinPath+` build -o /tmp/client
/tmp/client install`)
r := regexp.MustCompile(`Setting secret hishtory key to (.*)`)
matches := r.FindStringSubmatch(out)
Expand Down Expand Up @@ -1063,18 +1064,16 @@ func TestInstallViaPythonScriptWithCustomHishtoryPath(t *testing.T) {
require.NoError(t, err)
require.NoError(t, os.RemoveAll(path.Join(homedir, altHishtoryPath)))

testInstallViaPythonScriptChild(t, zshTester{})
testInstallViaPythonScriptChild(t, zshTester{}, Online)
}

func TestInstallViaPythonScriptInOfflineMode(t *testing.T) {
markTestForSharding(t, 1)
defer testutils.BackupAndRestore(t)()
defer testutils.BackupAndRestoreEnv("HISHTORY_OFFLINE")()
os.Setenv("HISHTORY_OFFLINE", "1")
tester := zshTester{}

// Check that installing works
testInstallViaPythonScriptChild(t, tester)
testInstallViaPythonScriptChild(t, tester, Offline)

// And check that it installed in offline mode
out := tester.RunInteractiveShell(t, `hishtory status -v`)
Expand All @@ -1083,14 +1082,14 @@ func TestInstallViaPythonScriptInOfflineMode(t *testing.T) {

func testInstallViaPythonScript(t *testing.T, tester shellTester) {
defer testutils.BackupAndRestore(t)()
testInstallViaPythonScriptChild(t, tester)
testInstallViaPythonScriptChild(t, tester, Online)

// And check that it installed in online mode
out := tester.RunInteractiveShell(t, `hishtory status -v`)
require.Contains(t, out, "\nSync Mode: Enabled\n")
}

func testInstallViaPythonScriptChild(t *testing.T, tester shellTester) {
func testInstallViaPythonScriptChild(t *testing.T, tester shellTester, onlineStatus OnlineStatus) {
if !testutils.IsOnline() {
t.Skip("skipping because we're currently offline")
}
Expand All @@ -1099,7 +1098,11 @@ func testInstallViaPythonScriptChild(t *testing.T, tester shellTester) {
defer testutils.BackupAndRestoreEnv("HISHTORY_TEST")()

// Install via the python script
out := tester.RunInteractiveShell(t, `curl https://hishtory.dev/install.py | python3 -`)
additionalFlags := " "
if onlineStatus == Offline {
additionalFlags = "--offline"
}
out := tester.RunInteractiveShell(t, `curl https://hishtory.dev/install.py | python3 - `+additionalFlags)
require.Contains(t, out, "Succesfully installed hishtory")
r := regexp.MustCompile(`Setting secret hishtory key to (.*)`)
matches := r.FindStringSubmatch(out)
Expand Down Expand Up @@ -1869,6 +1872,54 @@ func testTui_escaping(t *testing.T) {
testutils.CompareGoldens(t, out, "TestTui-Escaping")
}

func testTui_fullscreen(t *testing.T) {
// Setup
defer testutils.BackupAndRestore(t)()
tester, _, _ := setupTestTui(t, Online)

// By default full-screen mode is disabled
require.Equal(t, "false", strings.TrimSpace(tester.RunInteractiveShell(t, `hishtory config-get full-screen`)))
require.Equal(t, "false", strings.TrimSpace(tester.RunInteractiveShell(t, `hishtory config-get compact-mode`)))

// Test that we can enable it
tester.RunInteractiveShell(t, `hishtory config-set full-screen true`)
require.Equal(t, "true", strings.TrimSpace(tester.RunInteractiveShell(t, `hishtory config-get full-screen`)))

// Test that it renders in full-screen mode taking up the entire terminal
out := captureTerminalOutput(t, tester, []string{
"echo foo ENTER",
"hishtory SPACE tquery ENTER",
})
testutils.CompareGoldens(t, out, "TestTui-FullScreenRender")

// Test that it clears full-screen mode and restores the original terminal state
out = captureTerminalOutput(t, tester, []string{
"echo SPACE foo ENTER",
"hishtory SPACE tquery ENTER",
"Escape",
})
require.Contains(t, out, "echo foo\n")
require.Contains(t, out, "hishtory tquery\n")
require.NotContains(t, out, "Search Query")
require.True(t, len(strings.Split(out, "\n")) <= 7)

// Test that it renders the help page fine
out = captureTerminalOutput(t, tester, []string{
"echo SPACE foo ENTER",
"hishtory SPACE tquery ENTER",
"C-h",
})
testutils.CompareGoldens(t, out, "TestTui-FullScreenHelp")

// Test that it renders fine in full-screen mode and compact-mode
tester.RunInteractiveShell(t, `hishtory config-set compact-mode true`)
out = captureTerminalOutput(t, tester, []string{
"echo foo ENTER",
"hishtory SPACE tquery ENTER",
})
testutils.CompareGoldens(t, out, "TestTui-FullScreenCompactRender")
}

func testTui_defaultFilter(t *testing.T) {
// Setup
defer testutils.BackupAndRestore(t)()
Expand Down
11 changes: 11 additions & 0 deletions client/cmd/configGet.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ func init() {
configGetCmd.AddCommand(getAiCompletionEndpoint)
configGetCmd.AddCommand(getCompactMode)
configGetCmd.AddCommand(getLogLevelCmd)
configGetCmd.AddCommand(getFullScreenCmd)
}

var getLogLevelCmd = &cobra.Command{
Expand All @@ -195,3 +196,13 @@ var getLogLevelCmd = &cobra.Command{
fmt.Println(config.LogLevel.String())
},
}

var getFullScreenCmd = &cobra.Command{
Use: "full-screen",
Short: "Get whether or not hishtory is configured to run in full-screen mode",
Run: func(cmd *cobra.Command, args []string) {
ctx := hctx.MakeContext()
config := hctx.GetConf(ctx)
fmt.Println(config.FullScreenRendering)
},
}
14 changes: 14 additions & 0 deletions client/cmd/configSet.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,19 @@ var setLogLevelCmd = &cobra.Command{
},
}

var setFullScreenCmd = &cobra.Command{
Use: "full-screen",
Short: "Configure whether or not hishtory is configured to run in full-screen mode",
Args: cobra.MatchAll(cobra.ExactArgs(1), cobra.OnlyValidArgs),
ValidArgs: []string{"true", "false"},
Run: func(cmd *cobra.Command, args []string) {
ctx := hctx.MakeContext()
config := hctx.GetConf(ctx)
config.FullScreenRendering = args[0] == "true"
lib.CheckFatalError(hctx.SetConfig(config))
},
}

func init() {
rootCmd.AddCommand(configSetCmd)
configSetCmd.AddCommand(setEnableControlRCmd)
Expand All @@ -277,6 +290,7 @@ func init() {
configSetCmd.AddCommand(setAiCompletionEndpoint)
configSetCmd.AddCommand(compactMode)
configSetCmd.AddCommand(setLogLevelCmd)
configSetCmd.AddCommand(setFullScreenCmd)
setColorSchemeCmd.AddCommand(setColorSchemeSelectedText)
setColorSchemeCmd.AddCommand(setColorSchemeSelectedBackground)
setColorSchemeCmd.AddCommand(setColorSchemeBorderColor)
Expand Down
3 changes: 3 additions & 0 deletions client/cmd/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ var installCmd = &cobra.Command{
if len(args) > 0 {
secretKey = args[0]
}
if strings.HasPrefix(secretKey, "-") {
lib.CheckFatalError(fmt.Errorf("secret key %#v looks like a CLI flag, please use a secret key that does not start with a -", secretKey))
}
lib.CheckFatalError(install(secretKey, *offlineInstall, *skipConfigModification))
if os.Getenv("HISHTORY_SKIP_INIT_IMPORT") == "" {
db, err := hctx.OpenLocalSqliteDb()
Expand Down
2 changes: 2 additions & 0 deletions client/hctx/hctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,8 @@ type ClientConfig struct {
KeyBindings keybindings.SerializableKeyMap `json:"key_bindings"`
// The log level for hishtory (e.g., "debug", "info", "warn", "error")
LogLevel logrus.Level `json:"log_level"`
// Whether the TUI should render in full-screen mode
FullScreenRendering bool `json:"full_screen_rendering"`
}

type ColorScheme struct {
Expand Down
4 changes: 3 additions & 1 deletion client/lib/config.sh
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ function __hishtory_postcommand() {

if [ -n "${HISHTORY_FIRST_PROMPT:-}" ]; then
unset HISHTORY_FIRST_PROMPT
return
return $EXIT_CODE
fi

# Run after every prompt
Expand All @@ -57,6 +57,8 @@ function __hishtory_postcommand() {
LAST_SAVED_COMMAND=$CMD

(hishtory updateLocalDbFromRemote &)

return $EXIT_CODE
}
PROMPT_COMMAND="__hishtory_postcommand; $PROMPT_COMMAND"
export HISTTIMEFORMAT=$HISTTIMEFORMAT
Expand Down
40 changes: 40 additions & 0 deletions client/testdata/TestTui-FullScreenCompactRender
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
Search Query: > ls
┌────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│ Hostname CWD Timestamp Runtime Exit Code Command │
│────────────────────────────────────────────────────────────────────────────────────────────────────────│
│ localhost /tmp/ Oct 17 2022 21:43:21 PDT 3s 2 echo 'aaaaaa bbbb' │
│ localhost /tmp/ Oct 17 2022 21:43:16 PDT 3s 2 ls ~/ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
└────────────────────────────────────────────────────────────────────────────────────────────────────────┘
Loading

0 comments on commit 15bc95c

Please sign in to comment.