Skip to content

Instantly share code, notes, and snippets.

@nlf
Created May 8, 2019 23:04
Show Gist options
  • Save nlf/122842dab8b26c5b62c50d2af949d3ca to your computer and use it in GitHub Desktop.
Save nlf/122842dab8b26c5b62c50d2af949d3ca to your computer and use it in GitHub Desktop.
_color_dark_gray=$(tput setaf 238)
_color_light_gray=$(tput setaf 244)
_color_red=$(printf "\e[31m")
_color_blue=$(printf "\e[34m")
_color_green=$(printf "\e[32m")
_color_normal=$(printf "\e[0m")
_short_pwd() {
if [ "$PWD" == "/" ]; then
export __short_pwd="/"
return
fi
# this is where we'll accumulate our result
local path
# and where we'll accumulate a denormalized result
local fullpath
# find out if we're in the user's home directory and set prefixes if so
local trimmedpath=${PWD#$HOME}
if [ "$trimmedpath" != "$PWD" ]; then
path="~"
fullpath="$HOME"
fi
# split the trimmed path into an array and save the length - 1 for convenience
local parts=(${trimmedpath//\// })
local length=$(( ${#parts[@]} - 1 ))
# iterate over the path array
for i in "${!parts[@]}"; do
# reset the depth to 1, save the part to make things easier
local depth=1
local part=${parts[$i]}
# if this is the last item, just append it as is and return
if [ $i -eq $length ]; then
path="${path}/${part}"
continue
fi
# otherwise we evaluate to see if we should abbreviate and to what depth
# start by getting a list of files matching at the current depth
local files=("${fullpath}/${part::$depth}"*)
# loop until we get a depth that returns only one result, or until the whole string still has conflicts
while [ ${#files[@]} -gt 1 -a $depth -lt ${#part} ]; do
depth=$(( depth + 1 ))
files=("${fullpath}/${part::$depth}"*)
done
# append the abbreviated string to the path, and the full string to the full path
path="${path}/${part::$depth}"
fullpath="${fullpath}/${part}"
done
export __short_pwd="$path"
}
_git_prompt() {
local status
status=$(git status -unormal 2>&1)
if [ $? -ne 0 ]; then
unset __git_status __git_branch __git_color __git_suffix
return
fi
[[ "$status" =~ On\ branch\ ([^[:space:]]+) ]]; local branch=${BASH_REMATCH[1]}
[[ "$status" =~ Changes\ to\ be\ committed ]]; local staged=$?
[[ "$status" =~ Changes\ not\ staged\ for\ commit ]]; local changed=$?
[[ "$status" =~ Untracked\ files ]]; local untracked=$?
local suffix
local color=${_color_light_gray}
if [ $staged -eq 0 ]; then
color=${_color_green}
if [ $changed -eq 0 ]; then
suffix="+"
fi
elif [ $changed -eq 0 ]; then
color=${_color_red}
fi
if [ $untracked -eq 0 ]; then
suffix="${suffix}…"
fi
export __git_color=$color
export __git_branch=$branch
export __git_suffix="${suffix} "
}
_status() {
[ "$?" -eq 0 ] && __status_color=${_color_blue} || __status_color=${_color_red}
export __status_color
}
_full_line() {
export __line=$(eval printf %.0s─ '{1..'"$(tput cols)}"\})
}
[[ -s /etc/profile.d/vte.sh ]] && . /etc/profile.d/vte.sh
function _build_prompt() {
_status
history -a
_git_prompt
_short_pwd
# vte_pwd=$(__vte_osc7)
PS1='\[\e]0;$(basename $SHELL) $__short_pwd\a\]\n${__short_pwd} \[${__git_color}\]${__git_branch}\[${_color_light_gray}\]${__git_suffix}\[${__status_color}\]❯\[${_color_normal}\] '
}
PROMPT_COMMAND=_build_prompt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment