shell-base/modules/utility/fancy-prompt.func

86 lines
2.1 KiB
Plaintext
Raw Permalink Normal View History

#!/bin/bash
# load module and call fancy_prompt
# see
# used http://bashrcgenerator.com/
# and
# https://unix.stackexchange.com/questions/148/colorizing-your-terminal-and-shell-environment/174#174
# TODO move parse_git_branch, check for git before returning a value
function parse_git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
}
function set_prompt_colors () {
export TERM=xterm-color
export CLICOLOR=1
export LSCOLORS=ExFxCxDxBxegedabagacad
export COLOR_NC='\e[0m' # No Color
export COLOR_BLACK='\e[0;30m'
export COLOR_GRAY='\e[1;30m'
export COLOR_RED='\e[0;31m'
export COLOR_LIGHT_RED='\e[1;31m'
export COLOR_GREEN='\e[0;32m'
export COLOR_LIGHT_GREEN='\e[1;32m'
export COLOR_BROWN='\e[0;33m'
export COLOR_YELLOW='\e[1;33m'
export COLOR_BLUE='\e[0;34m'
export COLOR_LIGHT_BLUE='\e[1;34m'
export COLOR_PURPLE='\e[0;35m'
export COLOR_LIGHT_PURPLE='\e[1;35m'
export COLOR_CYAN='\e[0;36m'
export COLOR_LIGHT_CYAN='\e[1;36m'
export COLOR_LIGHT_GRAY='\e[0;37m'
export COLOR_WHITE='\e[1;37m'
}
function fancy_prompt () {
# if [ -x /usr/bin/tput ] && tput setaf 1 >&/dev/null; then
set_prompt_colors
function c () {
local color
name=COLOR_$1
color=${!name}
echo '\['$color'\]'
}
case $TERM in
xterm*|rxvt*)
# local TITLEBAR='\[\033]0;\u ${NEW_PWD}\007\]'
local PARENTBASE='${PWD#"${PWD%/*/*}/"}'
;;
*)
local PARENTBASE=''
# local TITLEBAR=""
;;
esac
local UC=WHITE # user's color
local MC=CYAN # machine color
[[ $SSH_SESSION == "true" ]] && UC=LIGHT_PURPLE && MC=WHITE # remote machine color
[ $UID -eq "0" ] && UC=LIGHT_RED # root's color
# PS1="$PARENTBASE\[${UC}\]\u@\h: \[${COLOR_LIGHT_GREEN}\]→\[${COLOR_NC}\] "
local user
local machine
local dir
local branch
local promptc
user="$(c $UC)\u$(c GREY)@$(c NC)"
machine="$(c $MC)\h$(c GREY):$(c NC)"
dir="$(c YELLOW)[${PARENTBASE}]$(c NC)"
branch=$(c LIGHT_BLUE)'$(parse_git_branch)'$(c NC)
promptc=$(c GREEN)' $ '$(c NC)
PS1=$(echo "$user$machine$dir$branch$promptc")
# echo $PS1
}