Skip to content

Instantly share code, notes, and snippets.

@hjpbarcelos
Created August 6, 2013 00:14
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 hjpbarcelos/6160833 to your computer and use it in GitHub Desktop.
Save hjpbarcelos/6160833 to your computer and use it in GitHub Desktop.
#-------------------------------------------------------------
# Greeting, motd etc. ...
#-------------------------------------------------------------
# Color definitions (taken from Color Bash Prompt HowTo).
# Some colors might look different of some terminals.
# For example, I see 'Bold Red' as 'orange' on my screen,
# hence the 'Green' 'BRed' 'Red' sequence I often use in my prompt.
# Normal Colors
Black=$fg[black] # Black
Red=$fg[red] # Red
Green=$fg[green] # Green
Yellow=$fg[yellow] # Yellow
Blue=$fg[blue] # Blue
Purple=$fg[purple] # Purple
Cyan=$fg[cyan] # Cyan
White=$fg[white] # White
# Bold
BBlack=$fg_bold[black] # Black
BRed=$fg_bold[red] # Red
BGreen=$fg_bold[green] # Green
BYellow=$fg_bold[yellow] # Yellow
BBlue=$fg_bold[blue] # Blue
BPurple=$fg_bold[purple] # Purple
BCyan=$fg_bold[cyan] # Cyan
BWhite=$fg_bold[white] # White
# Background
On_Black='\e[40m' # Black
On_Red='\e[41m' # Red
On_Green='\e[42m' # Green
On_Yellow='\e[43m' # Yellow
On_Blue='\e[44m' # Blue
On_Purple='\e[45m' # Purple
On_Cyan='\e[46m' # Cyan
On_White='\e[47m' # White
NC='\e[m' # Color Reset
ALERT=${BWhite}${On_Red} # Bold White on red background
# echo -e "${NC}This is BASH ${BRed}${BASH_VERSION%.*}${NC}\
# - DISPLAY on ${BRed}$DISPLAY${NC}\n"
# date +"%A, %d de %B de %Y"
# if [ -x /usr/games/fortune ]; then
# /usr/games/fortune -s # Makes our day a bit more fun.... :-)
# fi
function _exit() # Function to run upon exit of shell.
{
echo -e "${BRed}Hasta la vista, baby!${NC}"
}
trap _exit EXIT
#-------------------------------------------------------------
# Shell Prompt - for many examples, see:
# http://www.debian-administration.org/articles/205
# http://www.askapache.com/linux/bash-power-prompt.html
# http://tldp.org/HOWTO/Bash-Prompt-HOWTO
# https://github.com/nojhan/liquidprompt
#-------------------------------------------------------------
# Current Format: [TIME USER@HOST PWD] >
# TIME:
# Green == machine load is low
# Orange == machine load is medium
# Red == machine load is high
# ALERT == machine load is very high
# USER:
# Cyan == normal user
# Orange == SU to user
# Red == root
# HOST:
# Cyan == local session
# Green == secured remote connection (via ssh)
# Red == unsecured remote connection
# PWD:
# Green == more than 10% free disk space
# Orange == less than 10% free disk space
# ALERT == less than 5% free disk space
# Red == current user does not have write privileges
# Cyan == current filesystem is size zero (like /proc)
# >:
# White == no background or suspended jobs in this shell
# Cyan == at least one background job in this shell
# Orange == at least one suspended job in this shell
#
# Command is added to the history file each time you hit enter,
# so it's available to all shells (using 'history -a').
# Test connection type:
if [ -n "${SSH_CONNECTION}" ]; then
CNX=${Green} # Connected on remote machine, via ssh (good).
elif [[ "${DISPLAY%%:*}" != "" ]]; then
CNX=${ALERT} # Connected on remote machine, not via ssh (bad).
else
CNX=${BBlue} # Connected on local machine.
fi
# Test user type:
if [[ ${USER} == "root" ]]; then
SU=${BRed} # User is root.
elif [[ ${USER} != $(logname) ]]; then
SU=${Red} # User is not login user.
else
SU=${BBlue} # User is normal (well ... most of us are).
fi
NCPU=$(grep -c 'processor' /proc/cpuinfo) # Number of CPUs
SLOAD=$(( 100*${NCPU} )) # Small load
MLOAD=$(( 200*${NCPU} )) # Medium load
XLOAD=$(( 400*${NCPU} )) # Xlarge load
# Returns system load as percentage, i.e., '40' rather than '0.40)'.
function load()
{
local SYSLOAD=$(cut -d " " -f1 /proc/loadavg | tr -d '.')
# System load of the current host.
echo $((10#$SYSLOAD)) # Convert to decimal.
}
# Returns a color indicating system load.
function load_color()
{
local SYSLOAD=$(load)
if [ ${SYSLOAD} -gt ${XLOAD} ]; then
echo -en ${ALERT}
elif [ ${SYSLOAD} -gt ${MLOAD} ]; then
echo -en ${BRed}
elif [ ${SYSLOAD} -gt ${SLOAD} ]; then
echo -en ${BYellow}
else
echo -en ${BGreen}
fi
}
# Returns a color according to free disk space in $PWD.
function disk_color()
{
if [ -s "${PWD}" ] ; then
if [ ! -w "${PWD}" ] ; then
echo -en ${BRed}
# No 'write' privilege in the current directory.
else
local used=$(command df -P "$PWD" |
awk 'END {print $5} {sub(/%/,"")}')
if [ ${used} -gt 95 ]; then
echo -en ${ALERT} # Disk almost full (>95%).
elif [ ${used} -gt 90 ]; then
echo -en ${BRed} # Free disk space almost gone.
elif [ ${used} -gt 85 ]; then
echo -en ${BYellow} # Free disk space is low.
else
echo -en ${BGreen} # Free disk space is ok.
fi
fi
else
echo -en ${BCyan}
# Current directory is size '0' (like /proc, /sys etc).
fi
}
# Returns a color according to running/suspended jobs.
function job_color()
{
if [ $(jobs -s | wc -l) -gt "0" ]; then
echo -en ${BRed}
elif [ $(jobs -r | wc -l) -gt "0" ] ; then
echo -en ${BYellow}
else
echo -en ${BGreen}
fi
}
PROMPT='%{$(load_color)%}[%T] %{$SU%}%n@%{$CNX%}%m %{$(disk_color)%}%(!.%1~.%c) $(git_prompt_info)%_%{$(job_color)%}$(prompt_char)%{$reset_color%} '
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment