#!/bin/bash
# #!/usr/bin/ksh
# #!/bin/bash

###############################################################################
#                                                                             #
#   bashColors.sh : A script to have visual Shell color table.                #
#                                                                             #
#   +---------------------------------------------------------------------+   #
#   | Copyright (c) 2010 Flyounet                                         |   #
#   +---------------------------------------------------------------------+   #
#   | This program is free software: you can redistribute it and/or       |   #
#   | modify it under the terms of the GNU General Public License as      |   #
#   | published by the Free Software Foundation, either version 3 of the  |   #
#   | License, or (at your option) any later version.                     |   #
#   |                                                                     |   #
#   | This program is distributed in the hope that it will be useful,     |   #
#   | but WITHOUT ANY WARRANTY; without even the implied warranty of      |   #
#   | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the       |   #
#   | GNU General Public License for more details.                        |   #
#   |                                                                     |   #
#   | You should have received a copy of the GNU General Public License   |   #
#   | along with this program.                                            |   #
#   | If not, see <http://www.gnu.org/licenses/>.                         |   #
#   +---------------------------------------------------------------------+   #
#   | Cette œuvre est distribuée SANS AUCUNE GARANTIE hormis celle d'être |   #
#   | distribuée sous les termes de la License Demerdez-vous ("Demerden   |   #
#   | Sie Sich License") plus communément appelée DSSL telle que publiée  |   #
#   | par Flyounet : soit la version 1 de cette licence, soit (à votre    |   #
#   | gré) toute version ultérieure.                                      |   #
#   |                                                                     |   #
#   | Vous devriez avoir reçu une copie de la Licence Démerdez-vous avec  |   #
#   | cette œuvre ; si ce n’est pas le cas, consultez :                   |   #
#   | <http://dssl.flyounet.net/licenses/>.                               |   #
#   +---------------------------------------------------------------------+   #
#   | Author: Flyounet  < dev @@ flyounet . net >                         |   #
#   +---------------------------------------------------------------------+   #
#                                                                             #
###############################################################################
#                                                                             #
# v0.01 [30/03/2010] Flyounet  [@Home] :                                      #
#       > Initiale Release                                                    #
#       > Prerequisites : F01 common.lib (at least v0.24)                     #
# v0.02 [21/04/2010] Flyounet  [@Home] :                                      #
#       > Now Under DSSL & GPLv3                                              #
#       > set ts=4 ; set noai                                                 #
#       + Add information how to quit                                         #
#       * When run with Ksh, it show errors about declare... Erased them &    #
#         use typeset instead.                                                #
# v0.03 [27/04/2010] Flyounet  [@Home] :                                      #
#       + Added the work 'effect' in the rainbowTable function                #
#       > Now use the F01common.lib v0.27                                     #
#                                                                             #
#                                                                             #
###############################################################################
#                                                                             #
# Todo :                                                                      #
#                                                                             #
###############################################################################
#                                                                             #
# Legende :                                                                   #
#  + --> Indique une nouveaute, un ajout de fonctionnalite.                   #
#  * --> Indique une correction de bogue.                                     #
#  - --> Indique la suppression d'une fonctionnalite/variable.                #
#  > --> Indique une information n'ayant pas forcement de rapport avec le code#
#  < --> Indique une amelioration a apporter au code.                         #
# <- --> Indique une amelioration en cours de developpement/realisation.      #
# OK --> Indique qu'une amelioration a ete effectuee.                         #
#                                                                             #
###############################################################################

# As Prerequisites the Library F01 common.lib is needed
. "$(cd ${0%/*}; pwd)/prereqs/common.lib"

#Fxx.01.01
#function : verifyLibs, Verify if needed libraries are well loaded, else quit
#syntaxe  : verifyLibs
#return   : nothing
#notes    : Verify if the F0xVersion exists
#notes    : This function is called just after its definition.
verifyLibs () {
	if [ "F01Version" != "$(command -v "F01Version")" ]
	then
		printf -- "\E[01;31mError !\E[0m Library 'F01common.lib' is not loaded !\n"
		exit 1
	fi
}
verifyLibs

#Fxx.02.03
#function : colorShell, Print to screen Colors and mode effects + a color table
#syntaxe  : colorShell
#return   : nothing
#notes    : 
colorShell () {
	typeset -a _colorList
	typeset -a _attrbList
	_colorList=("black     " "red       " "green     " "yellow    " "blue      " "magenta   " "cyan      " "white     ")
	_attrbList[0]="Cancel all attrb "
	_attrbList[1]="Bright (bold)    "
	_attrbList[2]="Normal (not bold)"
	_attrbList[3]="3 Unknown        "
	_attrbList[4]="Underline        "
	_attrbList[5]="Blink            "
	_attrbList[6]="6 unknown        "
	_attrbList[7]="Reverse video    "
	_attrbList[8]="Concealed (hide) "

	p2s "\033[1;34mThe system recognizes 8 colors (8 in foreground and 8 in background)\033[0m\n"
	p2s "Color Name\tForeground\tBackground"
	_color=30
	while [ ${_color} -lt 38 ]
	do
		let __=${_color}+10
		p2s "${_colorList[$_color-30]}\t\033[0;${_color}m    ${_color}    \033[0m\t\033[0;${__}m    ${__}    \033[0m"
		let _color=${_color}+1
	done

	p2s "\n\033[1;34mThe system recognizes 6 mode effects\033[0m\n"
	_types=0
	while [ ${_types} -lt 9 ]
	do
		if [ ${_types} -ne 3 -a ${_types} -ne 6 ]
		then
			p2s "${_attrbList[${_types}]}\t\t\033[${_types}m    0${_types}    \033[0m"
		fi
		let _types=${_types}+1
	done

	p2s "\n\033[1;34mSimple color table\033[0m\n"
	_chaine=' ABCD abcd 0123  '
	_color=30
	_types=0
	p2s "\033[1;4mattributes\033[0m\033[4m        ${_attrbList[0]} ${_attrbList[1]} ${_attrbList[2]} ${_attrbList[4]} ${_attrbList[5]} ${_attrbList[7]}\033[0m"
	while [ ${_color} -lt 48 ]
	do
		if [ ${_color} -ne 38 -a ${_color} -ne 39 ]
		then
			if [ ${_color} -gt 39 ]
			then
				__=40
			else
				__=30
			fi
			_subString="#${_color} (${_colorList[$_color-$__]}) "
			__=${_types}
			while [ ${__} -lt 8 ]
			do
				if [ ${__} -ne 3 -a ${__} -ne 6 ]
				then
					_subString="${_subString}\E[0${__};${_color}m${_chaine}\E[0m "
				fi
				let __=${__}+1
			done
			p2s "${_subString}"
		fi
		let _color=${_color}+1
	done
}

#Fxx.03.03
#function : rainbowTable, Print to screen a rainbow color table
#syntaxe  : rainbowTable
#return   : nothing
#notes    : 
rainbowTable () {
	typeset -a _colorList
	typeset -a _attrbList
	_colorList=("black  " "red    " "green  " "yellow " "blue   " "magenta" "cyan   " "white  ")
	_attrbList[0]="Cancel all attrb "
	_attrbList[1]="Bright (bold)    "
	_attrbList[2]="Normal (not bold)"
	_attrbList[3]="3 Unknown        "
	_attrbList[4]="Underline        "
	_attrbList[5]="Blink            "
	_attrbList[6]="6 unknown        "
	_attrbList[7]="Reverse video    "
	_attrbList[8]="Concealed (hide) "

	p2s "\033[1;34mRainbow color bash table\033[0m\n"
	_string=' <T3st> '
	_fgColor=30
	_bgColor=40
	typeset -a _subString
	p2s "\033[1mBG colour\033[0m     \033[2m${_colorList[0]}         ${_colorList[1]}         ${_colorList[2]}         ${_colorList[3]}         ${_colorList[4]}         ${_colorList[5]}         ${_colorList[6]}         ${_colorList[7]}\033[0m"
	p2s "\033[2;4m       effect #\\\\033[40m       #\\\\033[41m       #\\\\033[42m       #\\\\033[43m       #\\\\033[44m       #\\\\033[45m       #\\\\033[46m       #\\\\033[47m     \033[0m"
	p2s "\033[1mFG colour\033[0m     \033[40m              \033[0m  \033[41m              \033[0m  \033[42m              \033[0m  \033[43m              \033[0m  \033[44m              \033[0m  \033[45m              \033[0m  \033[46m              \033[0m  \033[47m              \033[0m"
	while [ ${_fgColor} -lt 38 ]
	do
		_subString=("\033[2m${_colorList[$_fgColor-30]}\033[0m   0|1 " "\033[2m#\\\\033[${_fgColor}m\033[0m 2|4 " "\033[4m${_colorList[$_fgColor-30]}   5|7 \033[0m")
		_i=${_bgColor}
		while [ ${_i} -lt 48 ]
		do
			#_subString[0]="${_subString[0]}\033[0;${_fgColor};${_i}m${_string}\033[0m\033[1;${_fgColor};${_i}m${_string}\033[0m  "
			#_subString[1]="${_subString[1]}\033[2;${_fgColor};${_i}m${_string}\033[0m\033[4;${_fgColor};${_i}m${_string}\033[0m  "
			#_subString[2]="${_subString[2]}\033[5;${_fgColor};${_i}m${_string}\033[0m\033[7;${_fgColor};${_i}m${_string}\033[0m  "
			#_subString[0]="${_subString[0]}\033[0;${_fgColor};${_i}m 0;${_fgColor};${_i} \033[0m\033[1;${_fgColor};${_i}m 1;${_fgColor};${_i} \033[0m  "
			#_subString[1]="${_subString[1]}\033[2;${_fgColor};${_i}m 2;${_fgColor};${_i} \033[0m\033[4;${_fgColor};${_i}m 4;${_fgColor};${_i} \033[0m  "
			#_subString[2]="${_subString[2]}\033[5;${_fgColor};${_i}m 5;${_fgColor};${_i} \033[0m\033[7;${_fgColor};${_i}m 7;${_fgColor};${_i} \033[0m  "
			_subString[0]="${_subString[0]}\033[0;${_fgColor};${_i}m ${_fgColor};${_i} \033[0m\033[1;${_fgColor};${_i}m ${_fgColor};${_i} \033[0m  "
			_subString[1]="${_subString[1]}\033[2;${_fgColor};${_i}m ${_fgColor};${_i} \033[0m\033[4;${_fgColor};${_i}m ${_fgColor};${_i} \033[0m  "
			_subString[2]="${_subString[2]}\033[5;${_fgColor};${_i}m ${_fgColor};${_i} \033[0m\033[7;${_fgColor};${_i}m ${_fgColor};${_i} \033[0m  "
			let _i=${_i}+1
		done
		p2s "${_subString[0]}\n${_subString[1]}\n${_subString[2]}"
		let _fgColor=${_fgColor}+1
	done
}

clear
colorShell
while true
do
	p2s "\n\n\E[01;31m1\E[0m) Colors, mode effects and light table,        \E[01;31m2\E[0m) Rainbow color table,"
	p2s "\E[01;31mx\E[0m) Exit."
	if [ "x${BASH_VERSION}" != "x" -a "x${KSH_VERSION}" = "x" ]
	then
		read -n 1 -p "Your choice : " _answer
	else
		read  _answer?"Your choice : "
	fi
	clear
	case "${_answer}" in
		[xXqQ]) exit 0 ;;
		2) rainbowTable ;;
		*) colorShell ;;
	esac
done

exit 0

# +---------------------------------------------------------------------------+ #
# | Last subversion informations :                                      
# | $Revision: 10 $ : $Date: 2010-03-29 18:21:12 +0200 (lun. 29 mars 2010) $                                                     
# | $Author: Flyounet $
# +---------------------------------------------------------------------------+ #
