Skip to content

Commit b4db25a

Browse files
committed
update, output context code view on throw exception
1 parent 7ca931a commit b4db25a

File tree

5 files changed

+94
-20
lines changed

5 files changed

+94
-20
lines changed

examples/Controllers/HomeController.php

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Inhere\Console\Controller;
99
use Inhere\Console\IO\Input;
1010
use Inhere\Console\Style\Highlighter;
11+
use Inhere\Console\Style\LiteStyle;
1112
use Inhere\Console\Utils\Helper;
1213
use Inhere\Console\Utils\Interact;
1314
use Inhere\Console\Utils\ProgressBar;
@@ -116,7 +117,7 @@ public function highlightCommand($in)
116117
}
117118

118119
/**
119-
* a example for use color text output on command
120+
* a example for use color text output by Style::class
120121
* @usage {fullCommand}
121122
*/
122123
public function colorCommand()
@@ -137,12 +138,40 @@ public function colorCommand()
137138
return 0;
138139
}
139140

141+
/**
142+
* a example for use color text output by LiteStyle::class
143+
*/
144+
public function colorLiteCommand()
145+
{
146+
if (!$this->output->supportColor()) {
147+
$this->write('Current terminal is not support output color text.');
148+
149+
return -2;
150+
}
151+
152+
$this->output->startBuffer();
153+
154+
foreach (array_keys(LiteStyle::STYLES) as $style) {
155+
$this->output->write(LiteStyle::color("color text(style:$style)", $style));
156+
}
157+
158+
$this->output->flush();
159+
160+
return 0;
161+
}
162+
140163
/**
141164
* output block message text
142165
* @return int
143166
*/
144167
public function blockMsgCommand()
145168
{
169+
if (!$this->output->supportColor()) {
170+
$this->write('Current terminal is not support output color text.');
171+
172+
return 0;
173+
}
174+
146175
$this->write('block message:');
147176

148177
foreach (Show::getBlockMethods() as $type) {

src/Base/AbstractApplication.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
use Inhere\Console\IO\Input;
1212
use Inhere\Console\IO\Output;
13+
use Inhere\Console\Style\Highlighter;
1314
use Inhere\Console\Traits\InputOutputAwareTrait;
1415
use Inhere\Console\Traits\SimpleEventTrait;
1516
use Inhere\Console\Style\Style;
@@ -243,16 +244,22 @@ public function handleException($e)
243244
$tpl,
244245
// $e->getCode(),
245246
$e->getMessage(),
246-
$e->getFile(),
247-
$e->getLine(),
247+
$file = $e->getFile(),
248+
$line = $e->getLine(),
248249
__METHOD__,
249250
$e->getTraceAsString()
250251
);
251252

253+
$source = file_get_contents($file);
254+
$hl = Highlighter::create();
255+
$snippet = $hl->highlightSnippet($source, $line, 3, 3);
256+
$message .= "\nCode View:\n$snippet";
257+
252258
if ($this->meta['hideRootPath'] && ($rootPath = $this->meta['rootPath'])) {
253259
$message = str_replace($rootPath, '{ROOT}', $message);
254260
}
255261

262+
256263
$this->output->write($message, false);
257264
} else {
258265
// simple output

src/Base/AbstractCommand.php

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,7 @@ public function validateInput()
269269

270270
if (\count($missingArgs) > 0) {
271271
$this->output->liteError(sprintf('Not enough arguments (missing: "%s").', implode(', ', $missingArgs)));
272+
272273
return false;
273274
}
274275

@@ -284,9 +285,20 @@ public function validateInput()
284285

285286
// check options
286287
$opts = $missingOpts = [];
287-
//$givenLOpts = $in->getLongOpts();
288+
$givenOpts = $in->getOptions();
288289
$defOpts = $def->getOptions();
289290

291+
// check unknown options
292+
if ($unknown = array_diff_key($givenOpts, $defOpts)) {
293+
$names = array_keys($unknown);
294+
$first = array_shift($names);
295+
296+
throw new \InvalidArgumentException(sprintf(
297+
'Input option is not exists (unknown: "%s").',
298+
(isset($first[1]) ? '--' : '-') . $first
299+
));
300+
}
301+
290302
foreach ($defOpts as $name => $conf) {
291303
if (!$in->hasLOpt($name)) {
292304
if (($srt = $conf['shortcut']) && $in->hasSOpt($srt)) {
@@ -298,8 +310,9 @@ public function validateInput()
298310
}
299311

300312
if (\count($missingOpts) > 0) {
301-
$this->output->liteError(sprintf('Not enough options parameters (missing: "%s").',
302-
implode(', ', $missingOpts)));
313+
$this->output->liteError(
314+
sprintf('Not enough options parameters (missing: "%s").', implode(', ', $missingOpts))
315+
);
303316

304317
return false;
305318
}
@@ -319,7 +332,7 @@ public function validateInput()
319332
* @param string $name
320333
* @param string $value
321334
*/
322-
protected function addAnnotationVar(string $name, $value)
335+
protected function addAnnotationVar(string $name, $value)
323336
{
324337
if (!isset($this->annotationVars[$name])) {
325338
$this->annotationVars[$name] = (string)$value;

src/IO/Input.php

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,9 @@ public function read($question = null, $nl = false): string
129129
return trim(fgets($this->inputStream));
130130
}
131131

132-
/***************************************************************************
132+
/***********************************************************************************
133133
* arguments (eg: arg0 name=john city=chengdu)
134-
***************************************************************************/
134+
***********************************************************************************/
135135

136136
/**
137137
* @return array
@@ -288,9 +288,9 @@ public function clearArgs()
288288
$this->args = [];
289289
}
290290

291-
/***************************************************************************
291+
/***********************************************************************************
292292
* long/short options (eg: -d --help)
293-
***************************************************************************/
293+
***********************************************************************************/
294294

295295
/**
296296
* get (long/short)opt value
@@ -564,6 +564,14 @@ public function getOpts(): array
564564
return array_merge($this->sOpts, $this->lOpts);
565565
}
566566

567+
/**
568+
* @return array
569+
*/
570+
public function getOptions(): array
571+
{
572+
return array_merge($this->sOpts, $this->lOpts);
573+
}
574+
567575
/**
568576
* clear l-opts
569577
*/
@@ -572,9 +580,9 @@ public function clearLOpts()
572580
$this->lOpts = [];
573581
}
574582

575-
/////////////////////////////////////////////////////////////////////////////////////////
576-
/// getter/setter
577-
/////////////////////////////////////////////////////////////////////////////////////////
583+
/***********************************************************************************
584+
* getter/setter
585+
***********************************************************************************/
578586

579587
/**
580588
* @return string

src/Style/Highlighter.php

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ class Highlighter
2929
/** @var Style */
3030
private $color;
3131

32+
/**
33+
* @var self
34+
*/
35+
private static $instance;
36+
3237
/** @var array */
3338
private $defaultTheme = [
3439
self::TOKEN_STRING => 'red',
@@ -40,6 +45,18 @@ class Highlighter
4045
self::LINE_NUMBER => 'darkGray',
4146
];
4247

48+
/**
49+
* @return Highlighter
50+
*/
51+
public static function create()
52+
{
53+
if (!self::$instance) {
54+
self::$instance = new self();
55+
}
56+
57+
return self::$instance;
58+
}
59+
4360
/**
4461
* @param Style $color
4562
*/
@@ -50,15 +67,15 @@ public function __construct(Style $color = null)
5067

5168
/**
5269
* @param string $source
53-
* @param bool $withLn with line number
70+
* @param bool $withLineNumber with line number
5471
* @return string
5572
*/
56-
public function highlight(string $source, $withLn = false)
73+
public function highlight(string $source, $withLineNumber = false)
5774
{
5875
$tokenLines = $this->getHighlightedLines($source);
5976
$lines = $this->colorLines($tokenLines);
6077

61-
if ($withLn) {
78+
if ($withLineNumber) {
6279
return $this->lineNumbers($lines);
6380
}
6481

@@ -237,7 +254,6 @@ private function colorLines(array $tokenLines)
237254

238255
foreach ($tokenLines as $lineCount => $tokenLine) {
239256
$line = '';
240-
// foreach ($tokenLine as $token) {
241257
foreach ($tokenLine as list($tokenType, $tokenValue)) {
242258
$style = $this->defaultTheme[$tokenType];
243259

@@ -262,8 +278,9 @@ private function colorLines(array $tokenLines)
262278
private function lineNumbers(array $lines, $markLine = null)
263279
{
264280
end($lines);
265-
$lineStrlen = \strlen(key($lines) + 1);
281+
266282
$snippet = '';
283+
$lineLen = \strlen(key($lines) + 1);
267284
$lmStyle = $this->defaultTheme[self::ACTUAL_LINE_MARK];
268285
$lnStyle = $this->defaultTheme[self::LINE_NUMBER];
269286

@@ -272,7 +289,7 @@ private function lineNumbers(array $lines, $markLine = null)
272289
$snippet .= ($markLine === $i + 1 ? $this->color->apply($lmStyle, ' > ') : ' ');
273290
}
274291

275-
$snippet .= $this->color->apply($lnStyle, str_pad($i + 1, $lineStrlen, ' ', STR_PAD_LEFT) . '| ');
292+
$snippet .= $this->color->apply($lnStyle, str_pad($i + 1, $lineLen, ' ', STR_PAD_LEFT) . '| ');
276293
$snippet .= $line . PHP_EOL;
277294
}
278295

0 commit comments

Comments
 (0)