From 170857653a5b389a57f539a2f781d26b8c6ba445 Mon Sep 17 00:00:00 2001 From: Hedy Li Date: Fri, 4 Feb 2022 17:46:54 +0800 Subject: [PATCH] Fish: Better looking prompt - Prompt char being '>' instead of '$' Yes, just for the sake of having a little `~>` in my homedir :D - Different color for the base dir in the prompt pwd section - Right prompt of user@hostname and time I've had this prompt for like MONTHS and this was sitting in my uncommitted changes since then, lol. --- .config/fish/functions/fish_prompt.fish | 35 ++++++++++++++++--- .config/fish/functions/fish_right_prompt.fish | 6 ++++ .config/fish/functions/prompt_pwd.fish | 26 ++++++++++++++ 3 files changed, 63 insertions(+), 4 deletions(-) create mode 100644 .config/fish/functions/fish_right_prompt.fish create mode 100644 .config/fish/functions/prompt_pwd.fish diff --git a/.config/fish/functions/fish_prompt.fish b/.config/fish/functions/fish_prompt.fish index 96653cb..f38a83d 100644 --- a/.config/fish/functions/fish_prompt.fish +++ b/.config/fish/functions/fish_prompt.fish @@ -1,3 +1,13 @@ +# Based on top of fish's built-in "informative" shell style (see fish_config) +# I actually forgot what it's called exactly +# +# Most is the same. Only thing different is that it: +# - prints leading space because my head is tired from moving to the left in +# internet articles, terminal programs, and prompt +# - changes the suffix to ">" +# - fixes git clean state char +# - makes base name blue +# - print user@hostname in right prompt (see fish_right_prompt) function fish_prompt --description 'Write out the prompt' set -l last_pipestatus $pipestatus @@ -35,6 +45,10 @@ function fish_prompt --description 'Write out the prompt' set -g __fish_git_prompt_char_invalidstate "✖" end if not set -q __fish_git_prompt_char_cleanstate + # Switch from the check symbol to = because this fixes the width + # calculation problem where where the last char at the right prompt + # will be wrapped to the next line. + # Solution from stack overflow obviously, lost link though set -g __fish_git_prompt_char_cleanstate "=" end if not set -q __fish_git_prompt_color_dirtystate @@ -60,21 +74,34 @@ function fish_prompt --description 'Write out the prompt' case root toor if set -q fish_color_cwd_root set color_cwd $fish_color_cwd_root + set color_cwd_base $fish_color_cwd_root else set color_cwd $fish_color_cwd + set color_cwd_base blue end - set suffix '#' + set suffix ' #' case '*' set color_cwd $fish_color_cwd - set suffix '$' + set color_cwd_base blue + set suffix '>' end + # NOW we print the prompt :p + # PWD + set wd (prompt_pwd) set_color $color_cwd - echo -n (prompt_pwd) + if not [ $wd = '~' ] + # Make basename blue just because + echo -n (dirname $wd)'/' + set_color $color_cwd_base + echo -n (basename $wd) + else + echo -n ' ~' + end set_color normal - printf '%s ' (fish_vcs_prompt) + printf '%s' (fish_vcs_prompt) set -l pipestatus_string (__fish_print_pipestatus "[" "] " "|" (set_color $fish_color_status) (set_color --bold $fish_color_status) $last_pipestatus) echo -n $pipestatus_string diff --git a/.config/fish/functions/fish_right_prompt.fish b/.config/fish/functions/fish_right_prompt.fish new file mode 100644 index 0000000..cd121e3 --- /dev/null +++ b/.config/fish/functions/fish_right_prompt.fish @@ -0,0 +1,6 @@ +function fish_right_prompt + set_color $fish_color_autosuggestion 2> /dev/null; or set_color 555 + printf "%s@%s " $USER (hostname) + date "+%H:%M:%S" + set_color normal +end diff --git a/.config/fish/functions/prompt_pwd.fish b/.config/fish/functions/prompt_pwd.fish new file mode 100644 index 0000000..e055e62 --- /dev/null +++ b/.config/fish/functions/prompt_pwd.fish @@ -0,0 +1,26 @@ +# Based on the built-in prompt_pwd. +function prompt_pwd --description "Print the current working directory, shortened to fit the prompt" + set -l options h/help + argparse -n prompt_pwd --max-args=0 $options -- $argv + or return + + if set -q _flag_help + __fish_print_help prompt_pwd + return 0 + end + + # This allows overriding fish_prompt_pwd_dir_length from the outside (global or universal) without leaking it + set -q fish_prompt_pwd_dir_length + or set -l fish_prompt_pwd_dir_length 1 + + # Replace $HOME with "~" + set -l realhome ~ + set -l tmp (string replace -r '^'"$realhome"'($|/)' '~$1' $PWD) + + if [ $fish_prompt_pwd_dir_length -eq 0 ] + echo $tmp + else + # Shorten to at most $fish_prompt_pwd_dir_length characters per directory + string replace -ar '(\.?[^/]{'"$fish_prompt_pwd_dir_length"'})[^/]*/' '$1/' $tmp + end +end