Last active
August 29, 2016 17:58
-
-
Save adituv/8a0f7434e6240b3167d40f1f6254e425 to your computer and use it in GitHub Desktop.
.bash_prompt
This file contains hidden or 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
| #!/bin/bash | |
| PS1='\[$clr\](\[$bold$green\]$git_branch\[$clr\]$git_status_short) ' | |
| PS1+='\[$bold$blue\]$pwd_short\[$clr\]\$ ' | |
| export PS1 | |
| clr=$'\e[0m' | |
| bold=$'\e[1m' | |
| green=$'\e[32m' | |
| blue=$'\e[34m' | |
| shorten_pwd_nogit () { | |
| local pwd_temp | |
| declare -a pwd_parts | |
| pwd_temp=${PWD/#$HOME/'~'} | |
| IFS='/' pwd_parts=($pwd_temp) | |
| if [ "${#pwd_parts[@]}" -lt 4 ]; then | |
| echo "$pwd_temp" | |
| else | |
| echo "${pwd_parts[0]}/..${pwd_parts[-2]}/${pwd_parts[-1]}" | |
| fi | |
| } | |
| update_prompt () { | |
| local exit=$? | |
| local rev_parse_exit_code | |
| declare -a repo_info | |
| repo_info=( $(git rev-parse --git-dir --is-inside-git-dir \ | |
| --is-bare-repository --is-inside-work-tree \ | |
| --show-toplevel --show-prefix \ | |
| 2>/dev/null) ) | |
| rev_parse_exit_code="$?" | |
| if [ "$rev_parse_exit_code" = "128" ]; then | |
| # not in git repo | |
| git_branch="nogit" | |
| git_status_short="" | |
| pwd_short="$(shorten_pwd_nogit)" | |
| return $exit | |
| elif [ "$rev_parse_exit_code" != "0" ]; then | |
| # can't get repo info; set PS1 to a simple default | |
| PS1="\u:\w\$ " | |
| return $exit | |
| fi | |
| local git_basename git_curdir pwd_sep | |
| pwd_sep="/.." | |
| git_basename="$(echo ${repo_info[4]} | xargs basename)" | |
| git_curdir="$(expr match "${repo_info[5]}" '.*/\(.*/.*\)/$')" | |
| if [ -z "$git_curdir" ]; then | |
| git_curdir="${repo_info[5]%/}" | |
| pwd_sep="/" | |
| fi | |
| if [ -z "$git_curdir" ]; then | |
| pwd_sep="" | |
| fi | |
| git_branch="$(git describe --contains --all HEAD)" | |
| local p | |
| __git_ps1_show_upstream | |
| git_status_short=$p | |
| export pwd_short="$git_basename$pwd_sep$git_curdir" | |
| return $exit | |
| } | |
| PROMPT_COMMAND=update_prompt |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment