From 1bde6f929bf83b5294aeb5cc36e703f221954259 Mon Sep 17 00:00:00 2001 From: Jan De Dobbeleer Date: Wed, 18 Oct 2023 10:58:13 +0200 Subject: [PATCH] feat(nu): transient prompt --- src/shell/scripts/omp.nu | 33 ++++++++++++++---------- website/docs/configuration/transient.mdx | 2 +- website/docs/installation/customize.mdx | 2 +- website/docs/installation/prompt.mdx | 2 +- 4 files changed, 22 insertions(+), 17 deletions(-) diff --git a/src/shell/scripts/omp.nu b/src/shell/scripts/omp.nu index d52e369a..0300bccc 100644 --- a/src/shell/scripts/omp.nu +++ b/src/shell/scripts/omp.nu @@ -6,31 +6,36 @@ $env.PROMPT_INDICATOR = "" $env.POSH_PID = (random uuid) $env.POSH_SHELL_VERSION = (version | get version) +def posh_cmd_duration [] { + # 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. + if $env.CMD_DURATION_MS == "0823" { 0 } else { $env.CMD_DURATION_MS } +} + +def posh_width [] { + (term size).columns | into string +} + # PROMPTS $env.PROMPT_MULTILINE_INDICATOR = (^::OMP:: print secondary $"--config=($env.POSH_THEME)" --shell=nu $"--shell-version=($env.POSH_SHELL_VERSION)") $env.PROMPT_COMMAND = { || - # 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 } - # 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.POSH_SHELL_VERSION)" $"--execution-time=($cmd_duration)" $"--status=($env.LAST_EXIT_CODE)" $"--terminal-width=($width)" $"--cleared=($clear)" + ^::OMP:: print primary $"--config=($env.POSH_THEME)" --shell=nu $"--shell-version=($env.POSH_SHELL_VERSION)" $"--execution-time=(posh_cmd_duration)" $"--status=($env.LAST_EXIT_CODE)" $"--terminal-width=(posh_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 } +$env.PROMPT_COMMAND_RIGHT = { || + ^::OMP:: print right $"--config=($env.POSH_THEME)" --shell=nu $"--shell-version=($env.POSH_SHELL_VERSION)" $"--execution-time=(posh_cmd_duration)" $"--status=($env.LAST_EXIT_CODE)" $"--terminal-width=(posh_width)" +} - 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 "::TRANSIENT::" == "true" { + $env.TRANSIENT_PROMPT_COMMAND = { || + ^::OMP:: print transient $"--config=($env.POSH_THEME)" --shell=nu $"--shell-version=($env.POSH_SHELL_VERSION)" $"--execution-time=(posh_cmd_duration)" $"--status=($env.LAST_EXIT_CODE)" $"--terminal-width=(posh_width)" + } } if "::UPGRADE::" == "true" { diff --git a/website/docs/configuration/transient.mdx b/website/docs/configuration/transient.mdx index 0a8476e4..74b8e7b0 100644 --- a/website/docs/configuration/transient.mdx +++ b/website/docs/configuration/transient.mdx @@ -8,7 +8,7 @@ import Tabs from "@theme/Tabs"; import TabItem from "@theme/TabItem"; :::info -This feature only works in `fish`, `zsh`, `powershell` (`ConstrainedLanguage` mode unsupported) and `cmd` for the time being. +This feature only works in `nu`, `fish`, `zsh`, `powershell` (`ConstrainedLanguage` mode unsupported) and `cmd` for the time being. ::: Transient prompt, when enabled, replaces the prompt with a simpler one to allow more screen real estate. diff --git a/website/docs/installation/customize.mdx b/website/docs/installation/customize.mdx index 07643348..bc6317f1 100644 --- a/website/docs/installation/customize.mdx +++ b/website/docs/installation/customize.mdx @@ -158,7 +158,7 @@ Once altered, reload your config for the changes to take effect. :::caution -Oh My Posh requires Nushell v0.84.0 or higher. +Oh My Posh requires Nushell v0.86.0 or higher. ::: Adjust the Oh My Posh init line in the Nushell env file (`$nu.env-path`) by adding the `--config` flag diff --git a/website/docs/installation/prompt.mdx b/website/docs/installation/prompt.mdx index 9e95daac..777e00ab 100644 --- a/website/docs/installation/prompt.mdx +++ b/website/docs/installation/prompt.mdx @@ -104,7 +104,7 @@ exec fish :::caution -Oh My Posh requires Nushell v0.84.0 or higher. +Oh My Posh requires Nushell v0.86.0 or higher. ::: Add the following line to the Nushell env file (`$nu.env-path`):