]> git.ipfire.org Git - ipfire-3.x.git/blame - tools/make-beautify
Added new package: dosfstools.
[ipfire-3.x.git] / tools / make-beautify
CommitLineData
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
29if [ -z "${COLUMNS}" ]; then
ced361db 30 COLUMNS=$(stty size 2>/dev/null)
98be0467
MT
31 COLUMNS=${COLUMNS##* }
32fi
33
34# When using remote connections, such as a serial port, stty size returns 0
35if [ "${COLUMNS}" = "0" ]; then
36 COLUMNS=80
37fi
38
39## Measurements for positioning result messages
40RESULT_WIDTH=4
41TIME_WIDTH=8
42OPT_WIDTH=7
43VER_WIDTH=10
44RESULT_COL=$((${COLUMNS} - $RESULT_WIDTH - 4))
45TIME_COL=$((${RESULT_COL} - $TIME_WIDTH - 5))
46VER_COL=$((${TIME_COL} - $VER_WIDTH - 5))
47OPT_COL=$((${VER_COL} - $OPT_WIDTH - 5))
48
49## Set Cursur Position Commands, used via echo -e
50SET_RESULT_COL="\\033[${RESULT_COL}G"
51SET_TIME_COL="\\033[${TIME_COL}G"
52SET_VER_COL="\\033[${VER_COL}G"
53SET_OPT_COL="\\033[${OPT_COL}G"
54
55# Normal colors
56CLR_NORM_BLK="\\033[0;30m" # black
57CLR_NORM_RED="\\033[0;31m" # red
58CLR_NORM_GRN="\\033[0;32m" # green
59CLR_NORM_YEL="\\033[0;33m" # yellow
60CLR_NORM_BLU="\\033[0;34m" # blue
61CLR_NORM_MAG="\\033[0;35m" # magenta
62CLR_NORM_CYN="\\033[0;36m" # cyan
63CLR_NORM_WHT="\\033[0;37m" # white
64CLR_NORM_GRY="\\033[0;39m" # grey
65
66# Emphased colors
67CLR_BOLD_BLK="\\033[1;30m" # black
68CLR_BOLD_RED="\\033[1;31m" # red
69CLR_BOLD_GRN="\\033[1;32m" # green
70CLR_BOLD_YEL="\\033[1;33m" # yellow
71CLR_BOLD_BLU="\\033[1;34m" # blue
72CLR_BOLD_MAG="\\033[1;35m" # magenta
73CLR_BOLD_CYN="\\033[1;36m" # cyan
74CLR_BOLD_WHT="\\033[1;37m" # white
75CLR_BOLD_GRY="\\033[1;39m" # grey
76
77# Background colors
78CLR_BACK_BLK="\\033[40m" # black
79CLR_BACK_RED="\\033[41m" # red
80CLR_BACK_GRN="\\033[42m" # green
81CLR_BACK_YEL="\\033[43m" # yellow
82CLR_BACK_BLU="\\033[44m" # blue
83CLR_BACK_MAG="\\033[45m" # magenta
84CLR_BACK_CYN="\\033[46m" # cyan
85CLR_BACK_WHT="\\033[47m" # white
86
87# Action colors
88BOLD=$CLR_BOLD_GRY
89DONE=$CLR_BOLD_GRN
90SKIP=$CLR_BOLD_BLU
91WARN=$CLR_BOLD_MAG
92FAIL=$CLR_BOLD_RED
93NORMAL=$CLR_NORM_GRY
94
95# Color hooks
96BRACKET_L="${CLR_BOLD_BLU}[${NORMAL}"
97BRACKET_R="${CLR_BOLD_BLU}]${NORMAL}"
98
99position_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
128beautify() {
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
187get_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
208dialogerror() {
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()