mirror of
https://github.com/nushell/nu_scripts.git
synced 2024-08-16 08:50:31 +03:00
A place to share Nushell scripts with each other
74ba060f55
Rewrite of nushell/nushell#12156 for jdx/mise#1763. ### Why? Nushell philosophically omits a `set` list mutation. But `$env` is inherently mutable leading to issues described in nushell/nushell#12148. `set-env` provides such an operation exclusively for `$env`. ### What changed? 1. Explicit flag instead of implicit list concatenation 2. Expands updates to any `$env` field not only `$env.config` ### How is it used? ```yaml ❯ set-env -h Gracefully set an environment variable or merge a nested option. Examples: Set $env.NUPM_HOME > set-env NUPM_HOME $'($nu.home-path)/.local/share/nupm' Add to $env.NU_LIB_DIRS > set-env --append NU_LIB_DIRS $'($env.NUPM_HOME)/modules' Set a nested config option > set-env config.filesize.metric true Add a config hook > set-env -a config.hooks.pre_prompt 'ellie | print' Usage: > main {flags} <field> <value> Flags: -a, --append - Append to the previous value or wrap in a new list -h, --help - Display the help message for this command Parameters: field <cell-path>: The environment variable name or nested option cell path value <any>: The value to set or append Input/output types: ╭───┬─────────┬─────────╮ │ # │ input │ output │ ├───┼─────────┼─────────┤ │ 0 │ nothing │ nothing │ ╰───┴─────────┴─────────╯ ``` ### How does it work? ```nushell export def --env main [ field: cell-path value: any --append (-a) ]: nothing -> nothing { # just an alias def 'get or' [default field] { get --ignore-errors $field | default $default } let value = if $append { # append to the previous value or empty list $env | get or [] $field | append $value } else { $value } # work around nushell/nushell#12168 let field = $field | to text | split row . let value = match $field { [_] => $value # if cell path is nested [$root, ..$field] => { let field = $field | into cell-path # reassigning $env would be an error # merging reserved names like PWD would be an error # so merge from 1 level deep instead $env | get or {} $root | upsert $field $value } } # avoid issues noted above load-env { ($field | first): $value } } ``` ### Where are the tests? Pending next PR for nupm integration. |
||
---|---|---|
aliases | ||
assets | ||
before_v0.60 | ||
benchmarks | ||
custom-completions | ||
custom-menus | ||
example-config | ||
make_release | ||
modules | ||
nu-hooks | ||
sourced | ||
stdlib-candidate | ||
themes | ||
.gitattributes | ||
.gitignore | ||
LICENSE | ||
README.md |
Nushell Scripts
This is a place to share Nushell scripts with each other. If you'd like to share your scripts, fork this repository, and create a PR that adds it to the repo.
Sections
- aliases
- benchmarks
- cool-oneliners
- custom-completions - collection of custom completions for external commands.
- custom-menus - collection of custom nushell menus
- example-config
- nu-hooks
- modules - This has its dedicated readme
- nu_101 - Beginner introduction to nushell concepts.
- prompt
- themes
Running Scripts
You can run nushell scripts in a few different ways.
- You can type
nu <script name>
. - From with nushell, you can type
source <script name>
and if the script is just a bunch of commands it will run the script. If the script is a custom command it will load those custom commands into your current scope so you can run them like any other command.