Skip to content

Commit 9d7a3fa

Browse files
committed
add console code Highlighter tool, from 'jakub-onderka/php-console-highlighter'
1 parent 478e46d commit 9d7a3fa

File tree

12 files changed

+502
-61
lines changed

12 files changed

+502
-61
lines changed

examples/Controllers/HomeController.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Inhere\Console\Components\Download;
88
use Inhere\Console\Controller;
99
use Inhere\Console\IO\Input;
10+
use Inhere\Console\Style\Highlighter;
1011
use Inhere\Console\Utils\Helper;
1112
use Inhere\Console\Utils\Interact;
1213
use Inhere\Console\Utils\ProgressBar;
@@ -34,6 +35,7 @@ protected static function commandAliases()
3435
'pwd' => 'password',
3536
'l' => 'list',
3637
'h' => 'helpPanel',
38+
'hl' => 'highlight',
3739
'hp' => 'helpPanel',
3840
'af' => 'artFont',
3941
'ml' => 'multiList',
@@ -73,6 +75,24 @@ public function testCommand()
7375
$this->write('hello, welcome!! this is ' . __METHOD__);
7476
}
7577

78+
/**
79+
* a example for highlight code
80+
* @options
81+
* --ln With line number
82+
* @param Input $in
83+
*/
84+
public function highlightCommand($in)
85+
{
86+
// $file = $this->app->getRootPath() . '/examples/routes.php';
87+
$file = $this->app->getRootPath() . '/src/Utils/Show.php';
88+
$src = file_get_contents($file);
89+
90+
$hl = new Highlighter();
91+
$code = $hl->highlight($src, $in->getBoolOpt('ln'));
92+
93+
$this->output->writeRaw($code);
94+
}
95+
7696
/**
7797
* a example for use color text output on command
7898
* @usage {fullCommand}
File renamed without changes.

src/BuiltIn/Resources/auto-complete-scripts/phalcon.bash renamed to src/BuiltIn/Resources/auto-comp-scripts/phalcon.bash

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,4 @@ _phalcon()
3939

4040
} &&
4141
complete -F _phalcon phalcon
42+
# complete -o default -F _symfony console
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#compdef acme
2+
3+
_symfony()
4+
{
5+
local state com cur commands options
6+
7+
cur=${words[${#words[@]}]}
8+
9+
# lookup for command
10+
for word in ${words[@]:1}; do
11+
if [[ $word != -* ]]; then
12+
com=$word
13+
break
14+
fi
15+
done
16+
17+
[[ ${cur} == --* ]] && state="option"
18+
19+
[[ $cur == $com ]] && state="command"
20+
21+
case $state in
22+
command)
23+
commands=("${(@f)$(${words[1]} list --no-ansi --raw 2>/dev/null | awk '{ gsub(/:/, "\\:", $1); print }' | awk '{if (NF>1) print $1 ":" substr($0, index($0,$2)); else print $1}')}")
24+
_describe 'command' commands
25+
;;
26+
option)
27+
options=("${(@f)$(${words[1]} -h ${words[2]} --no-ansi 2>/dev/null | sed -n '/Options/,/^$/p' | sed -e '1d;$d' | sed 's/[^--]*\(--.*\)/\1/' | sed -En 's/[^ ]*(-(-[[:alnum:]]+){1,})[[:space:]]+(.*)/\1:\3/p' | awk '{$1=$1};1')}")
28+
_describe 'option' options
29+
;;
30+
*)
31+
# fallback to file completion
32+
_arguments '*:file:_files'
33+
esac
34+
}
35+
36+
compdef _symfony acme

src/BuiltIn/Resources/templates/auto-complete.bash.tpl renamed to src/BuiltIn/Resources/templates/auto-comp.bash.tpl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
#!/usr/bin/env bash
22

33
#
4+
# This is auto completion script for bash env.
45
# Auto generate by inhere/console.
5-
# console application name: {$name}
6+
# application name: {$name}
67
# @date {$datetime}
78
#
89

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#compdef <?php echo $vars['script'] ?>
2+
3+
#
4+
# This is auto completion script for zsh env.
5+
# Auto generate by inhere/console.
6+
# application name: {$name}
7+
# @date {$datetime}
8+
#
9+
10+
_<?php echo $vars['script'] ?>()
11+
{
12+
local state com cur
13+
14+
cur=${words[${#words[@]}]}
15+
16+
# lookup for command
17+
for word in ${words[@]:1}; do
18+
if [[ $word != -* ]]; then
19+
com=$word
20+
break
21+
fi
22+
done
23+
24+
if [[ ${cur} == --* ]]; then
25+
state="option"
26+
opts=(<?php echo join(' ', array_map(function($v) use ($helpers, $vars) { return $helpers['zsh_describe']($v, $vars['options_descriptions'][$v]); }, $vars['options_global'])) ?>)
27+
elif [[ $cur == $com ]]; then
28+
state="command"
29+
coms=(<?php echo join(' ', array_map(function($v) use ($helpers, $vars) { return $helpers['zsh_describe']($v, $vars['commands_descriptions'][$v]); }, $vars['commands'])) ?>)
30+
fi
31+
32+
case $state in
33+
command)
34+
_describe 'command' coms
35+
;;
36+
option)
37+
case "$com" in
38+
<?php foreach ($vars['options_command'] as $command => $options): ?>
39+
40+
<?php echo $command ?>)
41+
opts+=(<?php echo join(' ', array_map(function($v) use ($helpers, $vars, $command) { return $helpers['zsh_describe']($v, $vars['commands_options_descriptions'][$command][$v]); }, array_diff($options, $vars['options_global']))) ?>)
42+
;;
43+
<?php endforeach; ?>
44+
45+
esac
46+
47+
_describe 'option' opts
48+
;;
49+
*)
50+
# fallback to file completion
51+
_arguments '*:file:_files'
52+
esac
53+
}
54+
55+
<?php foreach ($vars['tools'] as $tool): ?>
56+
compdef _<?php echo $vars['script'] ?> <?php echo $tool ?>
57+
58+
<?php endforeach; ?>

src/Style/Color.php

Lines changed: 27 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,18 @@
1313
*/
1414
final class Color
1515
{
16-
/**
17-
* Foreground base value
18-
*/
16+
/** Foreground base value */
1917
const FG_BASE = 30;
2018

21-
/**
22-
* Background base value
23-
*/
19+
/** Background base value */
2420
const BG_BASE = 40;
2521

22+
/** Extra Foreground base value */
23+
const FG_EXTRA = 90;
24+
25+
/** Extra Background base value */
26+
const BG_EXTRA = 100;
27+
2628
// color
2729
const BLACK = 'black';
2830
const RED = 'red';
@@ -43,9 +45,7 @@ final class Color
4345
const REVERSE = 'reverse'; // 颠倒的 交换背景色与前景色
4446
const CONCEALED = 'concealed'; // 隐匿的
4547

46-
/**
47-
* Known color list
48-
*/
48+
/** @var array Known color list */
4949
private static $knownColors = array(
5050
'black' => 0,
5151
'red' => 1,
@@ -58,10 +58,7 @@ final class Color
5858
'normal' => 9,
5959
);
6060

61-
/**
62-
* Known style option
63-
* @var array
64-
*/
61+
/** @var array Known style option */
6562
private static $knownOptions = [
6663
'bold' => 1, // 22 加粗
6764
'fuzzy' => 2, // 模糊(不是所有的终端仿真器都支持)
@@ -72,44 +69,40 @@ final class Color
7269
'concealed' => 8, // 28 隐匿的
7370
];
7471

75-
/**
76-
* Foreground color
77-
*/
72+
/** @var int Foreground color */
7873
private $fgColor = 0;
7974

80-
/**
81-
* Background color
82-
*/
75+
/** @var int Background color */
8376
private $bgColor = 0;
8477

85-
/**
86-
* Array of style options
87-
*/
78+
/** @var array Array of style options */
8879
private $options = [];
8980

9081
/**
9182
* @param string $fg
9283
* @param string $bg
9384
* @param array $options
85+
* @param bool $extra
9486
* @return Color
9587
*/
96-
public static function make($fg = '', $bg = '', array $options = [])
88+
public static function make($fg = '', $bg = '', array $options = [], $extra = false)
9789
{
98-
return new self($fg, $bg, $options);
90+
return new self($fg, $bg, $options, $extra);
9991
}
10092

10193
/**
10294
* Create a color style from a parameter string.
10395
*
104-
* @param string $string e.g 'fg=white;bg=black;options=bold,underscore'
96+
* @param string $string e.g 'fg=white;bg=black;options=bold,underscore;extra=1'
10597
* @return static
10698
* @throws \RuntimeException
10799
*/
108100
public static function makeByString($string)
109101
{
110102
$fg = $bg = '';
103+
$extra = false;
111104
$options = [];
112-
$parts = explode(';', $string);
105+
$parts = explode(';', str_replace(' ', '', $string));
113106

114107
foreach ($parts as $part) {
115108
$subParts = explode('=', $part);
@@ -125,6 +118,9 @@ public static function makeByString($string)
125118
case 'bg':
126119
$bg = $subParts[1];
127120
break;
121+
case 'extra':
122+
$extra = $subParts[1];
123+
break;
128124
case 'options':
129125
$options = explode(',', $subParts[1]);
130126
break;
@@ -135,17 +131,17 @@ public static function makeByString($string)
135131
}
136132
}
137133

138-
return new self($fg, $bg, $options);
134+
return new self($fg, $bg, $options, $extra);
139135
}
140136

141137
/**
142138
* Constructor
143139
* @param string $fg Foreground color. e.g 'white'
144140
* @param string $bg Background color. e.g 'black'
145141
* @param array $options Style options. e.g ['bold', 'underscore']
146-
* @throws \InvalidArgumentException
142+
* @param bool $extra
147143
*/
148-
public function __construct($fg = '', $bg = '', array $options = [])
144+
public function __construct($fg = '', $bg = '', array $options = [], $extra = false)
149145
{
150146
if ($fg) {
151147
if (false === array_key_exists($fg, static::$knownColors)) {
@@ -157,7 +153,7 @@ public function __construct($fg = '', $bg = '', array $options = [])
157153
);
158154
}
159155

160-
$this->fgColor = self::FG_BASE + static::$knownColors[$fg];
156+
$this->fgColor = ($extra ? self::FG_EXTRA : self::FG_BASE) + static::$knownColors[$fg];
161157
}
162158

163159
if ($bg) {
@@ -170,7 +166,7 @@ public function __construct($fg = '', $bg = '', array $options = [])
170166
);
171167
}
172168

173-
$this->bgColor = self::BG_BASE + static::$knownColors[$bg];
169+
$this->bgColor = ($extra ? self::BG_EXTRA : self::BG_BASE) + static::$knownColors[$bg];
174170
}
175171

176172
foreach ($options as $option) {

0 commit comments

Comments
 (0)