#!/bin/bash
#
# Dynamic Bash Prompt
#   By: Jared Warren <jawarren@trentu.ca>
#
#   Features:
#     - ice-theme colours drawing focus appropriately
#     - upper-right floating clock
#     - command numbering with colour signification of errors
#
#   To use:
#     save as ~/.ps1
#     echo 'source ~/.ps1' >> ~/.bashrc
#
#   Warning:
#     On a slower machine you should hardcode variables
#     and collapse functions to speed execution.

# set_hist_color()
# > set HIST_COLOR variable based on exit value
function set_hist_color {
	# $? - exit value of last command
	if [ $? -gt 0 ]
	then
		HIST_COLOR='1;31' # success
	else
		HIST_COLOR='1;30' # failure
	fi
}

# display_clock()
# > display a clock in the upper-righthand corner
function display_clock {
	LIGHT_BLUE="$(tput setaf 4; tput bold)"
	DEFAULT="$(tput sgr0)"

	CLOCK="[$LIGHT_BLUE$(date +%H:%M:%S)$DEFAULT]"

	# <columns in display> - len($CLOCK)
	let UPPER_RIGHT=$(tput cols)-10

	tput sc
	tput cup 0 ${UPPER_RIGHT}
	echo -n ${CLOCK}
	tput rc
}

# prompt_command()
# > commands to run before each prompt display
function prompt_command {
	set_hist_color
	display_clock
}

function ps1 {
	# Colour Macros
 	BLUE='\[\033[0;34m\]'
 	LIGHT_CYAN='\[\033[1;36m\]'
 	WHITE='\[\033[1;37m\]'
 	DEFAULT='\[\033[0m\]'

	# Pre-Prompt Function
	PROMPT_COMMAND=prompt_command

	# Prompt
	# \! - history number of last command
	# $? - exit value of last command
	# \t - exit time of last command
	# \w - current directory (relative)
 	PS1="$BLUE[\[\033[\$(echo -n \$HIST_COLOR)m\]\!$BLUE] $WHITE\w$LIGHT_CYAN\$ $DEFAULT"
}

ps1
