Created
February 26, 2016 00:46
-
-
Save nlf/cd2623830e564b0ac2f2 to your computer and use it in GitHub Desktop.
This file contains 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
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" | |
} | |
dark_gray=$(tput setaf 236) | |
light_gray=$(tput setaf 242) | |
red=$(tput setaf 124) | |
blue=$(tput setaf 68) | |
green=$(tput setaf 28) | |
normal=$(tput sgr0) | |
git_prompt() { | |
local branch | |
local suffix | |
branch=$(git rev-parse --abbrev-ref HEAD 2>/dev/null) | |
if [ $? -ne 0 ]; then | |
unset __git_status __git_branch __git_color __git_suffix | |
return | |
fi | |
git diff-index --quiet --cached HEAD 2>/dev/null | |
local staged=$? | |
git diff-files --quiet 2>/dev/null | |
local changed=$? | |
test -z "$(git ls-files --exclude-standard --others 2>/dev/null)" | |
local untracked=$? | |
local color=${light_gray} | |
if [ $staged -eq 1 ]; then | |
color=${green} | |
if [ $changed -eq 1 ]; then | |
suffix="+" | |
fi | |
elif [ $changed -eq 1 ]; then | |
color=${red} | |
fi | |
if [ $untracked -eq 1 ]; then | |
suffix="${suffix}…" | |
fi | |
export __git_color=$color | |
export __git_branch=$branch | |
export __git_suffix="${suffix} " | |
} | |
status() { | |
__status=$? | |
[ "$__status" -eq 0 ] && __status_color=${blue} || __status_color=${red} | |
export __status_color | |
} | |
full_line() { | |
export __line=$(eval printf %.0s─ '{1..'"$(tput cols)}"\}) | |
} | |
PROMPT_COMMAND='status; full_line; git_prompt; short_pwd;' | |
PS1='\[\e]0;$USER@$(hostname -s): $__short_pwd\a\]\[${dark_gray}\]${__line}\n\[${normal}\]${__short_pwd} \[${__git_color}\]${__git_branch}\[${light_gray}\]${__git_suffix}\[${__status_color}\]❯\[${normal}\] ' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment