Skip to content

Instantly share code, notes, and snippets.

@bitnetwork
Last active October 30, 2018 05:25
Show Gist options
  • Save bitnetwork/c617ebab865db0f57f183233ce8363c5 to your computer and use it in GitHub Desktop.
Save bitnetwork/c617ebab865db0f57f183233ce8363c5 to your computer and use it in GitHub Desktop.
My custom bash prompt and my bash run command script. ps1 should be sourced by your .bashrc
PROMPT_COMMAND=_prompt_command
_prompt_command() {
local exit="$?"
PS1="$exit"
# Use unicode characters in prompt
ALTERNATE_CURVE=0
# color code $1
# 0: black
# 1: red
# 2: green
# 3: yellow
# 4: blue
# 5: purple
# 6: cyan
# 7: white
# +
# 30: foreground
# 40: background
# bold $2
# 0: not bold
# 1: bold
# print bash escapes $3
# blank: print
# else: don't
_color() {
if [ -z "$3" ]; then
echo -e -n "\[" # or \001
fi
if [ -v $1 ]; then
echo -e -n "\e[0m"
else
echo -e -n "\e[$2;$1m"
fi
if [ -z "$3" ]; then
echo -e -n "\]" # or \002
fi
}
# user hostname time cpu freemem title jobs
# ┌─[bitbyte@localhost]──[12:30:00]──[0.05:120000]──[pts/1]─[2]
# └─[dir]─[127]─
# cwd error
# user is green or bg red if root
# hostname is blue or cyan if sshing
# time is purple and is 24 hour
# cpu is red and cpu load 1 min
# freemem is blue and free memory kB
# title is cyan and $PT otherwise tty
# jobs is yellow and amount of running jobs
# cwd is red and is basename of cwd or ~ if $HOME
# error is yellow and is previous command error
PS1=""
if [ "$ALTERNATE_CURVE" -eq 1 ]; then
PS1+="$(_color 42)╭─$(_color)[" # box for username@hostname
else
PS1+="$(_color 42)┌─$(_color)[" # box for username@hostname
fi
if [ "$EUID" -eq 0 ]; then # check if root
PS1+="$(_color 41 1)" # background red if root
else
PS1+=$(_color 32 1) # green otherwise
fi
PS1+="$(whoami)" # username
PS1+="$(_color)@"
if [ -n "$SSH_CLIENT" ] || [ -n "$SSH_TTY" ]; then
PS1+="$(_color 36 1)$(hostname -s)" # cyan if sshing
else
PS1+="$(_color 34 1)$(hostname -s)" # blue otherwise
fi
PS1+="$(_color)]" # close box for username@hostname
PS1+="──[" # box for time
PS1+="$(_color 35 1)$(date '+%H:%M:%S')" # date
PS1+="$(_color)]" # close box for time
PS1+="──[" # box for cpuinfo
if [ -r /proc/loadavg ]; then
PS1+="$(_color 31 1)$(grep '[0-9|\.|\/]\+' /proc/loadavg -o | sed -n 1p)"
fi
if [ -r /proc/loadavg ] && [ -r /proc/meminfo ]; then
PS1+="$(_color):"
fi
if [ -r /proc/meminfo ]; then
PS1+="$(_color 34 1)$(grep MemFree /proc/meminfo | grep -o '[0-9]*')"
fi
PS1+="$(_color)]" # close box for cpuinfo
PS1+="──[" # box for title
if [ "$PT" != "" ]; then # title
PS1+="$(_color 36 1)${PT}" # use PT env variable
else
PS1+="$(_color 36 1)$(basename $(dirname $(tty)))/$(basename $(tty))" # default to tty id
fi
PS1+="$(_color)]" # close box for title
if [ $(jobs -p | wc -l) -ne 0 ]; then
PS1+="─["
PS1+="$(_color 33 1)$(jobs -p | wc -l)"
PS1+="$(_color)]"
fi
#PS1+="$(echo -e -n "\[")\n$(echo -e -n "\]")"
PS1+="\n" # next line
if [ "$ALTERNATE_CURVE" -eq 1 ]; then
PS1+="$(_color 46)╰─$(_color)[" # next line and box for working directory
else
PS1+="$(_color 46)└─$(_color)[" # next line and box for working directory
fi
PS1+="$(_color 31 1)$(basename "${PWD/$HOME/'~'}")" # make home tilde and use base directories
# PS1+="$(_color 31 1)$(basename "$PWD")" # use base normal directories
# PS1+="$(_color 31 1)${PWD/$HOME/'~'}" # use nornal directories with home
PS1+="$(_color)]─"
if [ $exit -ne 0 ]; then
PS1+="["
PS1+="$(_color 33 1)$exit"
PS1+="$(_color)]"
fi
PS1+="$(_color)─[" # end shell
if [ "$ALTERNATE_CURVE" -eq 1 ]; then
PS2="$(_color 45)╰─$(_color)[" # continuation prompt (for unmatched quotes)
else
PS2="$(_color 45)└─$(_color)[" # continuation prompt (for unmatched quotes)
fi
# Additional styling
# Cursor block color
PS1+="$(echo -e "\[")\e]12;#ff8000\a$(echo -e "\]")"
# Use a blinking cursor block
# PS1+="\e[1 q"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment