dotfiles/fish/.config/fish/functions/fish_prompt.fish
2017-04-14 18:38:15 +02:00

96 lines
2.9 KiB
Fish

function __bcarlin_prompt_suffix
set -l color_suffix
set -l suffix "➜"
switch $USER
case root toor
if set -q fish_color_cwd_root
echo root if
set color_suffix $fish_color_cwd_root
else
set color_suffix $fish_color_cwd
end
case '*'
set color_suffix $fish_color_cwd
end
echo -n -s ' ' (set_color $color_suffix --bold) "$suffix" (set_color normal) ' '
end
function __bcarlin_prompt_git
set -l git_infos (git status --porcelain --branch ^/dev/null )
if test $status -ne 0
echo -n -s ''
return
end
set -l git_branch (echo $git_infos | grep '##' | sed -e 's|## \([^\.]\+\).*|\1|')
set -l has_untracked false
set -l staged_count 0
set -l changed_count 0
set -l conflict_count 0
set -l staged_count 0
set -l behind (echo $git_infos | grep '##' | grep behind | sed -e 's|.*behind \([0-9]\+\).*|\1|')
set -l ahead (echo $git_infos | grep '##' | grep ahead | sed -e 's|.*ahead \([0-9]\+\).*|\1|')
for line in $git_infos
set -l flags (echo $line | cut -c '-2')
set flag1 (string sub -l 1 $flags)
set flag2 (string sub -s 2 -l 1 $flags)
if test $flags = '??'
set has_untracked true
continue
end
if test $flag1 = 'U' -o $flag2 = 'U'
set conflict_count (math $conflict_count + 1)
continue
end
if contains $flag1 'M' 'A' 'C' 'R' 'D'
set staged_count (math $staged_count + 1)
end
if contains $flag2 'M' 'A' 'R' 'C' 'D'
set changed_count (math $changed_count + 1)
end
end
echo -n -s ' ' (set_color blue) ± (set_color normal) '('
echo -n -s (set_color red --bold) $git_branch (set_color normal)
if test (math $staged_count + $conflict_count + $changed_count) -ne 0 -o $has_untracked = true
echo -n -s '|'
test -n "$behind"; and echo -s -n (set_color red) "$behind" (set_color normal)
test -n "$ahead"; and echo -s -n (set_color green) "$ahead" (set_color normal)
test $conflict_count -ne 0; and echo -s -n (set_color red) "$conflict_count" (set_color normal)
test $staged_count -ne 0; and echo -s -n (set_color red) "$staged_count" (set_color normal)
test $changed_count -ne 0; and echo -s -n (set_color blue) "$changed_count" (set_color normal)
test $has_untracked = true; and echo -s -n '…'
else
echo -n -s (set_color green --bold) "✔" (set_color normal)
end
echo -n -s ')'
end
function __bcarlin_prompt_venv
if test -n "$VIRTUAL_ENV"
set -l venv_name (basename $VIRTUAL_ENV)
if test $venv_name = '.venv'
set venv_name (basename (dirname $VIRTUAL_ENV))
end
echo -n -s (set_color blue --bold) "(" $venv_name ") " (set_color normal)
end
end
function fish_prompt --description 'Write out the prompt'
set -l _pwd (set_color $fish_color_cwd) (prompt_pwd)
echo -n -s (__bcarlin_prompt_venv)
echo -n -s "$USER" @ (prompt_hostname) ' ' $_pwd
echo -n -s (__bcarlin_prompt_git)
echo -n -s (__bcarlin_prompt_suffix)
end