Skip to content

Support NO_COLOR env var #181

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

Merged
merged 2 commits into from
Aug 21, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,12 @@ Another approach for DIC-only usages is to scan the generated php file, but that
### Scanning codebase located elsewhere:
- This can be done by pointing `--composer-json` to `composer.json` of the other codebase

### Disable colored output:
- Set `NO_COLOR` environment variable to disable colored output:
```
NO_COLOR=1 vendor/bin/composer-dependency-analyser
```

## Limitations:
- Extension dependencies are not analysed (e.g. `ext-json`)

Expand Down
5 changes: 3 additions & 2 deletions bin/composer-dependency-analyser
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@ spl_autoload_register(static function (string $class) use ($psr4Prefix): void {

/** @var non-empty-string $cwd */
$cwd = getcwd();
$noColor = getenv('NO_COLOR') !== false;

$stdOutPrinter = new Printer(STDOUT);
$stdErrPrinter = new Printer(STDERR);
$stdOutPrinter = new Printer(STDOUT, $noColor);
$stdErrPrinter = new Printer(STDERR, $noColor);
$initializer = new Initializer($cwd, $stdOutPrinter, $stdErrPrinter);
$stopwatch = new Stopwatch();

Expand Down
14 changes: 12 additions & 2 deletions src/Printer.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,18 @@ class Printer
*/
private $resource;

/**
* @var bool
*/
private $noColor;

/**
* @param resource $resource
*/
public function __construct($resource)
public function __construct($resource, bool $noColor)
{
$this->resource = $resource;
$this->noColor = $noColor;
}

public function printLine(string $string): void
Expand All @@ -52,7 +58,11 @@ public function print(string $string): void

private function colorize(string $string): string
{
return str_replace(array_keys(self::COLORS), array_values(self::COLORS), $string);
return str_replace(
array_keys(self::COLORS),
$this->noColor ? '' : array_values(self::COLORS),
$string
);
}

}
8 changes: 4 additions & 4 deletions tests/ConsoleFormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ public function testPrintResult(): void

OUT;

self::assertSame($this->normalizeEol($expectedNoIssuesOutput), $this->removeColors($noIssuesOutput));
self::assertSame($this->normalizeEol($expectedNoIssuesButWarningsOutput), $this->removeColors($noIssuesButUnusedIgnores));
self::assertSame($this->normalizeEol($expectedNoIssuesOutput), $noIssuesOutput);
self::assertSame($this->normalizeEol($expectedNoIssuesButWarningsOutput), $noIssuesButUnusedIgnores);

$analysisResult = new AnalysisResult(
10,
Expand Down Expand Up @@ -194,8 +194,8 @@ public function testPrintResult(): void

OUT;

self::assertSame($this->normalizeEol($expectedRegularOutput), $this->removeColors($regularOutput));
self::assertSame($this->normalizeEol($expectedVerboseOutput), $this->removeColors($verboseOutput));
self::assertSame($this->normalizeEol($expectedRegularOutput), $regularOutput);
self::assertSame($this->normalizeEol($expectedVerboseOutput), $verboseOutput);
// editorconfig-checker-enable
}

Expand Down
8 changes: 1 addition & 7 deletions tests/FormatterTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use PHPUnit\Framework\TestCase;
use ShipMonk\ComposerDependencyAnalyser\Result\ResultFormatter;
use function fopen;
use function preg_replace;
use function str_replace;
use function stream_get_contents;

Expand All @@ -23,7 +22,7 @@ protected function getFormatterNormalizedOutput(Closure $closure): string
$stream = fopen('php://memory', 'w');
self::assertNotFalse($stream);

$printer = new Printer($stream);
$printer = new Printer($stream, true);
$formatter = $this->createFormatter($printer);

$closure($formatter);
Expand All @@ -35,9 +34,4 @@ protected function normalizeEol(string $string): string
return str_replace("\r\n", "\n", $string);
}

protected function removeColors(string $output): string
{
return (string) preg_replace('#\\x1b[[][^A-Za-z]*[A-Za-z]#', '', $output);
}

}
14 changes: 13 additions & 1 deletion tests/PrinterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,24 @@ public function testPrintLine(): void
$stream = fopen('php://memory', 'w');
self::assertNotFalse($stream);

$printer = new Printer($stream);
$printer = new Printer($stream, false);

$printer->printLine('Hello, <red>world</red>!');
$printer->print('New line!');

self::assertSame("Hello, \033[31mworld\033[0m!" . PHP_EOL . 'New line!', stream_get_contents($stream, -1, 0));
}

public function testPrintNoColor(): void
{
$stream = fopen('php://memory', 'w');
self::assertNotFalse($stream);

$printer = new Printer($stream, true);

$printer->print('Hello, <red>world</red>!');

self::assertSame('Hello, world!', stream_get_contents($stream, -1, 0));
}

}
Loading