Created
November 10, 2025 14:39
-
-
Save mkatychev/5a9740e17da09e3e3114cb2274b699c9 to your computer and use it in GitHub Desktop.
Nushell status prompt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def git-icon [ | |
| colour # ansi colour argument | |
| one # icon for a match quantity of one | |
| many # icon for a match quantity of two or more | |
| ]: int -> oneof<string, nothing> { | |
| let icon = match $in { | |
| 1 => $one | |
| 2.. => $many | |
| _ => (return null) | |
| } | |
| $"(ansi $colour)($icon)" | |
| } | |
| # requires `nu_plugin_gstat` https://www.nushell.sh/commands/docs/gstat.html | |
| export def git-state []: nothing -> string { | |
| plugin use gstat | |
| let git = gstat | |
| # we're not in a git repo, return early | |
| if $git.ahead == -1 { | |
| return "" | |
| } | |
| mut git_status = [ | |
| $"(ansi teal)($git.branch)" | |
| # https://www.nerdfonts.com/cheat-sheet nerdfonts checkbox icons | |
| ($git.wt_untracked | git-icon yellow ) | |
| ($git.wt_modified | git-icon red ) | |
| ($git.idx_added_staged + $git.idx_modified_staged | git-icon green ) | |
| ] | compact | str join ' ' | |
| return $"(ansi yellow)($git_status)(ansi reset)" | |
| } | |
| # whether the last command was successful, green is exit code of 0, red for everything else | |
| export def last-exit-status [icon] { | |
| let exit_colour = if $env.LAST_EXIT_CODE == 0 { ansi green } else { ansi red } | |
| $"($exit_colour)($icon)" | |
| } | |
| # show current prompt path, showing path relative to home directory | |
| export def path [] { | |
| let dir = match (do -i { $env.PWD | path relative-to $nu.home-path }) { | |
| null => $env.PWD | |
| '' => '~' | |
| $relative_pwd => { | |
| let $dir_list = $relative_pwd | path split | [~ ...$in] | |
| if ($dir_list | length) <= 3 { | |
| $dir_list | path join | |
| } else { | |
| $dir_list | last 2 | path join | |
| } | |
| } | |
| } | |
| let path_color = (if (is-admin) { ansi red_bold } else { ansi yellow_bold }) | |
| let separator_color = (if (is-admin) { ansi light_red_bold } else { ansi light_green_bold }) | |
| let path_segment = $"($path_color)($dir)(ansi reset)" | |
| $path_segment | str replace --all (char path_sep) $"($separator_color)(char path_sep)($path_color)" | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment