From 470aeb09a72936a66937d68f96165a977b5c213d Mon Sep 17 00:00:00 2001 From: Antoine Stevan <44101798+amtoine@users.noreply.github.com> Date: Mon, 12 Jun 2023 11:02:46 +0200 Subject: [PATCH] feat: update the nushell init file and make it valid module and overlay (#5188) * break long commands into multiple lines for readability * fix the format of closures We generally write `{|| ...}` instead of `{ || ...}`. * remove the `$"--opt=(val)"` structure when possible `starship` does not require to use `--opt=val` and so we do not need to do that with Nushell :) the only place where this is required is with `--status` because `$env.LAST_EXIT_CODE` can be negative and `starship` does not appear to *like* values of the form `-2`... so i left this line as it was. on the other hand, `$env.CMD_DURATION_MS` and `(term size).columns` should be fine :relieved: * simplify the `config` mutation with new `?` syntax This is a new very handy feature of Nushell which gives a much simpler command combined with `default` and `merge`. * put all `let-env`s inside an `export-env` with `load-env` This commit has two reasons of existing: - i think it makes it a bit easier to read with less `let-env`s - it transforms the *script* into both a valid module and a valid overlay * bump the version to `0.78` in to docs * add a note about the init file being also a module to all docs * tweak the documentation * update the Nushell part of the install script * format the vuepress config file as previous commit 117580136d3da5e00a4ccb7ada508c8510a2e50e was not successful, let's try to make the CI happy manually :relieved: * remove code quotes in the `config_cmd` of Nushell * format the style in the Nushell `warning` section --- README.md | 4 +-- docs/.vuepress/config.ts | 6 ++--- docs/README.md | 5 ++-- install/install.sh | 11 ++++---- src/init/starship.nu | 58 ++++++++++++++++++++++++---------------- 5 files changed, 49 insertions(+), 35 deletions(-) diff --git a/README.md b/README.md index b12c1a27..96fa1e00 100644 --- a/README.md +++ b/README.md @@ -346,10 +346,10 @@ starship init nu | save -f ~/.cache/starship/init.nu And add the following to the end of your Nushell configuration (find it by running `$nu.config-path`): ```sh -source ~/.cache/starship/init.nu +use ~/.cache/starship/init.nu ``` -Note: Only Nushell v0.73+ is supported +Note: Only Nushell v0.78+ is supported diff --git a/docs/.vuepress/config.ts b/docs/.vuepress/config.ts index 393a2bfa..38d18333 100644 --- a/docs/.vuepress/config.ts +++ b/docs/.vuepress/config.ts @@ -118,14 +118,14 @@ module.exports = defineConfig({ ["meta", { name: "twitter:alt", content: "Starship: Cross-Shell Prompt" }], // Google Analytics [ - 'script', + "script", { async: true, - src: 'https://www.googletagmanager.com/gtag/js?id=G-N3M0VJ9NL6', + src: "https://www.googletagmanager.com/gtag/js?id=G-N3M0VJ9NL6", }, ], [ - 'script', + "script", {}, "window.dataLayer = window.dataLayer || [];\nfunction gtag(){dataLayer.push(arguments);}\ngtag('js', new Date());\ngtag('config', 'G-N3M0VJ9NL6');", ], diff --git a/docs/README.md b/docs/README.md index 0665a431..747b38fb 100644 --- a/docs/README.md +++ b/docs/README.md @@ -138,7 +138,7 @@ description: Starship is the minimal, blazing fast, and extremely customizable p ::: warning This will change in the future. - Only Nushell v0.73+ is supported. + Only Nushell v0.78+ is supported. ::: @@ -151,8 +151,9 @@ description: Starship is the minimal, blazing fast, and extremely customizable p And add the following to the end of your Nushell configuration (find it by running `$nu.config-path`): ```sh - source ~/.cache/starship/init.nu + use ~/.cache/starship/init.nu ``` + #### Xonsh Add the following to the end of `~/.xonshrc`: diff --git a/install/install.sh b/install/install.sh index 6c1f9a2a..494a9cbf 100755 --- a/install/install.sh +++ b/install/install.sh @@ -337,12 +337,13 @@ print_install() { nushell ) # shellcheck disable=SC2088 config_file="${BOLD}your nu config file${NO_COLOR} (find it by running ${BOLD}\$nu.config-path${NO_COLOR} in Nushell)" - config_cmd="mkdir ~/.cache/starship - starship init nu | save -f ~/.cache/starship/init.nu - source ~/.cache/starship/init.nu" + config_cmd="use ~/.cache/starship/init.nu" warning="${warning} This will change in the future. - Only Nushell v0.73 or higher is supported. - Add the following to the end of ${BOLD}your Nushell env file${NO_COLOR} (find it by running ${BOLD}\$nu.env-path${NO_COLOR} in Nushell): \"mkdir ~/.cache/starship; starship init nu | save -f ~/.cache/starship/init.nu\"" + Only Nushell v0.78 or higher is supported. + Add the following to the end of ${BOLD}your Nushell env file${NO_COLOR} (find it by running ${BOLD}\$nu.env-path${NO_COLOR} in Nushell): + + mkdir ~/.cache/starship + starship init nu | save -f ~/.cache/starship/init.nu" ;; esac printf " %s\n %s\n And add the following to the end of %s:\n\n\t%s\n\n" \ diff --git a/src/init/starship.nu b/src/init/starship.nu index fd8729d2..0601e180 100644 --- a/src/init/starship.nu +++ b/src/init/starship.nu @@ -1,27 +1,39 @@ -let-env STARSHIP_SHELL = "nu" -let-env STARSHIP_SESSION_KEY = (random chars -l 16) -let-env PROMPT_MULTILINE_INDICATOR = (^::STARSHIP:: prompt --continuation) +# this file is both a valid +# - overlay which can be loaded with `overlay use starship.nu` +# - module which can be used with `use starship.nu` +# - script which can be used with `source starship.nu` +export-env { load-env { + STARSHIP_SHELL: "nu" + STARSHIP_SESSION_KEY: (random chars -l 16) + PROMPT_MULTILINE_INDICATOR: ( + ^::STARSHIP:: prompt --continuation + ) -# Does not play well with default character module. -# TODO: Also Use starship vi mode indicators? -let-env PROMPT_INDICATOR = "" + # Does not play well with default character module. + # TODO: Also Use starship vi mode indicators? + PROMPT_INDICATOR: "" -let-env PROMPT_COMMAND = { || - # jobs are not supported - let width = (term size).columns - ^::STARSHIP:: prompt $"--cmd-duration=($env.CMD_DURATION_MS)" $"--status=($env.LAST_EXIT_CODE)" $"--terminal-width=($width)" -} + PROMPT_COMMAND: {|| + # jobs are not supported + ( + ^::STARSHIP:: prompt + --cmd-duration $env.CMD_DURATION_MS + $"--status=($env.LAST_EXIT_CODE)" + --terminal-width (term size).columns + ) + } -# Whether we have config items -let has_config_items = (not ($env | get -i config | is-empty)) + config: ($env.config? | default {} | merge { + render_right_prompt_on_last_line: true + }) -let-env config = if $has_config_items { - $env.config | upsert render_right_prompt_on_last_line true -} else { - {render_right_prompt_on_last_line: true} -} - -let-env PROMPT_COMMAND_RIGHT = { || - let width = (term size).columns - ^::STARSHIP:: prompt --right $"--cmd-duration=($env.CMD_DURATION_MS)" $"--status=($env.LAST_EXIT_CODE)" $"--terminal-width=($width)" -} + PROMPT_COMMAND_RIGHT: {|| + ( + ^::STARSHIP:: prompt + --right + --cmd-duration $env.CMD_DURATION_MS + $"--status=($env.LAST_EXIT_CODE)" + --terminal-width (term size).columns + ) + } +}}