From 1bf016bae302cf2b85dd11a0c69479b82d4ba693 Mon Sep 17 00:00:00 2001 From: Christopher Durham Date: Tue, 13 Feb 2024 07:28:52 -0500 Subject: [PATCH] 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. --- .../nu-utils/src/sample_config/default_env.nu | 22 +++++-------------- 1 file changed, 5 insertions(+), 17 deletions(-) diff --git a/crates/nu-utils/src/sample_config/default_env.nu b/crates/nu-utils/src/sample_config/default_env.nu index ebcb66a30f..0133db4d0e 100644 --- a/crates/nu-utils/src/sample_config/default_env.nu +++ b/crates/nu-utils/src/sample_config/default_env.nu @@ -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 })