]> git.ipfire.org Git - people/stevee/network.git/blame - src/functions/functions.colors
ipsec-connection: add color support
[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
5bcadc60
JS
154 ipsec-connection)
155 echo "${NETWORK_IPSEC_CONNS_DIR}/${name}/color"
156 ;;
410d2e85
JS
157 zone)
158 echo "$(zone_dir ${name})/color"
159 ;;
160 port)
161 echo "$(port_dir ${name})/color"
162 ;;
163 esac
164}
165
166color_hex_is_valid() {
167 # Check if a color hex is valid.
168 [[ ${1} =~ ^[0-9a-fA-F]{6}$ ]]
169}
170
171color_hex2rgb() {
172 # Converts a color hex into rgb values.
173 local hex=${1}
174
175 assert [ ${#hex} -eq 6 ]
176
177 for (( i = 0; i < 6; i += 2 )); do
178 hex2dec ${hex:${i}:2}
179 done | tr '\n' ' '
180
181 print # newline
182}
183
184_find_nearest_rgb_value() {
185 # For the calculation of the xterm value the rgb values must be:
186 # 0; 95; 135; 175; 215; 255;
187 # this function find the closest value of these 6 numbers for a give rgb number
188 local rgb=${1}
189
190 local best_value
191 local best_value_index
192
193 local values=( 0 95 135 175 215 255 )
194 local result
195 local i=0
196
197 local value
198 for value in ${values[@]}; do
199 result=$(( ${value} - ${rgb} ))
200 result=$(abs ${result})
201
202 if [ -z ${best_value} ]; then
203 best_value=${result}
204 best_value_index=${i}
205
206 # In the first iteration best_value is empty and so set to ${result}
207 # two lines above. So if statement must use -le because in the first iteration
208 # is the best_value eqal to result
209 elif [ ${result} -le ${best_value} ]; then
210 best_value=${result}
211 best_value_index=${i}
212 fi
213
214 (( i++ ))
215 done
216
217 echo "${best_value_index}"
218}
219
220color_rgb2shell() {
221 # Converts a rgb value triple into an xterm color code.
222 assert [ $# -eq 3 ]
223
224 local red=${1}
225 local green=${2}
226 local blue=${3}
227
228 local color
229 for color in red green blue; do
230 printf -v "${color}" $(_find_nearest_rgb_value ${!color})
231 done
232
233 print $(( 16 + 36 * ${red} + 6 * ${green} + ${blue} ))
234}
235
236color_set_shell() {
237 # Set the shell color which unfourtunately does not work for putty.
238 local where=${1}
239 local color=${2}
240
241 local prefix
242 case "${where}" in
243 fg)
244 prefix="\e[38"
245 ;;
246 bg)
247 prefix="\e[48"
248 ;;
249 esac
250
251 # Convert color from hex to RGB
252 local red green blue
253 read red green blue <<< $(color_hex2rgb ${color})
254
255 # Set standard shell color
256 local shell_color=$(color_rgb2shell ${red} ${green} ${blue})
257 printf "${prefix};5;${shell_color}m"
258
259 # For shells that support it, we will try to set the RGB color code
260 case "${TERM}" in
261 putty*)
262 # PuTTY is a piece of garbage and does not know
263 # how to handle colors at all although it has nice
264 # checkboxes to enable them, but they actually make
265 # things even worse. So no colors for you Windows
266 # users.
267 ;;
268 *)
269 printf "${prefix};2;${red};${green};${blue}m"
270 ;;
271 esac
272}