Simplify prompt tilde substitution (#11822)

# Description

Simplify the tilde substitution in `create_left_prompt` by using `path
relative-to`.

# User-Facing Changes

The default `env.nu` is simpler.

# Tests

I've been happily using this formulation in my prompt command for a
while now.
This commit is contained in:
Christopher Durham 2024-02-13 07:28:52 -05:00 committed by GitHub
parent da4c918392
commit 1bf016bae3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -3,23 +3,11 @@
# version = "0.90.2"
def create_left_prompt [] {
let home = $nu.home-path
# Perform tilde substitution on dir
# To determine if the prefix of the path matches the home dir, we split the current path into
# segments, and compare those with the segments of the home dir. In cases where the current dir
# is a parent of the home dir (e.g. `/home`, homedir is `/home/user`), this comparison will
# also evaluate to true. Inside the condition, we attempt to str replace `$home` with `~`.
# Inside the condition, either:
# 1. The home prefix will be replaced
# 2. The current dir is a parent of the home dir, so it will be uneffected by the str replace
let dir = (
if ($env.PWD | path split | zip ($home | path split) | all { $in.0 == $in.1 }) {
($env.PWD | str replace $home "~")
} else {
$env.PWD
}
)
let dir = match (do --ignore-shell-errors { $env.PWD | path relative-to $nu.home-path }) {
null => $env.PWD
'' => '~'
$relative_pwd => ([~ $relative_pwd] | path join)
}
let path_color = (if (is-admin) { ansi red_bold } else { ansi green_bold })
let separator_color = (if (is-admin) { ansi light_red_bold } else { ansi light_green_bold })