fish_prompt.fish
概要
左边是限制了长度的 $PWD,右边是 git 状态,返回码非 0 时 $ 会变红。
预览
代码
注:fish_prompt.fish 依赖 location.fish
~/.config/fish/functions/fish_prompt.fish
# 判断是否是git仓库的工具函数
function is_git_repo --description 'Check if directory is a repository'
test -d .git
or command git rev-parse --git-dir >/dev/null ^/dev/null
end
# PS1
function fish_prompt --description 'Write out the prompt'
set last_status $status
set -l os (uname -o)
if test "$os" = "Darwin"
set -g __fish_git_prompt_show_informative_status 1
end
if not test $last_status -eq 0
set status_color (set_color red)
else
set status_color (set_color normal)
end
if set --query SSH_TTY
set fish_color_host red
end
if test "$USER" = "root"
set user_info (set_color red)"$USER"(set_color normal)" on "
set prompt_sign $status_color' # '
else
set prompt_sign $status_color' $ '
end
set path (set_color 2ca7f8)(location)
echo -ne "$user_info$path$prompt_sign"
set_color normal
end
function fish_right_prompt
set -g __fish_git_prompt_showupstream "informative"
set -g __fish_git_prompt_showdirtystate 1
set -g __fish_git_prompt_showstashstate 1
set -g __fish_git_prompt_showuntrackedfiles 1
set -g __fish_git_prompt_char_stateseparator " "
set -g __fish_git_prompt_char_upstream_prefix " "
# set -g __fish_git_prompt_showcolorhints 1
set git_info
if is_git_repo
if set -q __fish_git_prompt_show_informative_status
set file_status (__fish_git_prompt "%s")
set git_info (set_color yellow)"$file_status"
else
__fish_git_prompt >/dev/null 2>&1
set upstream (__fish_git_prompt_show_upstream)
set branch (git branch | grep \* | sed 's/* //')
set file_status (__fish_git_prompt_informative_status)
set git_info (set_color yellow)"$branch$__fish_git_prompt_char_stateseparator$file_status$upstream"
end
end
set -l os (uname -o)
if test "$os" = "Darwin"
set -g __fish_git_prompt_show_informative_status 1
end
if set -q HTTP_PROXY
if test "$os" = "Darwin"
set proxy (set_color normal)"🚀 "
else
set proxy (set_color normal)"p "
end
end
echo -n "$proxy$git_info"
set_color normal
end
~/.config/fish/functions/location.fish
function location --description "限制了长度的 $PWD,中间用 ... 代替了"
set _prefix_len 7
set _suffix_len 10
set _pwd (string replace -r '^'"$HOME"'($|/)' '~$1' $PWD)
set _pwd_len (string length $_pwd)
set _divider '...'
set _divider_len (string length $_divider)
set _cur_path_len (math $_pwd_len + $_divider_len)
set _max_len (math $_prefix_len + $_suffix_len + $_divider_len)
set _basename (basename $_pwd)
set _basename_len (string length $_basename)
if test $_pwd_len -gt $_max_len
if test $_basename_len -gt $_suffix_len
# basename 比 _suffix 长时显示完整的 basename
set _surplus_len (math $_prefix_len + $_divider_len + $_suffix_len - $_basename_len)
set _suffix_len $_basename_len
# 没有剩余空间时不显示 prefix 和 ...
if test $_surplus_len -lt $_divider_len
set prefix ''
set _divider ''
# 至少能显示下省略号:剩余空间 > _prefix_len + _divider_len
else
set _prefix_len (math $_surplus_len - $_divider_len)
set prefix (string sub --length $_prefix_len $_pwd)
end
else
set prefix (string sub --length $_prefix_len $_pwd)
end
set suffix (string sub --start=-$_suffix_len $_pwd)
echo "$prefix$_divider$suffix"
else
echo $_pwd
end
end

