#!/bin/bash
#  color6.0
############################################
##
## color version 6.0
##
## a simple colorname to ansi code translator
## which can be used in themes
##
############################################
## USAGE:
## 
## _bashish_prompt_color [OPTIONS]
## 
## _bashish_prompt_color is a simple string to ansi color translator, it accepts options on the form
## [bg|fg|fgbright|no][color|atributes]
## where color can be any of:
## black, red, green, yellow, blue, magenta, cyan, white
## the colors can also be prefixed by \"fg\" or \"bg\"
## background or foreground
## foreground colors may also be prefixed \"bright\"
## 
## attributes are
## bold, underline, blink, reversed, strikethru/invisible
## the attributes can be prefixed with \"no\" to disable the attribute
##  
## normal ansi color numbers may also be given
## 
## eg.
## _bashish_prompt_color bgblue fgred bold
###############################################

## color 6 changes:
## high intensity colors:
## * bold for Eterm, linux console, aterm
## * 10x, 9x for rxvt-unicode, XTerm, putty
##
## start_unicode breaks the high bit's for linux 
## console even attributes, argh
##
## x3270:
## ibm-3279-4-e: only 3x and attributes, no bg
##
## recursive, unsupported terms?
## 
## unsupported:
## * wyse, colors?
## * VT100 original
## * vt52, colors?
##

## check if the terminal is known to be able to show colors:
case "$TERM" in
vt220|ibm-3279-4-e|vt320|Eterm|linux|rxvt*|iris-ansi|ansi*|xterm*|hpterm|screen|konsole|gnome-terminal|cygwin|interix|pcvt*|con80x*|con100x*|con132x*|console|386at|AT386|qnx*)

	function _bashish_prompt_color {
		typeset ANSI TRANSLATE

		## check what options are avaliable and translate them to ansi color codes.
		
		## main colorchanging function
		## black, red, green, yellow, blue, magenta, cyan, white
		## the colors can also be prefixed by "fg" or "bg"
		## background or foreground
		## foreground colors may also be prefixed "bright"
		##
		## attributes are
		## bold, underline, blink, reversed, strikethru/invisible
		## the attributes can be prefixed with "no" to disable the attribute
		## 
		## normal ansi colors may also be given
		
		## first null the string
		ANSI=""
		TRANSLATE=""
		
		## translate from text to ansi colors	
		_bashish_colorsubfunc ()
		{
			case $TRANSLATE in
			*black)		ANSI="$ANSI"0 ;;
			*red)		ANSI="$ANSI"1 ;;
			*green)		ANSI="$ANSI"2 ;;
			*yellow)	ANSI="$ANSI"3 ;;
			*blue)		ANSI="$ANSI"4 ;;
			*magenta)	ANSI="$ANSI"5 ;;
			*cyan)		ANSI="$ANSI"6 ;;
			*white)		ANSI="$ANSI"7 ;;
			esac
		}
		unset IFS
		for TRANSLATE in "$@"
		do
			case $TRANSLATE in
			[0-9]*)		ANSI="$ANSI;$TRANSLATE" ;;
			normal)		ANSI="$ANSI;0" ;;
			bold) 		ANSI="$ANSI;1" ;;
			underline)	ANSI="$ANSI;4" ;;
			blink)		ANSI="$ANSI;5" ;;
			reversed)	ANSI="$ANSI;7" ;;
			strikethrough)	ANSI="$ANSI;9" ;;
			invisible)	ANSI="$ANSI;9" ;;
			no) 		ANSI="$ANSI;2"
					_bashish_colorsubfunc   ;;
			fgbright*) 
				case ${TERM} in
				Eterm|rxvt|linux*)
					## these terminals do not support bright colors
					ANSI="$ANSI;1;3"
				;;
				*)
					ANSI="$ANSI;9"
				esac
				_bashish_colorsubfunc
			;;
			bgbright*) 
				case "${TERM}" in
				Eterm|rxvt|linux*)
					## these terminals do not support bright colors
					ANSI="$ANSI;4"
				;;
				*)
					ANSI="$ANSI;10"
				esac
				_bashish_colorsubfunc
			;;
			fg*)
				ANSI="$ANSI;3"
				_bashish_colorsubfunc
			;;
			bg*) 
				ANSI="$ANSI;4"
				_bashish_colorsubfunc
			esac
		done
	
		ANSI="\033[0${ANSI}m"
		
		printf "$ANSI"
		unset -f _bashish_colorsubfunc
	}
	_bashish_prompt_color "$@"
;;
*)
function _bashish_prompt_color {
	:
}
esac
