Skip to content

Commit

Permalink
Added "Use hooks to export state via environment variables" Section i…
Browse files Browse the repository at this point in the history
…n setup.md (#1100)

* added section about env var with hooks in setup.md

* Update cookbook/setup.md

Co-authored-by: Antoine Stevan <44101798+amtoine@users.noreply.github.com>

---------

Co-authored-by: Antoine Stevan <44101798+amtoine@users.noreply.github.com>
  • Loading branch information
cptpiepmatz and amtoine committed Oct 8, 2023
1 parent 3d704a4 commit 43d5155
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions cookbook/setup.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,43 @@ or
0 │ APPDATA │ string │ C:\Users\someuser10\AppData\Roaming │ C:\Users\someuser10\AppData\Roaming
───┴─────────┴────────┴─────────────────────────────────────┴─────────────────────────────────────
```

---

### Use hooks to export state via environment variables

Additional tools like starship run with every prompt showing up in nushell.
[`starship`](https://starship.rs) in particular replaces the default prompt with
its own.
To be most compatible, the `starship` binary will run every prompt render and
is absolute stateless.
Nushell, however, is very stateful in a single instance.

[Hooks](https://www.nushell.sh/book/hooks.html#hooks) allow registration of
custom callback functions.
In this case, the `pre_prompt` hook is very useful.
With it, we can export state information as an environment variable, for
example, what [overlays](https://www.nushell.sh/book/overlays.html) are
currently activated.

```nu
# set NU_OVERLAYS with overlay list, useful for starship prompt
$env.config.hooks.pre_prompt = ($env.config.hooks.pre_prompt | append {||
let overlays = overlay list | range 1..
if not ($overlays | is-empty) {
$env.NU_OVERLAYS = $overlays | str join ", "
} else {
$env.NU_OVERLAYS = null
}
})
```

Now in `starship`, we can use this environment variable to display what modules
are active.

```toml
[env_var.NU_OVERLAYS]
symbol = '📌 '
format = 'with [$symbol($env_value )]($style)'
style = 'red'
```

0 comments on commit 43d5155

Please sign in to comment.