Skip to content

Commit 0f20b7c

Browse files
author
Marius Burkard
committed
- colorize output in installer
1 parent ac7375f commit 0f20b7c

File tree

3 files changed

+176
-12
lines changed

3 files changed

+176
-12
lines changed

install/install.php

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -57,19 +57,21 @@
5757
*/
5858

5959
error_reporting(E_ALL|E_STRICT);
60+
require_once realpath(dirname(__FILE__)) . '/lib/classes/libbashcolor.inc.php';
6061

6162
define('INSTALLER_RUN', true);
6263

6364
//** The banner on the command line
6465
echo "\n\n".str_repeat('-', 80)."\n";
65-
echo " _____ ___________ _____ __ _ ____
66-
|_ _/ ___| ___ \ / __ \ / _(_) /__ \
67-
| | \ `--.| |_/ / | / \/ ___ _ __ | |_ _ __ _ _/ /
68-
| | `--. \ __/ | | / _ \| '_ \| _| |/ _` | |_ |
69-
_| |_/\__/ / | | \__/\ (_) | | | | | | | (_| | ___\ \
70-
\___/\____/\_| \____/\___/|_| |_|_| |_|\__, | \____/
66+
echo PXBashColor::getString( "<darkgrey>
67+
<strong> _____ ___________</strong> _____ __ _ ____
68+
<strong>|_ _/ ___| ___ \</strong> / __ \ / _(_) /__ \
69+
<strong> | | \ `--.| |_/ /</strong> | / \/ ___ _ __ | |_ _ __ _ _/ /
70+
<strong> | | `--. \ __/</strong> | | / _ \| '_ \| _| |/ _` | |_ |
71+
<strong> _| |_/\__/ / | </strong> | \__/\ (_) | | | | | | | (_| | ___\ \
72+
<strong> \___/\____/\_| </strong> \____/\___/|_| |_|_| |_|\__, | \____/
7173
__/ |
72-
|___/ ";
74+
|___/ </darkgrey>");
7375
echo "\n".str_repeat('-', 80)."\n";
7476
echo "\n\n>> Initial configuration \n\n";
7577

@@ -148,12 +150,12 @@
148150
if (empty($retval)) die ("ISPConfig requieres which \n");
149151

150152
swriteln($inst->lng(' Following will be a few questions for primary configuration so be careful.'));
151-
swriteln($inst->lng(' Default values are in [brackets] and can be accepted with <ENTER>.'));
152-
swriteln($inst->lng(' Tap in "quit" (without the quotes) to stop the installer.'."\n\n"));
153+
swriteln($inst->lng(' Default values are in <strong>[brackets]</strong> and can be accepted with <em><ENTER></em>.'));
154+
swriteln($inst->lng(' Tap in "<em>quit</em>" (without the quotes) to stop the installer.'."\n\n"));
153155

154156
//** Check log file is writable (probably not root or sudo)
155157
if(!is_writable(dirname(ISPC_LOG_FILE))){
156-
die("ERROR: Cannot write to the ".dirname(ISPC_LOG_FILE)." directory. Are you root or sudo ?\n\n");
158+
die(PXBashColor::getString("<strong><red>[ERROR]</red></strong> Cannot write to the ".dirname(ISPC_LOG_FILE)." directory. Are you root or sudo ?\n\n"));
157159
}
158160

159161
if(is_dir('/root/ispconfig') || is_dir('/home/admispconfig')) {
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
<?php
2+
3+
/*
4+
(c) 2017 by Marius Burkard, pixcept KG
5+
All rights reserved.
6+
7+
Redistribution and use in source and binary forms, with or without modification,
8+
are permitted provided that the following conditions are met:
9+
10+
* Redistributions of source code must retain the above copyright notice,
11+
this list of conditions and the following disclaimer.
12+
* Redistributions in binary form must reproduce the above copyright notice,
13+
this list of conditions and the following disclaimer in the documentation
14+
and/or other materials provided with the distribution.
15+
* Neither the name of ISPConfig nor the names of its contributors
16+
may be used to endorse or promote products derived from this software without
17+
specific prior written permission.
18+
19+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22+
IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
23+
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24+
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25+
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
26+
OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27+
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
28+
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29+
*/
30+
31+
32+
abstract class PXBashColor {
33+
34+
35+
private static $markers = array(
36+
'black' => 30,
37+
'red' => 31,
38+
'green' => 32,
39+
'yellow' => 33,
40+
'blue' => 34,
41+
'magenta' => 35,
42+
'cyan' => 36,
43+
'lightgrey' => 37,
44+
'default' => 39,
45+
'darkgrey' => 90,
46+
'lightred' => 91,
47+
'lightgreen' => 92,
48+
'lightyellow' => 93,
49+
'lightblue' => 94,
50+
'lightmagenta' => 95,
51+
'lightcyan' => 96,
52+
'white' => 97,
53+
54+
'bg:black' => 40,
55+
'bg:red' => 41,
56+
'bg:green' => 42,
57+
'bg:yellow' => 43,
58+
'bg:blue' => 44,
59+
'bg:magenta' => 45,
60+
'bg:cyan' => 46,
61+
'bg:lightgrey' => 47,
62+
'bg:default' => 49,
63+
'bg:darkgrey' => 100,
64+
'bg:lightred' => 101,
65+
'bg:lightgreen' => 102,
66+
'bg:lightyellow' => 103,
67+
'bg:lightblue' => 104,
68+
'bg:lightmagenta' => 105,
69+
'bg:lightcyan' => 106,
70+
'bg:white' => 107,
71+
72+
'bold' => 1,
73+
'dim' => 2,
74+
'italic' => 3,
75+
'underlined' => 4,
76+
'blink' => 5,
77+
'invert' => 7,
78+
'hidden' => 8
79+
80+
);
81+
82+
private static function getCode($active) {
83+
$code = "\033[0;";
84+
if(count($active) > 0) {
85+
$tmp = array();
86+
for($i = 0; $i < count($active); $i++) {
87+
$tmp[] = self::$markers[$active[$i]];
88+
}
89+
sort($tmp);
90+
$code .= implode(';', $tmp);
91+
unset($tmp);
92+
} else {
93+
$code .= "0";
94+
}
95+
96+
$code .= "m";
97+
return $code;
98+
}
99+
100+
public static function getString($string, $ignore_unknown_tags = false) {
101+
102+
$active = array();
103+
$echo_string = "";
104+
105+
while(preg_match('/<(\/?(?:bg:)?\w+)>/i', $string, $match, PREG_OFFSET_CAPTURE)) {
106+
$pos = $match[0][1];
107+
$tag = $match[1][0];
108+
$len = strlen($match[0][0]);
109+
110+
$close = false;
111+
if(substr($tag, 0, 1) == '/') {
112+
$close = true;
113+
$tag = substr($tag, 1);
114+
}
115+
116+
$key = $tag;
117+
if($key == 'strong' || $key == 'b') $key = 'bold';
118+
elseif($key == 'em' || $key == 'i') $key = 'italic';
119+
elseif($key == 'u') $key = 'underlined';
120+
elseif($key == 'inv') $key = 'invert';
121+
122+
if(!array_key_exists($key, self::$markers)) {
123+
if($ignore_unknown_tags == false) {
124+
throw new Exception('unknown tag: ' . $tag);
125+
} else {
126+
$echo_string .= self::getCode($active);
127+
$echo_string .= substr($string, 0, $pos + $len);
128+
$string = substr($string, $pos + $len);
129+
continue;
130+
}
131+
}
132+
133+
if($pos > 0) {
134+
$echo_string .= self::getCode($active);
135+
$echo_string .= substr($string, 0, $pos);
136+
}
137+
138+
if($close == true) {
139+
$last = end($active);
140+
if($key != $last) {
141+
throw new Exception('unbalanced tag: ' . $tag . ' (' . $last . ' expected), ' . var_export($active, true));
142+
}
143+
array_pop($active);
144+
} else {
145+
array_push($active, $key);
146+
}
147+
148+
$string = substr($string, $pos + $len);
149+
}
150+
151+
if($string != '') {
152+
$echo_string .= self::getCode($active);
153+
$echo_string .= $string;
154+
}
155+
156+
$echo_string .= "\e[0m";
157+
return $echo_string;
158+
}
159+
160+
}

install/lib/install.lib.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232

3333
$FILE = realpath('../install.php');
3434

35+
require_once realpath(dirname(__FILE__)) . '/classes/libbashcolor.inc.php';
36+
3537
//** Get distribution identifier
3638
//** IMPORTANT!
3739
// This is the same code as in server/lib/classes/monitor_tools.inc.php
@@ -214,7 +216,7 @@ function get_distname() {
214216
$distconfid = 'debian90';
215217
$distid = 'debian60';
216218
$distbaseid = 'debian';
217-
swriteln("Operating System: Debian 9.0 (Stretch) or compatible\n");
219+
swriteln("Operating System: <strong>Debian 9.0 (Stretch)</strong> or compatible\n");
218220
} elseif(strstr(trim(file_get_contents('/etc/debian_version')), '/sid')) {
219221
$distname = 'Debian';
220222
$distver = 'Testing';
@@ -379,7 +381,7 @@ function swrite($text = '') {
379381
}
380382

381383
function swriteln($text = '') {
382-
echo $text."\n";
384+
echo PXBashColor::getString($text, true)."\n";
383385
}
384386

385387
function ilog($msg){

0 commit comments

Comments
 (0)