fix(nu): use native right prompt command

resolves #4237

this also avoids renderibng the right prompt twice when the shell
script uses the `oh-my-posh print right` command
This commit is contained in:
Jan De Dobbeleer 2023-09-15 08:17:53 +02:00 committed by Jan De Dobbeleer
parent 81fd4fa5e6
commit b887d13ccd
4 changed files with 43 additions and 26 deletions

View file

@ -42,6 +42,17 @@ func (e *Engine) Primary() string {
cancelNewline = !didRender cancelNewline = !didRender
} }
// only render rprompt for shells where we need it from the primary prompt
renderRPrompt := true
switch e.Env.Shell() {
case shell.ELVISH, shell.FISH, shell.NU, shell.XONSH, shell.CMD:
renderRPrompt = false
}
if block.Type == RPrompt && !renderRPrompt {
continue
}
if e.renderBlock(block, cancelNewline) { if e.renderBlock(block, cancelNewline) {
didRender = true didRender = true
} }
@ -79,7 +90,7 @@ func (e *Engine) Primary() string {
prompt := fmt.Sprintf("PS1=\"%s\"", strings.ReplaceAll(e.string(), `"`, `\"`)) prompt := fmt.Sprintf("PS1=\"%s\"", strings.ReplaceAll(e.string(), `"`, `\"`))
prompt += fmt.Sprintf("\nRPROMPT=\"%s\"", e.rprompt) prompt += fmt.Sprintf("\nRPROMPT=\"%s\"", e.rprompt)
return prompt return prompt
case shell.PWSH, shell.PWSH5, shell.GENERIC, shell.NU: case shell.PWSH, shell.PWSH5, shell.GENERIC:
e.writeRPrompt() e.writeRPrompt()
case shell.BASH: case shell.BASH:
space, OK := e.canWriteRightBlock(true) space, OK := e.canWriteRightBlock(true)

View file

@ -1,30 +1,36 @@
export-env { $env.config = ($env.config | upsert render_right_prompt_on_last_line true)
$env.POWERLINE_COMMAND = 'oh-my-posh'
$env.POSH_THEME = ::CONFIG::
$env.PROMPT_INDICATOR = ""
$env.POSH_PID = (random uuid)
# By default displays the right prompt on the first line
# making it annoying when you have a multiline prompt
# making the behavior different compared to other shells
$env.PROMPT_COMMAND_RIGHT = ''
$env.POSH_SHELL_VERSION = (version | get version)
# PROMPTS $env.POWERLINE_COMMAND = 'oh-my-posh'
$env.PROMPT_MULTILINE_INDICATOR = (^::OMP:: print secondary $"--config=($env.POSH_THEME)" --shell=nu $"--shell-version=($env.POSH_SHELL_VERSION)") $env.POSH_THEME = ::CONFIG::
$env.PROMPT_INDICATOR = ""
$env.POSH_PID = (random uuid)
$env.POSH_SHELL_VERSION = (version | get version)
$env.PROMPT_COMMAND = { || # PROMPTS
# We have to do this because the initial value of `$env.CMD_DURATION_MS` is always `0823`, $env.PROMPT_MULTILINE_INDICATOR = (^::OMP:: print secondary $"--config=($env.POSH_THEME)" --shell=nu $"--shell-version=($env.POSH_SHELL_VERSION)")
# which is an official setting.
# 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 $env.PROMPT_COMMAND = { ||
# this obviously isn't bulletproof, but it's a start # We have to do this because the initial value of `$env.CMD_DURATION_MS` is always `0823`,
let clear = (history | last 1 | get 0.command) == "clear" # which is an official setting.
# 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 }
let width = ((term size).columns | into string) # hack to set the cursor line to 1 when the user clears the screen
^::OMP:: print primary $"--config=($env.POSH_THEME)" --shell=nu $"--shell-version=($env.POSH_SHELL_VERSION)" $"--execution-time=($cmd_duration)" $"--status=($env.LAST_EXIT_CODE)" $"--terminal-width=($width)" $"--cleared=($clear)" # 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.POSH_SHELL_VERSION)" $"--execution-time=($cmd_duration)" $"--status=($env.LAST_EXIT_CODE)" $"--terminal-width=($width)" $"--cleared=($clear)"
}
$env.PROMPT_COMMAND_RIGHT = { ||
# We have to do this because the initial value of `$env.CMD_DURATION_MS` is always `0823`,
# which is an official setting.
# 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 }
let width = ((term size).columns | into string)
^::OMP:: print right $"--config=($env.POSH_THEME)" --shell=nu $"--shell-version=($env.POSH_SHELL_VERSION)" $"--execution-time=($cmd_duration)" $"--status=($env.LAST_EXIT_CODE)" $"--terminal-width=($width)"
} }
if "::UPGRADE::" == "true" { if "::UPGRADE::" == "true" {

View file

@ -158,7 +158,7 @@ Once altered, reload your config for the changes to take effect.
<TabItem value="nu"> <TabItem value="nu">
:::caution :::caution
Oh My Posh requires Nushell v0.78.0 or higher. Oh My Posh requires Nushell v0.84.0 or higher.
::: :::
Adjust the Oh My Posh init line in the Nushell env file (`$nu.env-path`) by adding the `--config` flag Adjust the Oh My Posh init line in the Nushell env file (`$nu.env-path`) by adding the `--config` flag

View file

@ -104,7 +104,7 @@ exec fish
<TabItem value="nu"> <TabItem value="nu">
:::caution :::caution
Oh My Posh requires Nushell v0.78.0 or higher. Oh My Posh requires Nushell v0.84.0 or higher.
::: :::
Add the following line to the Nushell env file (`$nu.env-path`): Add the following line to the Nushell env file (`$nu.env-path`):