]> git.ipfire.org Git - people/stevee/network.git/blame - src/functions/functions.colors
network fix parameter passing when using ""
[people/stevee/network.git] / src / functions / functions.colors
CommitLineData
1848564d
MT
1#!/bin/bash
2###############################################################################
3# #
4# IPFire.org - A linux based firewall #
5# Copyright (C) 2010 Michael Tremer & Christian Schmidt #
6# #
7# This program is free software: you can redistribute it and/or modify #
8# it under the terms of the GNU General Public License as published by #
9# the Free Software Foundation, either version 3 of the License, or #
10# (at your option) any later version. #
11# #
12# This program is distributed in the hope that it will be useful, #
13# but WITHOUT ANY WARRANTY; without even the implied warranty of #
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
15# GNU General Public License for more details. #
16# #
17# You should have received a copy of the GNU General Public License #
18# along with this program. If not, see <http://www.gnu.org/licenses/>. #
19# #
20###############################################################################
21
e2112169
MT
22# Regular colours
23CLR_BLACK_R='\e[0;30m'
24CLR_RED_R='\e[0;31m'
25CLR_GREEN_R='\e[0;32m'
26CLR_YELLOW_R='\e[0;33m'
27CLR_BLUE_R='\e[0;34m'
28CLR_PURPLE_R='\e[0;35m'
29CLR_CYAN_R='\e[0;36m'
30CLR_WHITE_R='\e[0;37m'
e2112169
MT
31
32# Bold colours
33CLR_BLACK_B='\e[1;30m'
34CLR_RED_B='\e[1;31m'
35CLR_GREEN_B='\e[1;32m'
36CLR_YELLOW_B='\e[1;33m'
37CLR_BLUE_B='\e[1;34m'
38CLR_PURPLE_B='\e[1;35m'
39CLR_CYAN_B='\e[1;36m'
40CLR_WHITE_B='\e[1;37m'
e2112169
MT
41
42# Background colors
43CLR_BLACK_BG='\e[40m'
44CLR_RED_BG='\e[41m'
45CLR_GREEN_BG='\e[42m'
46CLR_YELLOW_BG='\e[43m'
47CLR_BLUE_BG='\e[44m'
48CLR_PURPLE_BG='\e[45m'
49CLR_CYAN_BG='\e[46m'
50CLR_WHITE_BG='\e[47m'
e2112169 51
ded49a6b
MT
52# Font decoration
53FONT_RESET="\e[0m"
54FONT_BOLD="\e[1m"
55FONT_UNDERLINED="\e[4m"
56FONT_BLINKING="\e[5m"
57FONT_INVERTED="\e[7m"
58
59# Reset everything
60CLR_RESET="${FONT_RESET}"
e2112169 61
e2112169 62# Predefined messages
4665dc30
MT
63MSG_HOOK_UP="${CLR_GREEN_BG}${CLR_WHITE_B} UP ${CLR_RESET}"
64MSG_HOOK_DOWN="${CLR_RED_BG}${CLR_WHITE_B} DOWN ${CLR_RESET}"
e2112169 65
4665dc30
MT
66MSG_DEVICE_STATUS_UNKNOWN="${CLR_GREY_BG}${CLR_BLACK_B} UNKNOWN ${CLR_RESET}"
67MSG_DEVICE_STATUS_UP="${CLR_GREEN_BG}${CLR_WHITE_B} UP ${CLR_RESET}"
68MSG_DEVICE_STATUS_DOWN="${CLR_RED_BG}${CLR_WHITE_B} DOWN ${CLR_RESET}"
69MSG_DEVICE_STATUS_NOCARRIER="${CLR_YELLOW_BG}${CLR_WHITE_B} NO-CARRIER ${CLR_RESET}"
e2112169 70
4665dc30
MT
71MSG_STP_FORWARDING="${CLR_GREEN_BG}${CLR_WHITE_B} FORWARDING ${CLR_RESET}"
72MSG_STP_DISCARDING="${CLR_RED_BG}${CLR_WHITE_B} DISCARDING ${CLR_RESET}"
73MSG_STP_LEARNING="${CLR_YELLOW_BG}${CLR_WHITE_B} LEARNING ${CLR_RESET}"
74MSG_STP_LISTENING="${CLR_YELLOW_BG}${CLR_WHITE_B} LISTENING ${CLR_RESET}"
75MSG_STP_BLOCKING="${CLR_RED_BG}${CLR_WHITE_B} BLOCKING ${CLR_RESET}"
410d2e85
JS
76
77color_cli() {
78 # Is the cli function to parse the options submitted by a user.
79 local type=${1}
80 local name=${2}
81 local action=${3}
82
83 case ${action} in
84 set)
85 local color=${4}
86 # Check if we get to many arguments
87 if [ $# -gt 4 ]; then
88 # We want to print only the 5th and greater argument
89 shift 4
90 error "Too many arguments: $@"
91 return ${EXIT_ERROR}
92 fi
93 color_set ${type} ${name} ${color}
94 ;;
95 reset)
96 # We set the color to white.
97 # Check if we get to many arguments
98 shift
99 if [ $# -gt 3 ]; then
100 # We want to print only the 4th and greater argument
101 shift 3
102 error "Too many arguments: $@"
103 return ${EXIT_ERROR}
104 fi
105 color_set ${type} ${name} "ffffff"
106 ;;
107 *)
108 error "Invalid argument: ${action}"
109 ;;
110 esac
111
112}
113
114color_set() {
115 # Write a given color into the color config file of a zone or port.
116 assert [ $# -eq 3 ]
117
118 local type=${1}
119 local name=${2}
120 local COLOR=${3}
121 # Check if we get to many arguments
122 # Check if the color code is valid
123 if ! color_hex_is_valid ${COLOR}; then
124 error "Hexadecimal color code '${COLOR}' is not valid"
125 return ${EXIT_ERROR}
126 fi
127
128 local file=$(color_format_filename ${type} ${name})
129 settings_write ${file} COLOR
130}
131
132color_read() {
133 # Read a color out of color config file of a zone or port.
134 # If this is unsuccessful we use white.
135 local type=${1}
136 local name=${2}
137
138 local file=$(color_format_filename ${type} ${name})
139
140 local COLOR
141
142 if ! settings_read ${file} COLOR; then
143 COLOR="ffffff"
144 fi
145
146 print "${COLOR}"
147}
148
149color_format_filename() {
150 # Formats the color config file name.
151 local type=${1}
152 local name=${2}
153 case ${type} in
154 zone)
155 echo "$(zone_dir ${name})/color"
156 ;;
157 port)
158 echo "$(port_dir ${name})/color"
159 ;;
160 esac
161}
162
163color_hex_is_valid() {
164 # Check if a color hex is valid.
165 [[ ${1} =~ ^[0-9a-fA-F]{6}$ ]]
166}
167
168color_hex2rgb() {
169 # Converts a color hex into rgb values.
170 local hex=${1}
171
172 assert [ ${#hex} -eq 6 ]
173
174 for (( i = 0; i < 6; i += 2 )); do
175 hex2dec ${hex:${i}:2}
176 done | tr '\n' ' '
177
178 print # newline
179}
180
181_find_nearest_rgb_value() {
182 # For the calculation of the xterm value the rgb values must be:
183 # 0; 95; 135; 175; 215; 255;
184 # this function find the closest value of these 6 numbers for a give rgb number
185 local rgb=${1}
186
187 local best_value
188 local best_value_index
189
190 local values=( 0 95 135 175 215 255 )
191 local result
192 local i=0
193
194 local value
195 for value in ${values[@]}; do
196 result=$(( ${value} - ${rgb} ))
197 result=$(abs ${result})
198
199 if [ -z ${best_value} ]; then
200 best_value=${result}
201 best_value_index=${i}
202
203 # In the first iteration best_value is empty and so set to ${result}
204 # two lines above. So if statement must use -le because in the first iteration
205 # is the best_value eqal to result
206 elif [ ${result} -le ${best_value} ]; then
207 best_value=${result}
208 best_value_index=${i}
209 fi
210
211 (( i++ ))
212 done
213
214 echo "${best_value_index}"
215}
216
217color_rgb2shell() {
218 # Converts a rgb value triple into an xterm color code.
219 assert [ $# -eq 3 ]
220
221 local red=${1}
222 local green=${2}
223 local blue=${3}
224
225 local color
226 for color in red green blue; do
227 printf -v "${color}" $(_find_nearest_rgb_value ${!color})
228 done
229
230 print $(( 16 + 36 * ${red} + 6 * ${green} + ${blue} ))
231}
232
233color_set_shell() {
234 # Set the shell color which unfourtunately does not work for putty.
235 local where=${1}
236 local color=${2}
237
238 local prefix
239 case "${where}" in
240 fg)
241 prefix="\e[38"
242 ;;
243 bg)
244 prefix="\e[48"
245 ;;
246 esac
247
248 # Convert color from hex to RGB
249 local red green blue
250 read red green blue <<< $(color_hex2rgb ${color})
251
252 # Set standard shell color
253 local shell_color=$(color_rgb2shell ${red} ${green} ${blue})
254 printf "${prefix};5;${shell_color}m"
255
256 # For shells that support it, we will try to set the RGB color code
257 case "${TERM}" in
258 putty*)
259 # PuTTY is a piece of garbage and does not know
260 # how to handle colors at all although it has nice
261 # checkboxes to enable them, but they actually make
262 # things even worse. So no colors for you Windows
263 # users.
264 ;;
265 *)
266 printf "${prefix};2;${red};${green};${blue}m"
267 ;;
268 esac
269}