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

128 lines
2.4 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
2023-07-10 22:22:40 -05:00
if test (uname) = Linux || test (uname) = Darwin
has_command hostname && hostname --short || cat /etc/hostname
else
# assume bsd
hostname | head -n 1
end
2021-11-03 12:01:43 -05:00
end
2020-01-16 17:01:59 -06:00
2021-11-03 12:01:43 -05:00
function fish_greeting
2022-10-26 16:22:42 -05:00
_prompt_prefix
printf "%s\n" (date)
2021-11-03 12:01:43 -05:00
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 _maybe_aws_profile
if set -q AWS_PROFILE && test $AWS_PROFILE = prd
printf " "
set_color -b yellow black
printf " AWS_PROFILE=prd "
set_color -b normal normal
end
end
2022-10-17 10:08:38 -05:00
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
2023-06-16 14:06:56 -05:00
printf "%s@%s" $USER (get_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
2022-10-20 20:51:05 -05:00
set num_changes (git status --porcelain | wc -l | string trim)
2022-10-17 10:08:38 -05:00
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-12-13 15:34:46 -06:00
function _prompt_marker
printf "%b133;A%b" "\x1b\x5d" "\x1b\x5c"
end
function _prompt_continuation_marker
printf "%b133;A;k=s%b" "\x1b\x5d" "\x1b\x5c"
end
function cmd_marker --on-variable _
printf "%b133;C%b" "\x1b\x5d" "\x1b\x5c"
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-12-13 15:34:46 -06:00
_prompt_marker
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
_maybe_aws_profile
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