From 366a895a0891055aba919906b0811dc78ada89eb Mon Sep 17 00:00:00 2001 From: aaron Date: Mon, 14 Sep 2026 16:46:17 +0200 Subject: [PATCH] feature(shell): shorten the path in the agnoster prompt, if it gets too long --- modules/home-manager/shell.nix | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/modules/home-manager/shell.nix b/modules/home-manager/shell.nix index ad35c12..ccde820 100644 --- a/modules/home-manager/shell.nix +++ b/modules/home-manager/shell.nix @@ -23,6 +23,35 @@ fi ''; }; + + # Unlike oh-my-zsh.extraConfig above, this lands *after* + # `source $ZSH/oh-my-zsh.sh`, which is where the theme's prompt functions + # come into existence. + initContent = lib.mkAfter '' + # Keep the prompt on one line in deeply nested trees. + # + # Both themes render the working directory as `%~`, the full path with + # $HOME collapsed to `~`. Somewhere like + # ~/git/bfh/cas-pml-sem/documents/bericht that is 39 columns on its own, + # and together with user@host and a long branch name the line wraps. + # + # `%(4~|…/%2~|%~)` is a prompt ternary: at four or more components show + # an ellipsis and the last two, otherwise show the path whole. Short + # paths like ~/git/nixconfig are left alone. + if [[ "$ZSH_THEME" == "agnoster" ]]; then + # agnoster calls prompt_dir on every render, so redefining it is + # enough. AGNOSTER_DIR_* keep the segment's own colours. + prompt_dir() { + prompt_segment "$AGNOSTER_DIR_BG" "$AGNOSTER_DIR_FG" '%(4~|…/%2~|%~)' + } + else + # gentoo builds PROMPT once at load time, so patch the string. Its + # `%~` is the non-root branch of `%(!.%1~.%~)`, and it is the first + # literal `%~` in there, so the root branch is untouched. ASCII `...` + # because this branch is the Linux console, whose font may lack U+2026. + PROMPT="''${PROMPT/\%~/%(4~|.../%2~|%~)}" + fi + ''; }; # set important env vars -- 2.52.0