zero/home-environments/radio/files/.config/fish/functions/fish_prompt.fish

75 lines
2.2 KiB
Fish
Raw Permalink Normal View History

2024-03-08 14:32:05 +00:00
# Utilities
function displayed_string_length
string replace -ra -- '\x1b.*?[mGKH]' '' "$argv" | string length
end
function padding
printf "%-"$argv[1]"s%s" " " "$output"
end
# Current working directory prompt
function fish_cwd_prompt
set -l cwd (pwd)
2024-08-25 18:58:32 +00:00
switch "$cwd"
2024-03-08 14:32:05 +00:00
case "$HOME"
printf "~"
case "/"
printf "/"
case "*"
2024-08-25 18:58:32 +00:00
set -l parent_dir (dirname "$cwd")
2024-03-08 14:32:05 +00:00
switch "$parent_dir"
case "$HOME"
2024-08-25 18:58:32 +00:00
printf "~/%s" (basename "$cwd")
2024-03-08 14:32:05 +00:00
case "/"
2024-08-25 18:58:32 +00:00
printf "/%s" (basename "$cwd")
2024-03-08 14:32:05 +00:00
case "*"
2024-08-25 18:58:32 +00:00
test (dirname "$parent_dir") = "/" && printf "/"
printf "%s/%s" (basename "$parent_dir") (basename "$cwd")
2024-03-08 14:32:05 +00:00
end
end
end
function fish_prompt
# Color
set -x red_or_blue (test "$USER" = "root" && printf "red" || printf "blue")
function printc
set_color $red_or_blue; printf $argv; set_color normal
end
# Guix environment prompt
function fish_environment_prompt
printf "(env "; printc "%s" $GUIX_ENVIRONMENT; printf ")"
end
2024-03-08 14:32:05 +00:00
if test -n "$GUIX_ENVIRONMENT"
2024-08-25 18:58:32 +00:00
set -l fish_git_prompt (fish_git_prompt)
set -l fish_environment_prompt (fish_environment_prompt)
set -l fish_environment_prompt_length (displayed_string_length "$fish_environment_prompt")
set -l fish_git_prompt_length (displayed_string_length "$fish_git_prompt")
set -l fish_prompt_padding (math $COLUMNS - $fish_environment_prompt_length - $fish_git_prompt_length)
2024-03-08 14:32:05 +00:00
2024-08-25 18:58:32 +00:00
printf "$fish_environment_prompt"
printf (padding $fish_prompt_padding)
printf "$fish_git_prompt\n"
2024-03-08 14:32:05 +00:00
else
function fish_right_prompt
fish_git_prompt
end
end
set -g fish_key_bindings fish_vi_key_bindings
2024-08-25 18:58:32 +00:00
switch "$fish_key_bindings"
2024-03-08 14:32:05 +00:00
case fish_vi_key_bindings fish_hybrid_key_bindings
test $fish_bind_mode = 'default' && printf ':' || printf '+'
2024-03-08 14:32:05 +00:00
end
printc (whoami); printf ' at '; printc (prompt_hostname)
printf ' in '; printc (fish_cwd_prompt)
printf ' > '
2024-03-08 14:32:05 +00:00
end
function fish_default_mode_prompt -d "Display vi prompt mode"
end