126 lines
2.8 KiB
Bash
126 lines
2.8 KiB
Bash
#
|
||
# ~/.bashrc
|
||
#
|
||
|
||
[[ $- != *i* ]] && return
|
||
|
||
prompt_git() {
|
||
local s='';
|
||
local branchName='';
|
||
|
||
# Check if the current directory is in a Git repository.
|
||
if [ $(git rev-parse --is-inside-work-tree &>/dev/null; echo "${?}") == '0' ]; then
|
||
|
||
# check if the current directory is in .git before running git checks
|
||
if [ "$(git rev-parse --is-inside-git-dir 2> /dev/null)" == 'false' ]; then
|
||
|
||
# Ensure the index is up to date.
|
||
git update-index --really-refresh -q &>/dev/null;
|
||
|
||
# Check for uncommitted changes in the index.
|
||
if ! $(git diff --quiet --ignore-submodules --cached); then
|
||
s+='+';
|
||
fi;
|
||
|
||
# Check for unstaged changes.
|
||
if ! $(git diff-files --quiet --ignore-submodules --); then
|
||
s+='!';
|
||
fi;
|
||
|
||
# Check for untracked files.
|
||
if [ -n "$(git ls-files --others --exclude-standard)" ]; then
|
||
s+='?';
|
||
fi;
|
||
|
||
# Check for stashed files.
|
||
if $(git rev-parse --verify refs/stash &>/dev/null); then
|
||
s+='$';
|
||
fi;
|
||
|
||
fi;
|
||
|
||
# Get the short symbolic ref.
|
||
# If HEAD isn’t a symbolic ref, get the short SHA for the latest commit
|
||
# Otherwise, just give up.
|
||
branchName="$(git symbolic-ref --quiet --short HEAD 2> /dev/null || \
|
||
git rev-parse --short HEAD 2> /dev/null || \
|
||
echo '(unknown)')";
|
||
|
||
[ -n "${s}" ] && s=" [${s}]";
|
||
|
||
echo -e " ${1}${branchName}${2}${s}";
|
||
else
|
||
return;
|
||
fi;
|
||
}
|
||
|
||
hostname_color() {
|
||
case $(hostname) in
|
||
blue*)
|
||
echo "34"
|
||
;;
|
||
green*)
|
||
echo "32"
|
||
;;
|
||
pink*)
|
||
echo "35"
|
||
;;
|
||
red*)
|
||
echo "31"
|
||
;;
|
||
*)
|
||
echo "37"
|
||
;;
|
||
esac
|
||
}
|
||
|
||
status_color() {
|
||
if [[ ${EUID} == 0 ]] ; then
|
||
echo "31"
|
||
else
|
||
if [[ $(whoami) == "sheychen" ]] ; then
|
||
if [[ $(sudo -n uptime 2>&1|grep "load"|wc -l) -gt 0 ]] ; then
|
||
echo "33"
|
||
else
|
||
echo "32"
|
||
fi
|
||
else
|
||
echo "35"
|
||
fi
|
||
fi
|
||
}
|
||
|
||
parse_user() {
|
||
if [[ ${EUID} != 0 ]] ; then
|
||
if [[ $(whoami) != "sheychen" ]] ; then
|
||
echo "$(whoami)@"
|
||
fi
|
||
fi
|
||
}
|
||
|
||
function spwd {
|
||
echo $PWD | sed "s:${HOME}:~:" | sed "s:/\(.\)[^/]*:/\1:g" | sed "s:/[^/]*$:/$(basename "$PWD"):"
|
||
}
|
||
|
||
PS1='\[\033[01;$(status_color)m\][\[\033[01;37m\]$(parse_user)\[\033[01;$(hostname_color)m\]\H\[\033[01;37m\] $(spwd)$(prompt_git)\[\033[01;$(status_color)m\]]\[\033[00m\] '
|
||
|
||
type shopt &> /dev/null && shopt -s histappend;
|
||
shopt -s cmdhist
|
||
shopt -s checkwinsize
|
||
shopt -s autocd
|
||
bind Space:magic-space
|
||
bind '"\eh": "\C-a\eb\ed\C-y\e#man \C-y\C-m\C-p\C-p\C-a\C-d\C-e"'
|
||
unset HISTFILE
|
||
|
||
cl() {
|
||
local dir="$1"
|
||
local dir="${dir:=$HOME}"
|
||
if [[ -d "$dir" ]]; then
|
||
cd "$dir" >/dev/null; ls
|
||
else
|
||
echo "bash: cl: $dir: Directory not found"
|
||
fi
|
||
}
|
||
|
||
[[ -f ~/.shrc ]] && . ~/.shrc
|