Last active
October 9, 2015 07:37
-
-
Save ByScripts/3462873 to your computer and use it in GitHub Desktop.
Bash and Zsh prompt with Git support
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
# To use, download this file at the root of your home directory, than add : | |
# . .bash_prompt | |
# at the end of you .bashrc file | |
# Colors | |
LineColor="\e[0;36m" | |
GitNotOkColor="\e[1;31m" | |
GitOkColor="\e[0;32m" | |
RootLineColor=$GitNotOkColor | |
WorkingDirColor="\e[0;93m" | |
TimeColor="\e[0;96m" | |
RvmColor=$WorkingDirColor | |
RvmGemsetColor=$GitOkColor | |
HostnameColor=$GitOkColor | |
# Get the current working dir "home-shortened" (~/dir instead of /home/user/dir) | |
function getShortWorkingDirectory { | |
escaped_home=$(echo $HOME | sed -e 's/[\/&]/\\&/g') | |
short_working_directory=$(pwd | sed -e "s/$escaped_home/~/g") | |
echo $short_working_directory | |
} | |
if [[ $(type -t __git_ps1) == "" ]]; then | |
function __git_ps1 { | |
local b="$(git symbolic-ref HEAD 2>/dev/null)"; | |
if [ -n "$b" ]; then | |
printf " (%s)" "${b##refs/heads/}"; | |
fi | |
} | |
fi | |
# Get the current Git branch without parenthesis | |
function getGitBranch { | |
echo $(__git_ps1 "%s") | |
} | |
# Get the current Git status | |
# ok = Current Git branch is clean | |
# nok = Current Git branch is dirty | |
# notgit = Not in a git repository | |
function getGitStatus { | |
if [[ $(getGitBranch) != "" ]]; then | |
git status | grep "nothing to commit" > /dev/null 2>&1 | |
if [[ $? -eq 0 ]]; then | |
echo "ok" | |
else | |
echo "nok" | |
fi | |
else | |
echo "nogit" | |
fi | |
} | |
# Build the prompt | |
function gitPrompt { | |
dir=$(getShortWorkingDirectory) | |
branch=$(getGitBranch) | |
status=$(getGitStatus) | |
time=$(date +%H:%M:%S) | |
hostname=$(hostname) | |
before="───┤ " | |
after=" ├───" | |
if [[ $(type -t rvm-prompt) != "" ]]; then | |
rvm=$(rvm-prompt v) | |
rvm_gemset=$(rvm-prompt g) | |
fi | |
# Change the line color if logged as root (sudo) | |
if [[ $(whoami) == "root" ]]; then | |
LineColor=$RootLineColor | |
fi | |
prompt="┌$before$hostname$after$before$time$after" | |
colored_prompt="$LineColor┌$before$HostnameColor$hostname$LineColor$after$before$TimeColor$time$LineColor$after" | |
if [[ $status != "nogit" ]]; then | |
# We are in a git repository | |
prompt="$prompt$before" | |
colored_prompt="$colored_prompt$before" | |
if [[ $status == "ok" ]]; then | |
# The current branch is clean | |
prompt="$prompt ✓ $branch" | |
colored_prompt="$colored_prompt $GitOkColor✓ $branch" | |
else | |
# The current branch is dirty | |
prompt="$prompt ✕ $branch" | |
colored_prompt="$colored_prompt $GitNotOkColor✕ $branch" | |
fi | |
prompt="$prompt$after" | |
colored_prompt="$colored_prompt$LineColor$after" | |
fi | |
prompt="$prompt$before$dir$after" | |
colored_prompt="$colored_prompt$before$WorkingDirColor$dir$LineColor$after" | |
if [[ $rvm != "" ]]; then | |
prompt="$prompt$before$rvm$rvm_gemset$after" | |
colored_prompt="$colored_prompt$before$RvmColor$rvm$RvmGemsetColor$rvm_gemset$LineColor$after" | |
fi | |
# Counting the length of the generated prompt | |
length=$(echo $prompt | wc -m) | |
# Compute the length difference between total columns, and prompt length | |
let fill_count=$COLUMNS-$length | |
# Right pad the prompt to fill the screen width | |
while [[ $fill_count -gt 0 ]]; do | |
prompt="$prompt─" | |
colored_prompt="$colored_prompt─" | |
let fill_count=$fill_count-1 | |
done | |
# Insert a new line before the prompt, for more clarity | |
echo "" | |
echo -e $colored_prompt | |
echo -ne "└──▶ " | |
} | |
# Replace the current prompt | |
export PS1="\$(gitPrompt)" |
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
fillme(){ | |
wdir_length=${#${PWD/#$HOME/"~"}} | |
gpi=$(git_prompt_info_reverse) | |
local zero='%([BSUbfksu]|([FB]|){*})' | |
local gpi_length=${#${(S%%)gpi//$~zero/}} | |
let length=$COLUMNS-$wdir_length-$gpi_length-32 | |
local fill="" | |
while [[ $length -gt 0 ]]; do | |
fill="$fill─" | |
let length=$length-1 | |
done | |
echo $fill | |
} | |
PROMPT=$'\ | |
%{$fg[cyan]%}┌───┤%{$fg[white]%} %D{%H:%M:%S} %{$fg[cyan]%}├──────┤ %{$fg[yellow]%}%~%{$fg[cyan]%} ├───$(git_prompt_info_reverse)───$(fillme)\ | |
└──▶ %{$reset_color%} ' | |
PROMPT2=" %{$fg[cyan]%}┈▶%{$reset_color%} " | |
ZSH_THEME_GIT_PROMPT_PREFIX="───┤ " | |
ZSH_THEME_GIT_PROMPT_SUFFIX="%{$fg_no_bold[cyan]%} ├───" | |
ZSH_THEME_GIT_PROMPT_DIRTY="%{$fg_bold[red]%}✕ " | |
ZSH_THEME_GIT_PROMPT_CLEAN="%{$fg_bold[green]%}✓ " |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment