Skip to content

Commit e7550ed

Browse files
committed
Changed the "--colors" option to accept "yes", "no" and "auto" as values, defaulting to "auto", which automatically turns on coloring on UNIX and Windows+ANSICON;
Escape the "escape" character when outputting it with colors enabled; Added back the ".git" registration for Console_Color.
1 parent a156cea commit e7550ed

File tree

2 files changed

+53
-29
lines changed

2 files changed

+53
-29
lines changed

data/roscon.xml

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,23 @@ Defaults to PHP's default_socket_timeout ini option.</description>
5151
</option>
5252
<option name="colors">
5353
<long_name>--colors</long_name>
54-
<description>Turn on color output.</description>
55-
<action>StoreTrue</action>
54+
<description>Choose whether to color output. Possible values:
55+
"auto" - color is always enabled on UNIX operating systems, and on Windows if ANSICON is installed (detected via the ANSICON_VER environment variable).
56+
"yes" - force colored output.
57+
"no" - force no coloring of output.
58+
(Default: "auto")</description>
59+
<action>StoreString</action>
60+
<choices>
61+
<choice>auto</choice>
62+
<choice>yes</choice>
63+
<choice>no</choice>
64+
</choices>
5665
</option>
5766
<option name="size">
5867
<short_name>-w</short_name>
5968
<long_name>--width</long_name>
60-
<description>Width of console screen. Used in verbose mode to wrap output in this length.</description>
69+
<description>Width of console screen. Used in verbose mode to wrap output in this length.
70+
(Default: 80)</description>
6171
<action>StoreInt</action>
6272
</option>
6373
<option name="commandMode">

scripts/roscon.php

Lines changed: 40 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
Autoload::initialize(realpath('../src'));
6060
Autoload::initialize(realpath('../../Net_Transmitter.git/src'));
6161
Autoload::initialize(realpath('../../Cache_SHM.git/src'));
62+
Autoload::initialize(realpath('../../Console_Color.git/src'));
6263
} else {
6364
fwrite(
6465
STDERR,
@@ -116,6 +117,32 @@
116117
$cmdParser->displayUsage(13);
117118
}
118119

120+
$cmd->options['colors'] = $cmd->options['colors'] ?: 'auto';
121+
$cmd->options['size'] = $cmd->options['size'] ?: 80;
122+
$cmd->options['commandMode'] = $cmd->options['commandMode'] ?: 's';
123+
$cmd->options['replyMode'] = $cmd->options['replyMode'] ?: 's';
124+
$comTimeout = null === $cmd->options['conTime']
125+
? (null === $cmd->options['time']
126+
? (int)ini_get('default_socket_timeout')
127+
: $cmd->options['time'])
128+
: $cmd->options['conTime'];
129+
$cmd->options['time'] = $cmd->options['time'] ?: 3;
130+
$comContext = null === $cmd->options['caPath']
131+
? null
132+
: stream_context_create(
133+
is_file($cmd->options['caPath'])
134+
? array(
135+
'ssl' => array(
136+
'verify_peer' => true,
137+
'cafile' => $cmd->options['caPath'])
138+
)
139+
: array(
140+
'ssl' => array(
141+
'verify_peer' => true,
142+
'capath' => $cmd->options['caPath'])
143+
)
144+
);
145+
119146
$c_colors = array(
120147
'SEND' => '',
121148
'SENT' => '',
@@ -124,7 +151,11 @@
124151
'NOTE' => '',
125152
'' => ''
126153
);
127-
if ($cmd->options['colors']) {
154+
if ('auto' === $cmd->options['colors']) {
155+
$cmd->options['colors'] = (strtoupper(substr(PHP_OS, 0, 3)) !== 'WIN'
156+
|| getenv('ANSICON_VER') != false) ? 'yes' : 'no';
157+
}
158+
if ('yes' === $cmd->options['colors']) {
128159
$c_colors['SENT'] = new Color(
129160
Color\Fonts::BLACK,
130161
Color\Backgrounds::PURPLE
@@ -150,31 +181,6 @@
150181
}
151182
}
152183

153-
$cmd->options['size'] = $cmd->options['size'] ?: 80;
154-
$cmd->options['commandMode'] = $cmd->options['commandMode'] ?: 's';
155-
$cmd->options['replyMode'] = $cmd->options['replyMode'] ?: 's';
156-
$comTimeout = null === $cmd->options['conTime']
157-
? (null === $cmd->options['time']
158-
? (int)ini_get('default_socket_timeout')
159-
: $cmd->options['time'])
160-
: $cmd->options['conTime'];
161-
$cmd->options['time'] = $cmd->options['time'] ?: 3;
162-
$comContext = null === $cmd->options['caPath']
163-
? null
164-
: stream_context_create(
165-
is_file($cmd->options['caPath'])
166-
? array(
167-
'ssl' => array(
168-
'verify_peer' => true,
169-
'cafile' => $cmd->options['caPath'])
170-
)
171-
: array(
172-
'ssl' => array(
173-
'verify_peer' => true,
174-
'capath' => $cmd->options['caPath'])
175-
)
176-
);
177-
178184
try {
179185
$com = new RouterOS\Communicator(
180186
$cmd->args['hostname'],
@@ -327,6 +333,9 @@
327333
for ($i = 0, $l = count($wordFragments); $i < $l; $i += 2) {
328334
unset($wordFragments[$i]);
329335
}
336+
if ('' !== $c_colors['']) {
337+
$wordFragments = str_replace("\033", "\033[27@", $wordFragments);
338+
}
330339

331340
$isAbnormal = 'ERR' === $mode || 'NOTE' === $mode;
332341
if ($isAbnormal) {
@@ -385,6 +394,11 @@
385394
);
386395
}
387396
: function ($mode, $word, $msg = '') use ($c_colors) {
397+
if ('' !== $c_colors['']) {
398+
$word = str_replace("\033", "\033[27@", $word);
399+
$msg = str_replace("\033", "\033[27@", $msg);
400+
}
401+
388402
if ('ERR' === $mode || 'NOTE' === $mode) {
389403
fwrite(STDERR, "{$c_colors[$mode]}-- {$msg}");
390404
if ('' !== $word) {

0 commit comments

Comments
 (0)