Skip to content

Instantly share code, notes, and snippets.

@doloopwhile
Created November 1, 2018 05:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save doloopwhile/a48650a7fb05d576a9d199b370421a12 to your computer and use it in GitHub Desktop.
Save doloopwhile/a48650a7fb05d576a9d199b370421a12 to your computer and use it in GitHub Desktop.
The ultimate script to indicate git status on PS1
#!/bin/bash
# PS1の設定
C() {
case $1 in
black) echo -e -n "\033[1;30m";;
red) echo -e -n "\033[1;31m";;
green) echo -e -n "\033[1;32m";;
yellow) echo -e -n "\033[1;33m";;
blue) echo -e -n "\033[1;34m";;
magenta) echo -e -n "\033[1;35m";;
cyan) echo -e -n "\033[1;36m";;
white) echo -e -n "\033[1;37m";;
*) echo -e -n "\033[0m";;
esac
}
ps1_info() {
local END_CODE=$?
echo "${USER}@$(hostname -s) $(git-ps)"
echo "$(date +'%F %H:%M:%S') $(pwd | sed -e "s|^${HOME%/}|~|")"
}
PS1='$(ps1_info)'
# カーソル位置がずれる問題を回避するため、ここの部分だけは文字列連結にする
if [ "$(id -u)" -eq 0 ]; then
PS1="${PS1}\n\[$(C red)\]#\[$(red)\] " # rootの場合はシャープ
else
PS1="${PS1}\n\[$(C cyan)\]\$\[$(C reset)\] " # 非rootユーザの場合はドル
fi
export PS1
#!/bin/bash
set -euC
C() {
case $1 in
black) echo -e -n "\033[1;30m";;
red) echo -e -n "\033[1;31m";;
green) echo -e -n "\033[1;32m";;
yellow) echo -e -n "\033[1;33m";;
blue) echo -e -n "\033[1;34m";;
magenta) echo -e -n "\033[1;35m";;
cyan) echo -e -n "\033[1;36m";;
white) echo -e -n "\033[1;37m";;
*) echo -e -n "\033[0m";;
esac
}
status() {
local modified=0
local cached=0
local untracked=0
while read -r line; do
if [ "$line" = '_?_?_' ]; then
untracked=1
continue
fi
if [[ "$line" =~ ^_[^[:space:]]_.?_ ]]; then
cached=1
fi
if [[ "$line" =~ ^_._[^[:space:]]_ ]]; then
modified=1
fi
done < <(git status --short | cut -b -2 | sed -e 's/\(.\)\(.*\)/_\1_\2_/')
if [ $modified -ne 0 ]; then
C red
echo -n 'M'
fi
if [ $cached -ne 0 ]; then
C green
echo -n 'C'
fi
if [ $untracked -ne 0 ]; then
C red
echo -n '?'
fi
if [ -n "$(git stash list)" ]; then
C cyan
echo -n 'S'
fi
C reset
}
branch() {
local branch
branch="$(git branch 2>/dev/null | grep '^\*' | sed -e "s/^* //")"
if [[ "${branch}" =~ ^bug- ]]; then
C green
elif [[ "${branch}" =~ ^atc- ]]; then
C cyan
elif [[ "${branch}" =~ ^tmp ]]; then
C magenta
elif [[ "${branch}" = "(detached from hde/master)" ]]; then
C yellow
else
C white
fi
echo -n "${branch}"
C reset
}
hash() {
git log --pretty=format:'%h' -n 1
}
git_ps() {
if ! git status --ignore-submodules &>/dev/null; then
return
fi
echo "git[$(status)]=$(branch):$(hash)"
}
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
git_ps
fi
$ wget 

Edit ~/.bashrc or ~/.bash_profile.

Redefine PS1 as following:

export PS1=
@Whyglobaleyes
Copy link

@doloopwhile
Hi, Thanks for this script it's fabulous! It took me a little (a lot) of effort to figure out how the script works (and I learned a little - a lot - about bash) but now I've based a section of my PS1 on it.

On my system I had to make one change in lines 25, 30 & 34 and I thought I'd mention it in case anyone else hits the same gotcha.
FROM: untracked=1 :: TO: ((untracked++))

bw
Alex

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment