Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Colorized file name and line in console output #4160

Merged
merged 2 commits into from Nov 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/Psalm/Report/ConsoleReport.php
Expand Up @@ -6,8 +6,10 @@
use Psalm\Internal\Analyzer\IssueData;
use Psalm\Report;

use function basename;
use function get_cfg_var;
use function ini_get;
use function strlen;
use function strtr;
use function substr;

Expand Down Expand Up @@ -118,6 +120,15 @@ private function getFileReference($data): string
return $reference;
}

$file_basename = basename($data->file_name);
$file_path = substr($data->file_name, 0, -strlen($file_basename));

$reference = $file_path
. "\033[1;31m"
. $file_basename . ':' . $data->line_from . ':' . $data->column_from
. "\033[0m"
;

if (null === $this->link_format) {
// if xdebug is not enabled, use `get_cfg_var` to get the value directly from php.ini
$this->link_format = ini_get('xdebug.file_link_format') ?: get_cfg_var('xdebug.file_link_format')
Expand All @@ -127,6 +138,7 @@ private function getFileReference($data): string
$link = strtr($this->link_format, ['%f' => $data->file_path, '%l' => $data->line_from]);
// $reference = $data->file_name . ':' . $data->line_from . ':' . $data->column_from;


return "\033]8;;" . $link . "\033\\" . $reference . "\033]8;;\033\\";
}
}
2 changes: 1 addition & 1 deletion tests/ReportOutputTest.php
Expand Up @@ -1079,7 +1079,7 @@ public function testConsoleReportWithLinks(): void
$output = IssueBuffer::getOutput(IssueBuffer::getIssuesData(), $console_report_options);

$this->assertStringContainsString(
"\033]8;;file://somefile.php#L3\033\\somefile.php:3:10\033]8;;\033\\",
"\033]8;;file://somefile.php#L3\033\\\033[1;31msomefile.php:3:10\033[0m\033]8;;\033\\",
$output
);
}
Expand Down