Skip to content

Instantly share code, notes, and snippets.

@bradynpoulsen
Last active August 29, 2015 13:56
Show Gist options
  • Save bradynpoulsen/8815536 to your computer and use it in GitHub Desktop.
Save bradynpoulsen/8815536 to your computer and use it in GitHub Desktop.
Bash :: Exit status indicator in command line prompt
# This is a nearly identical copy and paste how I have my status indicators work in terminal
export PROMPT_COMMAND=prompt
function prompt {
local EXIT=$? # This MUST be first to get exit code of last command
# Text Colors, I chose to use tput to get my colors
local BL=`tput setaf 0` # Black
local RD=`tput setaf 1` # Red
local GR=`tput setaf 2` # Green
local YE=`tput setaf 3` # Yellow
local BU=`tput setaf 4` # Blue
local MA=`tput setaf 5` # Magenta
local CY=`tput setaf 6` # Cyan
local WH=`tput setaf 7` # White
# Background Colors
local BLBG=`tput setab 0` # Black
local RDBG=`tput setab 1` # Red
local GRBG=`tput setab 2` # Green
local YEBG=`tput setab 3` # Yellow
local BUBG=`tput setab 4` # Blue
local MABG=`tput setab 5` # Magenta
local CYBG=`tput setab 6` # Cyan
local WHBG=`tput setab 7` # White
local ESC=`tput sgr0` # Escape text styles
# process your chosen indicator code(s) with $'..' syntax
# To get a status indicator shape:
# 1. Copy the shape to your clipboard
# 2. Run the command 'echo -n "<symbol(s)>" | hexdump'
# This will return something like:
# 0000000 e2 9c 88
# 0000003
# 3. Copy the " e2 9c 88" and replace the spaces with \x => \xe2\x9c\x88
# 4. Save the new character string in a variable like:
# local SYMBOL=$'\xe2\x9c\x88'
# == Now your symbol can be used in your PS1/PS2 string like "${SYMBOL}" ==
local AP=$'\xe2\x9c\x88' # Airplane
local RA=$'\xe2\x98\xa2' # Radioactive
local PF=$'\x28\xe0\xb8\x87\x27\xcc\x80\x2d\x27\xcc\x81\x29\xe0\xb8\x87' # Pissed off face
if [[ $EXIT -eq "0" ]]; then # if last command exited successfully
export PS1="${PS1} ${GR}${RA} ${WH}${AP} " # outputs " (green)☢ (white)✈"
else
export PS1="${PS1} ${RD}${PF} ${WH}${AP} " # outputs " (red)<MyFaceSymbol> (white)✈"
fi
export PS2="${WH}>${ESC}"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment