Skip to content

Commit

Permalink
BlueScreen: added column highlighting
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Mar 29, 2022
1 parent 89af719 commit e519bbd
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 10 deletions.
26 changes: 18 additions & 8 deletions src/Tracy/BlueScreen/BlueScreen.php
Original file line number Diff line number Diff line change
Expand Up @@ -295,16 +295,22 @@ private function renderActions(\Throwable $ex): array
/**
* Returns syntax highlighted source code.
*/
public static function highlightFile(string $file, int $line, int $lines = 15, bool $php = true): ?string
public static function highlightFile(
string $file,
int $line,
int $lines = 15,
bool $php = true,
int $column = 0
): ?string
{
$source = @file_get_contents($file); // @ file may not exist
if ($source === false) {
return null;
}

$source = $php
? static::highlightPhp($source, $line, $lines)
: '<pre class=tracy-code><div>' . static::highlightLine(htmlspecialchars($source, ENT_IGNORE, 'UTF-8'), $line, $lines) . '</div></pre>';
? static::highlightPhp($source, $line, $lines, $column)
: '<pre class=tracy-code><div>' . static::highlightLine(htmlspecialchars($source, ENT_IGNORE, 'UTF-8'), $line, $lines, $column) . '</div></pre>';

if ($editor = Helpers::editorUri($file, $line)) {
$source = substr_replace($source, ' title="Ctrl-Click to open in editor" data-tracy-href="' . Helpers::escapeHtml($editor) . '"', 4, 0);
Expand All @@ -317,7 +323,7 @@ public static function highlightFile(string $file, int $line, int $lines = 15, b
/**
* Returns syntax highlighted source code.
*/
public static function highlightPhp(string $source, int $line, int $lines = 15): string
public static function highlightPhp(string $source, int $line, int $lines = 15, int $column = 0): string
{
if (function_exists('ini_set')) {
ini_set('highlight.comment', '#998; font-style: italic');
Expand All @@ -332,7 +338,7 @@ public static function highlightPhp(string $source, int $line, int $lines = 15):
$source = explode("\n", highlight_string($source, true));
$out = $source[0]; // <code><span color=highlight.html>
$source = str_replace('<br />', "\n", $source[1]);
$out .= static::highlightLine($source, $line, $lines);
$out .= static::highlightLine($source, $line, $lines, $column);
$out = str_replace('&nbsp;', ' ', $out);
return "<pre class='tracy-code'><div>$out</div></pre>";
}
Expand All @@ -341,7 +347,7 @@ public static function highlightPhp(string $source, int $line, int $lines = 15):
/**
* Returns highlighted line in HTML code.
*/
public static function highlightLine(string $html, int $line, int $lines = 15): string
public static function highlightLine(string $html, int $line, int $lines = 15, int $column = 0): string
{
$source = explode("\n", "\n" . str_replace("\r\n", "\n", $html));
$out = '';
Expand All @@ -367,10 +373,14 @@ public static function highlightLine(string $html, int $line, int $lines = 15):
$s = str_replace(["\r", "\n"], ['', ''], $s);
preg_match_all('#<[^>]+>#', $s, $tags);
if ($n == $line) {
$s = strip_tags($s);
if ($column) {
$s = preg_replace('#((?:&.*?;|.){' . ($column - 1) . '})(&.*?;|.)#u', '\1<span class="tracy-column-highlight">\2</span>', $s, 1);
}
$out .= sprintf(
"<span class='tracy-line-highlight'>%{$numWidth}s: %s\n</span>%s",
$n,
strip_tags($s),
$s,
implode('', $tags[0])
);
} else {
Expand All @@ -386,7 +396,7 @@ public static function highlightLine(string $html, int $line, int $lines = 15):
/**
* Returns syntax highlighted source code to Terminal.
*/
public static function highlightPhpCli(string $file, int $line, int $lines = 15): ?string
public static function highlightPhpCli(string $file, int $line, int $lines = 15, int $column = 0): ?string
{
$source = @file_get_contents($file); // @ file may not exist
if ($source === false) {
Expand Down
11 changes: 9 additions & 2 deletions src/Tracy/BlueScreen/assets/bluescreen.css
Original file line number Diff line number Diff line change
Expand Up @@ -261,8 +261,15 @@
font-weight: bold;
font-style: normal;
display: block;
padding: 0 .4em;
margin: 0 -.4em;
padding: 0 1ch;
margin: 0 -1ch;
}

#tracy-bs .tracy-column-highlight {
display: inline-block;
backdrop-filter: grayscale(1);
margin: 0 -1px;
padding: 0 1px;
}

#tracy-bs .tracy-line {
Expand Down

0 comments on commit e519bbd

Please sign in to comment.