Created
December 28, 2018 18:10
-
-
Save nonnymoose/815f8f7e451ff2474bc85345e70412ba to your computer and use it in GitHub Desktop.
A fish prompt with full directory names that won't overflow your terminal!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/fish | |
function fish_prompt | |
# Cache exit status | |
set -l last_status $status | |
# Just calculate these once, to save a few cycles when displaying the prompt | |
if not set -q __fish_prompt_hostname | |
set -g __fish_prompt_hostname (hostname|cut -d . -f 1) | |
end | |
if not set -q __fish_prompt_char | |
switch (id -u) | |
case 0 | |
set -g __fish_prompt_char \u276f\u276f | |
case '*' | |
set -g __fish_prompt_char » | |
end | |
end | |
# Configure __fish_git_prompt | |
set -g __fish_git_prompt_show_informative_status true | |
set -g __fish_git_prompt_showcolorhints true | |
# Color prompt char red for non-zero exit status | |
set -l pcolor (set_color green) | |
if [ $last_status -ne 0 ] | |
set pcolor (set_color -o red)"$last_status " | |
end | |
# echo -n (set_color cyan)$USER(set_color normal)@(set_color yellow)$__fish_prompt_hostname(set_color normal):(set_color -o red)(prompt_pwd)(set_color normal) | |
# __fish_git_prompt | |
# | |
# echo | |
# | |
# echo -n $pcolor$__fish_prompt_char (set_color normal) | |
# ugh. If the prompt overflows, fish just replaces it with `>` instead of allowing it to span multiple lines like literally every other shell. I've got to fix that | |
# set -l promptstring (set_color cyan)$USER(set_color normal)@(set_color yellow)$__fish_prompt_hostname(set_color normal):(set_color -o red)(prompt_pwd)(set_color normal)(__fish_git_prompt)\n$pcolor$__fish_prompt_char (set_color normal) | |
# Nope, it works better if I split it up | |
set -l promptBeforeWD (set_color cyan)$USER(set_color normal)@(set_color yellow)$__fish_prompt_hostname(set_color normal):(set_color -o red) # note that WD stands for "working directory" | |
set -l promptWD (prompt_pwd) | |
set -l promptAfterWD (set_color normal)(printf "%s\n" (__fish_git_prompt)) | |
set -l promptSecondLine $pcolor$__fish_prompt_char" "(set_color normal) | |
if test (string length (string replace -ar "\e[\[\(].*?m" "" $promptBeforeWD$promptWD$promptAfterWD)) -gt $COLUMNS # check if the length of the first prompt line, with ansi escape sequences removed, is greater than the terminal width (overflow) | |
set -l newLength (math $COLUMNS"-"(string length (string replace -ar "\e[\[\(].*?m" "" $promptBeforeWD$promptAfterWD))) # subtract the length of the first prompt line (except for the part containing the working directory), minus ansi escape sequences, from the terminal width to get the maximum allowable working directory string length | |
set -l oldLength (string length $promptWD) # the current working directory string length | |
# set promptWD ...(string sub -s (math $oldLength-$newLength+4) $promptWD) # take the substring of the current working directory such that just enough characters at the beginning are cut of so that it is the maximum permissible length. The reason I'm adding 4, instead of 3, is apparently someone thought it would be a good idea to make the string indices in fish start at 1. This is a bad idea. It made it a pain to figure this out, too | |
set promptWD …(string sub -s (math $oldLength-$newLength+2) $promptWD) # use this one if you have a unicode terminal because it will only eat 1 character for the ellipsis | |
end | |
echo $promptBeforeWD$promptWD$promptAfterWD | |
echo -n $promptSecondLine | |
# From `man string`: | |
# The start of the substring can be specified with -s or --start followed by a 1-based index value. | |
# That's it! I'm going back to bash! (Actually, it's ok. I came back the next day) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Github messed up my indentation and I'm not fixing it. 💩