File: colors.sh

package info (click to toggle)
libbash 0.9.11-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 776 kB
  • sloc: sh: 1,402; makefile: 53
file content (114 lines) | stat: -rw-r--r-- 2,802 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#               Do not run this script directly!
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#
###########################################################################
# Copyright (c) 2004-2009 Hai Zaar and Gil Ran                            #
#                                                                         #
# This program is free software; you can redistribute it and/or modify it #
# under the terms of version 3 of the GNU General Public License as       #
# published by the Free Software Foundation.                              #
#                                                                         #
###########################################################################
#
# $Date: 2009-05-10 20:08:10 +0300 (Sun, 10 May 2009) $
# $Author: hai-zaar $
#
# Simple library that implements interface for colorfull tty printouts
#

#-------------------------------------------------------------------------
#EXPORT=colorSet colorReset colorPrint colorPrintN
#REQUIRE=
#-------------------------------------------------------------------------

# Internal constants
__colors_GREEN="\\033[1;32m"
__colors_RED="\\033[1;31m"
__colors_YELLOW="\\033[1;33m"
__colors_WHITE="\\033[0;39m"
__colors_DefaultIndent=60


###################
#### FUNCTIONS ####
###################

#
# __colors_ident __colors_ident [INDENT]
#
#	Shifts  curret to INDENT position.
#
#	Parameters:
#		INDENT	- Column to indent to. Defaults to 60.
__colors_ident() 
{
	local DefaultIndent=60
	local INDENT=${1:-$DefaultIndent}
	
	echo -en "\\033[${INDENT}G"
}

#
# colorSet colorSet <COLOR>
#
#	Sets the color of the tty prints to COLOR.
#	
#	Parameters:
#		COLOR	- The new color for tty prints.
colorSet()
{
	eval "local WantedColor=\"\$__colors_$(echo $1 | tr a-z A-Z)\""
	if [[ WantedColor == "" ]] ; then
		echo "colors: Warning: Color $1 is not listed in the colors list." 1>&2
		return 1
	fi
	
	echo -en "$WantedColor"
}

#
# colorReset colorReset
#
#	Resets tty color to normal
#	
colorReset()
{
	#  Reset text attributes to normal
	tput sgr0
}

#
# colorPrint colorPrint [INDENT] <COLOR> <TEXT> 
#
#	Prints TEXT in the color COLOR while shifting curret to INDENT
#	
#	Parameters:
#		COLOR	- The color for the tty print.
#		TEXT	- The text to be printed in the color COLOR.
#		INDENT	- Move curret to INDENT before printing
colorPrint()
{

	# If INDENT parameter given - respect it.
	echo $1 |grep -q '^[0-9][0-9]*$' && __colors_ident $1 && shift
	
	local COLOR=$1
	shift
	colorSet $COLOR || return 1
	echo -en "$@"

	#  Reset text attributes to normal
	tput sgr0
}

#
# colorPrintN colorPrintN [INDENT] <COLOR> <TEXT> 
#	Same as colorPrint but prints trailing \n as well
#
colorPrintN()
{
	colorPrint $*
	echo
}