From c76e9fd16681e79a86cfcd353c1af80b2214a09a Mon Sep 17 00:00:00 2001 From: Jan De Dobbeleer Date: Sun, 5 Feb 2023 09:06:26 +0100 Subject: [PATCH] feat: ignore leading space on clear --- src/cli/print.go | 3 +++ src/engine/engine.go | 2 +- src/platform/shell.go | 1 + src/shell/scripts/omp.fish | 7 ++++++- src/shell/scripts/omp.nu | 6 +++++- 5 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/cli/print.go b/src/cli/print.go index e443dae2..8444a772 100644 --- a/src/cli/print.go +++ b/src/cli/print.go @@ -17,6 +17,7 @@ var ( stackCount int terminalWidth int eval bool + cleared bool command string shellVersion string @@ -58,6 +59,7 @@ var printCmd = &cobra.Command{ ShellVersion: shellVersion, Plain: plain, Primary: args[0] == "primary", + Cleared: cleared, } eng := engine.New(flags) @@ -97,6 +99,7 @@ func init() { //nolint:gochecknoinits printCmd.Flags().IntVarP(&terminalWidth, "terminal-width", "w", 0, "width of the terminal") printCmd.Flags().StringVar(&command, "command", "", "tooltip command") printCmd.Flags().BoolVarP(&plain, "plain", "p", false, "plain text output (no ANSI)") + printCmd.Flags().BoolVar(&cleared, "cleared", false, "do we have a clear terminal or not") printCmd.Flags().BoolVar(&eval, "eval", false, "output the prompt for eval") RootCmd.AddCommand(printCmd) } diff --git a/src/engine/engine.go b/src/engine/engine.go index a27cdadf..2d15e458 100644 --- a/src/engine/engine.go +++ b/src/engine/engine.go @@ -67,7 +67,7 @@ func (e *Engine) PrintPrimary() string { var cancelNewline bool if i == 0 { row, _ := e.Env.CursorPosition() - cancelNewline = e.Env.Flags().PromptCount == 1 || row == 1 + cancelNewline = e.Env.Flags().Cleared || e.Env.Flags().PromptCount == 1 || row == 1 } e.renderBlock(block, cancelNewline) } diff --git a/src/platform/shell.go b/src/platform/shell.go index c6991e8a..add8053b 100644 --- a/src/platform/shell.go +++ b/src/platform/shell.go @@ -66,6 +66,7 @@ type Flags struct { Plain bool Primary bool PromptCount int + Cleared bool } type CommandError struct { diff --git a/src/shell/scripts/omp.fish b/src/shell/scripts/omp.fish index 390cc349..b4448cab 100644 --- a/src/shell/scripts/omp.fish +++ b/src/shell/scripts/omp.fish @@ -36,7 +36,12 @@ function fish_prompt set --global --export omp_last_status_generation $status_generation end set_poshcontext - ::OMP:: print primary --config $POSH_THEME --shell fish --error $omp_status_cache --execution-time $omp_duration --stack-count $omp_stack_count --shell-version $FISH_VERSION + # validate if the user cleared the screen + set --local omp_cleared false + if test (history | head -1) = "clear" + set omp_cleared true + end + ::OMP:: print primary --config $POSH_THEME --shell fish --error $omp_status_cache --execution-time $omp_duration --stack-count $omp_stack_count --shell-version $FISH_VERSION --cleared=$omp_cleared end function fish_right_prompt diff --git a/src/shell/scripts/omp.nu b/src/shell/scripts/omp.nu index eac85b65..ef9fe835 100644 --- a/src/shell/scripts/omp.nu +++ b/src/shell/scripts/omp.nu @@ -18,7 +18,11 @@ export-env { # See https://github.com/nushell/nushell/discussions/6402#discussioncomment-3466687. let cmd_duration = if $env.CMD_DURATION_MS == "0823" { 0 } else { $env.CMD_DURATION_MS } + # hack to set the cursor line to 1 when the user clears the screen + # this obviously isn't bulletproof, but it's a start + let clear = (history | last 1 | get 0.command) == "clear" + let width = ((term size).columns | into string) - ^::OMP:: print primary $"--config=($env.POSH_THEME)" --shell=nu $"--shell-version=($env.NU_VERSION)" $"--execution-time=($cmd_duration)" $"--error=($env.LAST_EXIT_CODE)" $"--terminal-width=($width)" + ^::OMP:: print primary $"--config=($env.POSH_THEME)" --shell=nu $"--shell-version=($env.NU_VERSION)" $"--execution-time=($cmd_duration)" $"--error=($env.LAST_EXIT_CODE)" $"--terminal-width=($width)" $"--cleared=($clear)" } }