This repository has been archived on 2024-03-28. You can view files and clone it, but cannot push or open issues or pull requests.
dotfiles/common/fish/prompt.fish

100 lines
1.9 KiB
Fish
Raw Normal View History

2021-11-03 12:01:43 -05:00
set MAX_PATH_PIECE_CHARS 3
2020-01-16 17:01:59 -06:00
2021-11-03 12:01:43 -05:00
function get_hostname
has_command hostname && hostname || cat /etc/hostname
end
2020-01-16 17:01:59 -06:00
2021-11-03 12:01:43 -05:00
function fish_greeting
2022-10-11 22:59:06 -05:00
set_color -b normal brblack
2021-11-03 12:01:43 -05:00
printf "%s@%s %s\n" $USER (get_hostname) (date)
end
2020-01-16 17:01:59 -06:00
function preprocess_pwd
test (pwd) = / && echo "/" && return 1
test (pwd) = $NICE_HOME && echo "~" && return 0
2022-05-10 13:23:32 -05:00
pwd | cut -c2- | \
awk '{n=split($0,p,"/");for(i=1;i<=n;i++){if(i==n){printf "/%s",p[i]}else{printf "/%.'$MAX_PATH_PIECE_CHARS[1]'s",p[i]}}}'
2020-01-16 17:01:59 -06:00
end
2022-10-17 10:08:38 -05:00
function _maybe_sudo_prefix
2022-10-11 22:59:06 -05:00
if set -q SUDO_USER
set_color -b yellow black
printf " SUDO "
set_color -b normal normal
printf " "
end
2022-10-17 10:08:38 -05:00
end
function _user_and_host
if test $argv[1] -eq 0
2022-10-11 22:59:06 -05:00
set_color -b normal blue
else
set_color -b normal red
end
printf "$USER@$hostname"
2022-10-17 10:08:38 -05:00
end
function _cur_work_dir
2022-10-11 22:59:06 -05:00
set_color -b normal magenta
printf " %s" (preprocess_pwd)
2022-10-17 10:08:38 -05:00
end
function _last_cmd_duration
2022-10-11 22:59:06 -05:00
set_color -b normal green
set -q CMD_DURATION && printf " %dms" $CMD_DURATION
2022-10-17 10:08:38 -05:00
end
function _maybe_jobs_summary
2022-10-11 22:59:06 -05:00
if jobs -q
set_color -b normal cyan
2022-10-11 23:14:24 -05:00
printf " &%d" (jobs -p | wc -l)
2022-10-11 22:59:06 -05:00
end
2022-10-17 10:08:38 -05:00
end
function _user_prompt
2022-10-11 22:59:06 -05:00
printf "\n"
set_color brblack
2020-04-21 16:38:58 -05:00
if test (id -u) -eq 0
2022-10-17 10:12:43 -05:00
printf '# '
2020-01-16 17:01:59 -06:00
else
2022-10-11 22:59:06 -05:00
printf '$ '
2020-01-16 17:01:59 -06:00
end
2022-10-11 22:59:06 -05:00
set_color -b normal normal
2020-01-16 17:01:59 -06:00
end
2022-10-17 10:08:38 -05:00
function _maybe_git_summary
set_color -b normal yellow
set cur_sha (git rev-parse --short HEAD 2>/dev/null)
if test $status = 0
set num_changes (git status --porcelain | wc -l)
if test $num_changes = 0
set num_changes "✔"
else
set num_changes "+$num_changes"
end
printf " %s %s %s" (git branch --show-current) $cur_sha $num_changes
end
end
2022-10-17 10:11:49 -05:00
function _prompt_prefix
set_color -b normal brblack
printf "# "
end
2022-10-17 10:08:38 -05:00
function fish_prompt
set last_cmd_status $status
2022-10-17 10:11:49 -05:00
_prompt_prefix
2022-10-17 10:08:38 -05:00
_maybe_sudo_prefix
_user_and_host $last_cmd_status
_cur_work_dir
2022-10-17 10:09:14 -05:00
_maybe_git_summary
2022-10-17 10:08:38 -05:00
_last_cmd_duration
_maybe_jobs_summary
_user_prompt
end
2020-01-17 03:02:53 -06:00
function fish_mode_prompt; end
2022-10-11 22:59:06 -05:00
function fish_right_prompt
end