]>
Commit | Line | Data |
---|---|---|
98be0467 MT |
1 | #!/bin/bash |
2 | ############################################################################### | |
3 | # # | |
4 | # IPFire.org - A linux based firewall # | |
5 | # Copyright (C) 2008 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 | ############################################################################### | |
22 | # | |
23 | # Beautifying variables & presentation & input output interface | |
24 | # | |
25 | ############################################################################### | |
26 | ||
27 | ## Screen Dimentions | |
28 | # Find current screen size | |
29 | if [ -z "${COLUMNS}" ]; then | |
ced361db | 30 | COLUMNS=$(stty size 2>/dev/null) |
98be0467 MT |
31 | COLUMNS=${COLUMNS##* } |
32 | fi | |
33 | ||
34 | # When using remote connections, such as a serial port, stty size returns 0 | |
35 | if [ "${COLUMNS}" = "0" ]; then | |
36 | COLUMNS=80 | |
37 | fi | |
38 | ||
39 | ## Measurements for positioning result messages | |
40 | RESULT_WIDTH=4 | |
41 | TIME_WIDTH=8 | |
42 | OPT_WIDTH=7 | |
43 | VER_WIDTH=10 | |
44 | RESULT_COL=$((${COLUMNS} - $RESULT_WIDTH - 4)) | |
45 | TIME_COL=$((${RESULT_COL} - $TIME_WIDTH - 5)) | |
46 | VER_COL=$((${TIME_COL} - $VER_WIDTH - 5)) | |
47 | OPT_COL=$((${VER_COL} - $OPT_WIDTH - 5)) | |
48 | ||
49 | ## Set Cursur Position Commands, used via echo -e | |
50 | SET_RESULT_COL="\\033[${RESULT_COL}G" | |
51 | SET_TIME_COL="\\033[${TIME_COL}G" | |
52 | SET_VER_COL="\\033[${VER_COL}G" | |
53 | SET_OPT_COL="\\033[${OPT_COL}G" | |
54 | ||
55 | # Normal colors | |
56 | CLR_NORM_BLK="\\033[0;30m" # black | |
57 | CLR_NORM_RED="\\033[0;31m" # red | |
58 | CLR_NORM_GRN="\\033[0;32m" # green | |
59 | CLR_NORM_YEL="\\033[0;33m" # yellow | |
60 | CLR_NORM_BLU="\\033[0;34m" # blue | |
61 | CLR_NORM_MAG="\\033[0;35m" # magenta | |
62 | CLR_NORM_CYN="\\033[0;36m" # cyan | |
63 | CLR_NORM_WHT="\\033[0;37m" # white | |
64 | CLR_NORM_GRY="\\033[0;39m" # grey | |
65 | ||
66 | # Emphased colors | |
67 | CLR_BOLD_BLK="\\033[1;30m" # black | |
68 | CLR_BOLD_RED="\\033[1;31m" # red | |
69 | CLR_BOLD_GRN="\\033[1;32m" # green | |
70 | CLR_BOLD_YEL="\\033[1;33m" # yellow | |
71 | CLR_BOLD_BLU="\\033[1;34m" # blue | |
72 | CLR_BOLD_MAG="\\033[1;35m" # magenta | |
73 | CLR_BOLD_CYN="\\033[1;36m" # cyan | |
74 | CLR_BOLD_WHT="\\033[1;37m" # white | |
75 | CLR_BOLD_GRY="\\033[1;39m" # grey | |
76 | ||
77 | # Background colors | |
78 | CLR_BACK_BLK="\\033[40m" # black | |
79 | CLR_BACK_RED="\\033[41m" # red | |
80 | CLR_BACK_GRN="\\033[42m" # green | |
81 | CLR_BACK_YEL="\\033[43m" # yellow | |
82 | CLR_BACK_BLU="\\033[44m" # blue | |
83 | CLR_BACK_MAG="\\033[45m" # magenta | |
84 | CLR_BACK_CYN="\\033[46m" # cyan | |
85 | CLR_BACK_WHT="\\033[47m" # white | |
86 | ||
87 | # Action colors | |
88 | BOLD=$CLR_BOLD_GRY | |
89 | DONE=$CLR_BOLD_GRN | |
90 | SKIP=$CLR_BOLD_BLU | |
91 | WARN=$CLR_BOLD_MAG | |
92 | FAIL=$CLR_BOLD_RED | |
93 | NORMAL=$CLR_NORM_GRY | |
94 | ||
95 | # Color hooks | |
96 | BRACKET_L="${CLR_BOLD_BLU}[${NORMAL}" | |
97 | BRACKET_R="${CLR_BOLD_BLU}]${NORMAL}" | |
98 | ||
99 | position_cursor() { | |
100 | # ARG1=starting position on screen | |
101 | # ARG2=string to be printed | |
102 | # ARG3=offset, negative for left movement, positive for right movement, relative to ARG1 | |
103 | # For example if your starting position is column 50 and you want to print Hello three columns to the right | |
104 | # of your starting position, your call will look like this: | |
105 | # position_cursor 50 "Hello" 3 (you'll get the string Hello at position 53 (= 50 + 3) | |
106 | # If on the other hand you want your string "Hello" to end three columns to the left of position 50, | |
107 | # your call will look like this: | |
108 | # position_cursor 50 "Hello" -3 (you'll get the string Hello at position 42 (= 50 - 5 -3) | |
109 | # If you want to start printing at the exact starting location, use offset 0 | |
110 | ||
111 | START=$1 | |
112 | STRING=$2 | |
113 | OFFSET=$3 | |
114 | ||
115 | STRING_LENGTH=${#STRING} | |
116 | ||
117 | if [ ${OFFSET} -lt 0 ]; then | |
118 | COL=$((${START} + ${OFFSET} - ${STRING_LENGTH})) | |
119 | else | |
120 | COL=$((${START} + ${OFFSET})) | |
121 | fi | |
122 | ||
123 | SET_COL="\\033[${COL}G" | |
124 | ||
125 | echo $SET_COL | |
126 | } # End of position_cursor() | |
127 | ||
128 | beautify() { | |
129 | # Commands: build_stage, make_pkg, message, result | |
130 | case "$1" in | |
131 | message) | |
132 | MESSAGE="$3" | |
133 | echo -ne "${BOLD}${MESSAGE}${NORMAL}${SET_RESULT_COL}${BRACKET_L}" | |
134 | case "$2" in | |
a2653d81 MT |
135 | DONE) echo -ne " ${DONE}DONE${NORMAL} ";; |
136 | WARN) echo -ne " ${WARN}WARN${NORMAL} ";; | |
137 | FAIL) echo -ne " ${FAIL}FAIL${NORMAL} ";; | |
138 | SKIP) echo -ne " ${SKIP}SKIP${NORMAL} ";; | |
139 | ON|ENAB*) echo -ne " ${DONE}ENAB${NORMAL} ";; | |
140 | OFF|DISA*) echo -ne " ${FAIL}DISA${NORMAL} ";; | |
98be0467 MT |
141 | esac |
142 | echo -ne "${BRACKET_R}\n" | |
143 | ;; | |
144 | build_stage) | |
145 | MESSAGE=$2 | |
146 | echo -ne "${BOLD}*** ${MESSAGE}${SET_OPT_COL} options${SET_VER_COL} version " | |
147 | echo -ne "${SET_TIME_COL} time (sec)${SET_RESULT_COL} status${NORMAL}\n" | |
148 | ;; | |
149 | make_pkg) | |
150 | echo "$2" | while read PKG_VER PROGRAM OPTIONS | |
151 | do | |
152 | SET_VER_COL_REAL=`position_cursor $TIME_COL $PKG_VER -3` | |
153 | ||
154 | echo -ne "${PROGRAM}" | |
155 | if ! [ "$OPTIONS" == "" ]; then | |
156 | echo -ne "${SET_OPT_COL}${BRACKET_L} ${BOLD}${OPTIONS}${NORMAL} ${BRACKET_R}" | |
157 | fi | |
158 | ||
159 | if [ "${PKG_VER}" == "${SNAME}" ] || [ "${PKG_VER}" == "LFS" ]; then | |
160 | echo -ne "${SET_RESULT_COL}" | |
161 | else | |
162 | echo -ne "${SET_VER_COL}${BRACKET_L} ${BOLD}${SET_VER_COL_REAL}${PKG_VER}" | |
163 | echo -ne "${NORMAL} ${BRACKET_R}${SET_RESULT_COL}" | |
164 | fi | |
165 | done | |
166 | ;; | |
167 | result) | |
168 | RESULT=$2 | |
169 | ||
170 | if [ ! $3 ]; then | |
171 | PKG_TIME=0 | |
172 | else | |
173 | PKG_TIME=$3 | |
174 | fi | |
175 | ||
176 | SET_TIME_COL_REAL=`position_cursor $RESULT_COL $PKG_TIME -3` | |
177 | echo -ne "${SET_TIME_COL}${BRACKET_L} ${BOLD}${SET_TIME_COL_REAL}$PKG_TIME${NORMAL} ${BRACKET_R}" | |
178 | case "$RESULT" in | |
a2653d81 MT |
179 | DONE) echo -ne "${SET_RESULT_COL}${BRACKET_L} ${DONE}DONE${NORMAL} ${BRACKET_R}\n";; |
180 | FAIL) echo -ne "${SET_RESULT_COL}${BRACKET_L} ${FAIL}FAIL${NORMAL} ${BRACKET_R}\n";; | |
181 | SKIP) echo -ne "${SET_RESULT_COL}${BRACKET_L} ${SKIP}SKIP${NORMAL} ${BRACKET_R}\n";; | |
98be0467 MT |
182 | esac |
183 | ;; | |
184 | esac | |
185 | } # End of beautify() | |
186 | ||
187 | get_pkg_ver() { | |
faefaede | 188 | PKG_VER=`grep ^PKG_VER $1 | awk '{print $3}'` |
98be0467 MT |
189 | |
190 | if [ -z $PKG_VER ]; then | |
191 | PKG_VER=`grep "Exp " $1 | awk '{print $4}'` | |
192 | fi | |
193 | ||
194 | if [ ${#PKG_VER} -gt $VER_WIDTH ]; then | |
195 | # If a package version number is greater than $VER_WIDTH, we keep the first 4 characters | |
196 | # and replace enough characters to fit the resulting string on the screen. We'll replace | |
197 | # the extra character with .. (two dots). That's why the "+ 2" in the formula below. | |
198 | # Example: if we have a 21-long version number that we want to fit into a 10-long space, | |
199 | # we have to remove 11 characters. But if we replace 11 characters with 2 characters, we'll | |
200 | # end up with a 12-character long string. That's why we replace 12 characters with .. | |
201 | REMOVE=`expr substr "$PKG_VER" 4 $[ ${#PKG_VER} - $VER_WIDTH + 2 ]` | |
202 | PKG_VER=`echo ${PKG_VER/$REMOVE/..}` | |
203 | fi | |
204 | ||
205 | echo "$PKG_VER" | |
206 | } # End of get_pkg_ver() | |
207 | ||
208 | dialogerror() { | |
209 | beautify message FAIL | |
210 | ||
211 | echo -ne "${FAIL}ERROR${NORMAL}: ${BOLD}$*${NORMAL}\n" | |
212 | [ -z "$LOGFILE" ] || \ | |
213 | echo " Check $LOGFILE for errors if applicable" | |
214 | } # End of dialogerror() |