Skip to content

Instantly share code, notes, and snippets.

@possan
Last active April 17, 2017 14:20
Show Gist options
  • Select an option

  • Save possan/dbaed44c953fa3ef79fff6ac041c5265 to your computer and use it in GitHub Desktop.

Select an option

Save possan/dbaed44c953fa3ef79fff6ac041c5265 to your computer and use it in GitHub Desktop.
Powerline prompt hack
#!/bin/sh
# Git logic stolen from https://github.com/twolfson/sexy-bash-prompt/blob/master/.bash_prompt
# Blocky fonts from https://github.com/powerline/fonts
# Colors from http://misc.flogisoft.com/bash/tip_colors_and_formatting
# http://www.calmar.ws/vim/256-xterm-24bit-rgb-color-chart.html
# magentas 236 24 247
# grn 34
# red 161
# dark gray 236 fg 247
# dark cyan 24 fg 81
ESC=$(printf "\e")
A0=$(echo -e "\xEE\x82\xA0")
A1=$(echo -e "\xEE\x82\xA1")
A2=$(echo -e "\xEE\x82\xA2")
B0=$(echo -e "\xEE\x82\xB0")
B1=$(echo -e "\xEE\x82\xB1")
B2=$(echo -e "\xEE\x82\xB2")
B3=$(echo -e "\xEE\x82\xB3")
# A0="X"
# A1="X"
# A2="X"
# B0="X"
# B1="X"
# B2="X"
# B3="X"
S0="\[\e[39m\]\[\e[38;5;236m\]"
S1="\[\e[48;5;236m\]\[\e[38;5;247m\]"
S2="\[\e[48;5;24m\]\[\e[38;5;236m\]"
S3="\[\e[48;5;24m\]\[\e[38;5;255m\]"
S4="\[\e[39m\]\[\e[38;5;24m\]"
S5="\[\e[48;5;247m\]\[\e[38;5;24m\]"
S6="\[\e[48;5;247m\]\[\e[38;5;255m\]"
S7="\[\e[39m\]\[\e[38;5;247m\]"
C4R="\[\e[48;5;161m\]\[\e[38;5;24m\]"
CR="\[\e[48;5;161m\]\[\e[97m\]"
CR0="\[\e[0m\]\[\e[38;5;161m\]"
C4G="\[\e[48;5;34m\]\[\e[38;5;24m\]"
CG="\[\e[48;5;34m\]\[\e[97m\]"
CG0="\[\e[0m\]\[\e[38;5;34m\]"
RESET='\[\e[0m\]'
sexy_bash_prompt_is_branch1_behind_branch2 () {
first_log="$(git log $1..$2 -1 2> /dev/null)"
[[ -n "$first_log" ]]
}
function sexy_bash_prompt_parse_git_dirty() {
if [[ -n "$(git status --porcelain 2> /dev/null)" ]]; then
echo 1
else
echo 0
fi
}
sexy_bash_prompt_branch_exists () {
git branch --remote 2> /dev/null | grep --quiet "$1"
}
sexy_bash_prompt_parse_git_ahead () {
branch="$(sexy_bash_prompt_get_git_branch)"
remote="$(git config --get "branch.${branch}.remote" || echo -n "origin")"
remote_branch="$remote/$branch"
if (sexy_bash_prompt_is_branch1_behind_branch2 "$remote_branch" "$branch" ||
! sexy_bash_prompt_branch_exists "$remote_branch"); then
echo 1
else
echo 0
fi
}
sexy_bash_prompt_parse_git_behind () {
branch="$(sexy_bash_prompt_get_git_branch)"
remote="$(git config --get "branch.${branch}.remote" || echo -n "origin")"
remote_branch="$remote/$branch"
if sexy_bash_prompt_is_branch1_behind_branch2 "$branch" "$remote_branch"; then
echo 1
else
echo 0
fi
}
# function sexy_bash_prompt_get_git_status() {
# dirty_branch="$(sexy_bash_prompt_parse_git_dirty)"
# branch_ahead="$(sexy_bash_prompt_parse_git_ahead)"
# branch_behind="$(sexy_bash_prompt_parse_git_behind)"
# if [[ "$dirty_branch" == 1 && "$branch_ahead" == 1 && "$branch_behind" == 1 ]]; then
# echo "$sexy_bash_prompt_dirty_unpushed_unpulled_symbol"
# elif [[ "$branch_ahead" == 1 && "$branch_behind" == 1 ]]; then
# echo "$sexy_bash_prompt_unpushed_unpulled_symbol"
# elif [[ "$dirty_branch" == 1 && "$branch_ahead" == 1 ]]; then
# echo "$sexy_bash_prompt_dirty_unpushed_symbol"
# elif [[ "$branch_ahead" == 1 ]]; then
# echo "$sexy_bash_prompt_unpushed_symbol"
# elif [[ "$dirty_branch" == 1 && "$branch_behind" == 1 ]]; then
# echo "$sexy_bash_prompt_dirty_unpulled_symbol"
# elif [[ "$branch_behind" == 1 ]]; then
# echo "$sexy_bash_prompt_unpulled_symbol"
# elif [[ "$dirty_branch" == 1 ]]; then
# echo "$sexy_bash_prompt_dirty_synced_symbol"
# else # clean
# echo "$sexy_bash_prompt_synced_symbol"
# fi
# }
# function sexy_bash_prompt_get_git_progress() {
# git_dir="$(git rev-parse --git-dir)"
# # git merge
# if [[ -f "$git_dir/MERGE_HEAD" ]]; then
# echo " [merge]"
# elif [[ -d "$git_dir/rebase-apply" ]]; then
# # git am
# if [[ -f "$git_dir/rebase-apply/applying" ]]; then
# echo " [am]"
# # git rebase
# else
# echo " [rebase]"
# fi
# elif [[ -d "$git_dir/rebase-merge" ]]; then
# # git rebase --interactive/--merge
# echo " [rebase]"
# elif [[ -f "$git_dir/CHERRY_PICK_HEAD" ]]; then
# # git cherry-pick
# echo " [cherry-pick]"
# fi
# if [[ -f "$git_dir/BISECT_LOG" ]]; then
# # git bisect
# echo " [bisect]"
# fi
# if [[ -f "$git_dir/REVERT_HEAD" ]]; then
# # git revert --no-commit
# echo " [revert]"
# fi
# }
function sexy_bash_prompt_get_git_branch() {
# On branches, this will return the branch name
# On non-branches, (no branch)
ref="$(git symbolic-ref HEAD 2> /dev/null | sed -e 's/refs\/heads\///')"
if [[ "$ref" != "" ]]; then
echo "$ref"
else
echo "(no branch)"
fi
}
function time_section() {
echo $RESET$S1' $(date +"%H:%M:%S") '
# echo " $(date +'%H:%M:%S') |
}
function path_section() {
echo "${S2}${B0}${S3} $(pwd) "
# echo " $(pwd) | "
}
function git_section() {
if [[ $(git rev-parse 2>/dev/null; echo $?) == 0 ]]; then
dirty_branch="$(sexy_bash_prompt_parse_git_dirty)"
branch_ahead="$(sexy_bash_prompt_parse_git_ahead)"
branch_behind="$(sexy_bash_prompt_parse_git_behind)"
if [[ "${dirty_branch}" == "1" ]]; then
echo "${C4R}${B0}${CR} ${A0} $(sexy_bash_prompt_get_git_branch) +${branch_ahead} -${branch_behind} ${CR0}${B0}"
else
echo "${C4G}${B0}${CG} ${A0} $(sexy_bash_prompt_get_git_branch) +${branch_ahead} -${branch_behind} ${CG0}${B0}"
fi
return
fi
echo "${C4R}${B0}${CR} ${CR0}${B0}"
# echo "${C4R}${B0}${CR} y no git? ${CR0}${B0}"
# echo " no git $"
}
function last_reset_section() {
echo "${RESET} "
}
function test_cmd() {
echo '\[\e[01m\] RESET'
}
# PS1='$(time_section)$(path_section)$(git_section)$(last_reset_section)'
set_bash_prompt(){
# PS1="\u@\h $(call_your_function) $>"
P1=$(time_section)
P2=$(path_section)
P3=$(git_section)
P4=$(last_reset_section)
STR=$(test_cmd)
PS1=$P1$P2$P3$P4
# PS1=$STR' \[\e[0m\] >'
}
PROMPT_COMMAND=set_bash_prompt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment