Skip to content

Commit 899af44

Browse files
committed
Fixed code snippet for small files
1 parent 6efabd3 commit 899af44

File tree

1 file changed

+5
-10
lines changed

1 file changed

+5
-10
lines changed

src/Error.php

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

33+
use Colors\Color;
3334
use JakubOnderka\PhpConsoleHighlighter\Highlighter;
3435

3536
class Error
@@ -104,6 +105,7 @@ protected function getCodeSnippet($lineNumber, $linesBefore = 2, $linesAfter = 2
104105
$lines = file($this->filePath);
105106

106107
$offset = $lineNumber - $linesBefore - 1;
108+
$offset = max($offset, 0);
107109
$length = $linesAfter + $linesBefore + 1;
108110
$lines = array_slice($lines, $offset, $length, $preserveKeys = true);
109111

@@ -210,17 +212,10 @@ protected function getCodeSnippet($lineNumber, $linesBefore = 2, $linesAfter = 2
210212
return parent::getCodeSnippet($lineNumber, $linesBefore, $linesAfter);
211213
}
212214

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);
215+
$colors = new Color();
216+
$highlighter = new Highlighter($colors);
222217

223218
$fileContent = file_get_contents($this->filePath);
224-
return $highliger->getCodeSnippet($fileContent, $lineNumber, $linesBefore, $linesAfter);
219+
return $highlighter->getCodeSnippet($fileContent, $lineNumber, $linesBefore, $linesAfter);
225220
}
226221
}

0 commit comments

Comments
 (0)