Skip to content
This repository was archived by the owner on Jul 1, 2023. It is now read-only.

Commit ef73af3

Browse files
committed
Added support for highlighted source code with suggested package jakub-onderka/php-console-highlighter
1 parent cdc7596 commit ef73af3

File tree

6 files changed

+93
-21
lines changed

6 files changed

+93
-21
lines changed

composer.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
"require": {
77
"php": ">=5.3.2"
88
},
9+
"suggest": {
10+
"jakub-onderka/php-console-highlighter": "Highlight syntax in code snippet"
11+
},
912
"authors": [
1013
{
1114
"name": "Jakub Onderka",

parallel-lint.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ function showOptions()
2121
--exclude Exclude directory. If you want exclude multiple directory, use
2222
multiple exclude parameters.
2323
-j <num> Run <num> jobs in parallel (default 10)
24+
--no-colors Disable colors in console output.
2425
-h, --help Print this help.
2526
<?php
2627
}
@@ -38,7 +39,7 @@ function showOptions()
3839
exit;
3940
}
4041

41-
require_once __DIR__ . '/src/Manager.php';
42+
require_once __DIR__ . '/vendor/autoload.php';
4243

4344
try {
4445
$manager = new PhpParallelLint\Manager;

src/Error.php

Lines changed: 45 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
either expressed or implied, of the FreeBSD Project.
3131
*/
3232

33+
use JakubOnderka\PhpConsoleHighlighter\Highlighter;
34+
3335
class Error
3436
{
3537
/** @var string */
@@ -92,22 +94,26 @@ public function __toString()
9294
}
9395

9496
/**
95-
* @param int $line
97+
* @param int $lineNumber
98+
* @param int $linesBefore
99+
* @param int $linesAfter
96100
* @return string
97101
*/
98-
protected function getCodeSnippet($line)
102+
protected function getCodeSnippet($lineNumber, $linesBefore = 2, $linesAfter = 2)
99103
{
100104
$lines = file($this->filePath);
101105

102-
$snippet = '';
103-
$lineStrlen = strlen($line + 2);
104-
$line -= 1; // because $lines array is indexed from zero
106+
$offset = $lineNumber - $linesBefore - 1;
107+
$length = $linesAfter + $linesBefore + 1;
108+
$lines = array_slice($lines, $offset, $length, $preserveKeys = true);
105109

106-
for ($i = $line - 2; $i <= $line + 2; $i++) {
107-
if (isset($lines[$i])) {
108-
$snippet .= ($line === $i ? ' > ' : ' ');
109-
$snippet .= $this->stringWidth($i + 1, $lineStrlen) . '| ' . rtrim($lines[$i]) . PHP_EOL;
110-
}
110+
end($lines);
111+
$lineStrlen = strlen(key($lines) + 1);
112+
113+
$snippet = '';
114+
foreach ($lines as $i => $line) {
115+
$snippet .= ($lineNumber === $i + 1 ? ' > ' : ' ');
116+
$snippet .= str_pad($i + 1, $lineStrlen, ' ', STR_PAD_LEFT) . '| ' . rtrim($line) . PHP_EOL;
111117
}
112118

113119
return $snippet;
@@ -188,4 +194,33 @@ protected function getShortFilePath()
188194
{
189195
return str_replace(getcwd(), '', $this->filePath);
190196
}
197+
}
198+
199+
class SyntaxErrorColored extends SyntaxError
200+
{
201+
/**
202+
* @param int $lineNumber
203+
* @param int $linesBefore
204+
* @param int $linesAfter
205+
* @return string
206+
*/
207+
protected function getCodeSnippet($lineNumber, $linesBefore = 2, $linesAfter = 2)
208+
{
209+
if (!class_exists('\JakubOnderka\PhpConsoleHighlighter\Highlighter')) {
210+
return parent::getCodeSnippet($lineNumber, $linesBefore, $linesAfter);
211+
}
212+
213+
$colors = new \Colors\Color();
214+
$colors->setTheme(array(
215+
Highlighter::TOKEN_STRING => 'red',
216+
Highlighter::TOKEN_COMMENT => 'yellow',
217+
Highlighter::TOKEN_KEYWORD => 'green',
218+
Highlighter::TOKEN_DEFAULT => 'white',
219+
Highlighter::TOKEN_HTML => 'cyan'
220+
));
221+
$highliger = new Highlighter($colors);
222+
223+
$fileContent = file_get_contents($this->filePath);
224+
return $highliger->getCodeSnippet($fileContent, $lineNumber, $linesBefore, $linesAfter);
225+
}
191226
}

src/Manager.php

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,6 @@
3030
either expressed or implied, of the FreeBSD Project.
3131
*/
3232

33-
34-
require_once __DIR__ . '/exceptions.php';
35-
require_once __DIR__ . '/Settings.php';
36-
require_once __DIR__ . '/Process.php';
37-
require_once __DIR__ . '/Output.php';
38-
require_once __DIR__ . '/Error.php';
39-
4033
class Manager
4134
{
4235
const CODE_OK = 0,
@@ -87,6 +80,10 @@ public function parseArguments(array $arguments)
8780
$setting->parallelJobs = max((int) $arguments->getNext(), 1);
8881
break;
8982

83+
case '--no-colors':
84+
$setting->colors = false;
85+
break;
86+
9087
default:
9188
throw new InvalidArgumentException($argument);
9289
}
@@ -108,7 +105,7 @@ public function parseArguments(array $arguments)
108105
public function run(Settings $settings = null)
109106
{
110107
$settings = $settings ?: new Settings;
111-
$output = $this->output ?: new Output(new ConsoleWriter);
108+
$output = $this->output ?: ($settings->colors ? new OutputColored(new ConsoleWriter) : new Output(new ConsoleWriter));
112109

113110
$this->checkPhpExecutableExists($settings->phpExecutable);
114111

@@ -145,7 +142,12 @@ public function run(Settings $settings = null)
145142
} else {
146143
$checkedFiles++;
147144
if ($process->hasSyntaxError()) {
148-
$errors[] = new SyntaxError($file, $process->getSyntaxError());
145+
if ($settings->colors) {
146+
$errors[] = new SyntaxErrorColored($file, $process->getSyntaxError());
147+
} else {
148+
$errors[] = new SyntaxError($file, $process->getSyntaxError());
149+
}
150+
149151
$filesWithSyntaxError++;
150152
$output->error();
151153
} else {
@@ -174,7 +176,7 @@ public function run(Settings $settings = null)
174176
$output->writeNewLine();
175177

176178
foreach ($errors as $errorMessage) {
177-
$output->writeLine('----------------------------------------------------------------------');
179+
$output->writeLine(str_repeat('-', 60));
178180
$output->writeLine($errorMessage);
179181
}
180182

src/Output.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,31 @@ protected function stringWidth($input, $width = 3)
115115
}
116116
}
117117

118+
class OutputColored extends Output
119+
{
120+
/** @var \Colors\Color */
121+
private $colors;
122+
123+
public function __construct(IWriter $writer)
124+
{
125+
parent::__construct($writer);
126+
127+
if (class_exists('\Colors\Color')) {
128+
$this->colors = new \Colors\Color();
129+
}
130+
}
131+
132+
public function error()
133+
{
134+
if ($this->colors) {
135+
$this->writer->write($this->colors->apply('bg_red', 'X'));
136+
$this->progress();
137+
} else {
138+
parent::error();
139+
}
140+
}
141+
}
142+
118143
interface IWriter
119144
{
120145
/**

src/Settings.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,10 @@ class Settings
7373
* @var array
7474
*/
7575
public $excluded = array();
76+
77+
/**
78+
* Print to console with colors
79+
* @var bool
80+
*/
81+
public $colors = true;
7682
}

0 commit comments

Comments
 (0)