Skip to content

Instantly share code, notes, and snippets.

@Kami-no
Created June 20, 2019 20:20
Show Gist options
  • Save Kami-no/752add87076df45d37a56e4eacaf39d3 to your computer and use it in GitHub Desktop.
Save Kami-no/752add87076df45d37a56e4eacaf39d3 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Power-line
PS1_col() {
case "$1" in
black) echo 0;;
red) echo 1;;
green) echo 2;;
yellow) echo 3;;
blue) echo 4;;
magenta) echo 5;;
cyan) echo 6;;
white) echo 7;;
blood) echo 88;;
esac
}
PS1_seg() {
# $1 background
# $2 foreground
# $3 content
# $4 static/dynamic/inline
local PS1_separator output
if [[ $4 == "s" ]]; then
PS1_separator=""
else
PS1_separator=""
fi
if [[ $4 == "i" ]]; then
if [ -x /usr/bin/tput ] && tput setaf 1 >&/dev/null; then
$4 = "i"
else
$4 = "s"
fi
fi
output=""
output+="$( /usr/bin/tput setab $( PS1_col $1 ) )"
output+="${PS1_separator}"
output+="$( /usr/bin/tput setaf $( PS1_col $2 ) )"
output+="$( /usr/bin/tput setab $( PS1_col $1 ) )"
output+=" $3 "
output+="$(/usr/bin/tput sgr0)"
output+="$( /usr/bin/tput setaf $( PS1_col $1 ) )"
echo $output
}
PS1_con() {
if [[ ${EUID} == 0 ]]; then
PS1_seg blood white "$( hostname -s )" "s"
else
PS1_seg black white "${USER}@$( hostname -s )" "s"
fi
}
PS1_dir() {
PS1_seg blue black "$( pwd | sed "s@$HOME@~@" )"
}
PS1_git() {
local git_branch
git_branch=$(git branch 2> /dev/null | sed -e "/^[^*]/d" -e "s/* \(.*\)/\1/")
if [[ -n $git_branch ]]; then
if [[ -n $(git status -s 2> /dev/null | tail -n 1) ]]; then
PS1_seg yellow black "$(basename $(git rev-parse --show-toplevel))  ${git_branch} ±" "d"
else
PS1_seg green black "$(basename $(git rev-parse --show-toplevel))  ${git_branch}" "d"
fi
fi
}
PS1_ven() {
local output
if [[ ! -z $VIRTUAL_ENV ]]; then
output=$( basename "${VIRTUAL_ENV#$WORKON_HOME}")
PS1_seg cyan black " ${output}" "d"
fi
}
PS1_end() {
echo "$(/usr/bin/tput sgr0)\n\\$ "
}
PS1_bld() {
local output
output=""
output+="$(PS1_con)"
output+="$(PS1_dir)"
output+="$(PS1_git)"
output+="$(PS1_ven)"
output+="$(PS1_end)"
PS1=$output
# set title
echo -ne "\033]0;${USER}@$(hostname -s):$( pwd | sed "s@$HOME@~@" )"; echo -ne "\007"
}
PROMPT_COMMAND=PS1_bld
PS1='\u@\h:\w\$ '
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment