Skip to content

Commit 1618405

Browse files
committed
Merge pull request #82 from glady/issue_78
Issue #78: added option to force colors
2 parents 54dbe83 + fce9ec0 commit 1618405

File tree

6 files changed

+46
-14
lines changed

6 files changed

+46
-14
lines changed

parallel-lint.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ function showOptions()
2828
--exclude Exclude a file or directory. If you want exclude multiple items,
2929
use multiple exclude parameters.
3030
-j <num> Run <num> jobs in parallel (default: 10).
31+
--colors Enable colors in console output. (disables auto detection of color support)
3132
--no-colors Disable colors in console output.
3233
--json Output results as JSON string (require PHP 5.4).
3334
--blame Try to show git blame for row with error.

src/ErrorFormatter.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,19 @@
3535

3636
class ErrorFormatter
3737
{
38-
/** @var bool */
38+
/** @var string */
3939
private $useColors;
4040

41+
/** @var bool */
42+
private $forceColors;
43+
4144
/** @var bool */
4245
private $translateTokens;
4346

44-
public function __construct($useColors = false, $translateTokens = false)
47+
public function __construct($useColors = Settings::AUTODETECT, $translateTokens = false, $forceColors = false)
4548
{
4649
$this->useColors = $useColors;
50+
$this->forceColors = $forceColors;
4751
$this->translateTokens = $translateTokens;
4852
}
4953

@@ -78,7 +82,7 @@ public function formatSyntaxErrorMessage(SyntaxError $error, $withCodeSnipped =
7882
$string .= ":$onLine" . PHP_EOL;
7983

8084
if ($withCodeSnipped) {
81-
if ($this->useColors) {
85+
if ($this->useColors !== Settings::DISABLED) {
8286
$string .= $this->getColoredCodeSnippet($error->getFilePath(), $onLine);
8387
} else {
8488
$string .= $this->getCodeSnippet($error->getFilePath(), $onLine);
@@ -143,10 +147,10 @@ protected function getColoredCodeSnippet($filePath, $lineNumber, $linesBefore =
143147
}
144148

145149
$colors = new ConsoleColor();
146-
$colors->setForceStyle(true);
150+
$colors->setForceStyle($this->forceColors);
147151
$highlighter = new Highlighter($colors);
148152

149153
$fileContent = file_get_contents($filePath);
150154
return $highlighter->getCodeSnippet($fileContent, $lineNumber, $linesBefore, $linesAfter);
151155
}
152-
}
156+
}

src/Manager.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,10 @@ protected function getDefaultOutput(Settings $settings)
107107
if ($settings->json) {
108108
return new JsonOutput($writer);
109109
} else {
110-
return ($settings->colors ? new TextOutputColored($writer) : new TextOutput($writer));
110+
if ($settings->colors === Settings::DISABLED) {
111+
return new TextOutput($writer);
112+
}
113+
return new TextOutputColored($writer, $settings->colors);
111114
}
112115
}
113116

src/Output.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -320,13 +320,13 @@ class TextOutputColored extends TextOutput
320320
/** @var \JakubOnderka\PhpConsoleColor\ConsoleColor */
321321
private $colors;
322322

323-
public function __construct(IWriter $writer)
323+
public function __construct(IWriter $writer, $colors = Settings::AUTODETECT)
324324
{
325325
parent::__construct($writer);
326326

327327
if (class_exists('\JakubOnderka\PhpConsoleColor\ConsoleColor')) {
328328
$this->colors = new \JakubOnderka\PhpConsoleColor\ConsoleColor();
329-
$this->colors->setForceStyle(true);
329+
$this->colors->setForceStyle($colors === Settings::FORCED);
330330
}
331331
}
332332

src/Settings.php

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,14 @@
3232

3333
class Settings
3434
{
35+
36+
/**
37+
* constants for enum settings
38+
*/
39+
const FORCED = 'FORCED';
40+
const DISABLED = 'DISABLED';
41+
const AUTODETECT = 'AUTODETECT';
42+
3543
/**
3644
* Path to PHP executable
3745
* @var string
@@ -75,10 +83,10 @@ class Settings
7583
public $excluded = array();
7684

7785
/**
78-
* Print to console with colors
79-
* @var bool
86+
* Mode for color detection. Possible values: self::FORCED, self::DISABLED and self::AUTODETECT
87+
* @var string
8088
*/
81-
public $colors = true;
89+
public $colors = self::AUTODETECT;
8290

8391
/**
8492
* Output results as JSON string
@@ -158,8 +166,12 @@ public static function parseArguments(array $arguments)
158166
$settings->parallelJobs = max((int) $arguments->getNext(), 1);
159167
break;
160168

169+
case '--colors':
170+
$settings->colors = self::FORCED;
171+
break;
172+
161173
case '--no-colors':
162-
$settings->colors = false;
174+
$settings->colors = self::DISABLED;
163175
break;
164176

165177
case '--json':

tests/Settings.parseArguments.phpt

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class SettingsParseArgumentsTest extends Tester\TestCase
2525
$expectedSettings->extensions = array('php', 'phtml', 'php3', 'php4', 'php5');
2626
$expectedSettings->paths = array('.');
2727
$expectedSettings->excluded = array();
28-
$expectedSettings->colors = true;
28+
$expectedSettings->colors = Settings::AUTODETECT;
2929
$expectedSettings->json = false;
3030

3131
Assert::equal($expectedSettings->phpExecutable, $settings->phpExecutable);
@@ -53,7 +53,7 @@ class SettingsParseArgumentsTest extends Tester\TestCase
5353
$expectedSettings->extensions = array('php', 'phtml', 'php3', 'php4', 'php5');
5454
$expectedSettings->paths = array('.');
5555
$expectedSettings->excluded = array('vendor');
56-
$expectedSettings->colors = false;
56+
$expectedSettings->colors = Settings::DISABLED;
5757
$expectedSettings->json = false;
5858

5959
Assert::equal($expectedSettings->phpExecutable, $settings->phpExecutable);
@@ -66,6 +66,18 @@ class SettingsParseArgumentsTest extends Tester\TestCase
6666
Assert::equal($expectedSettings->colors, $settings->colors);
6767
Assert::equal($expectedSettings->json, $settings->json);
6868
}
69+
70+
public function testColorsForced()
71+
{
72+
$commandLine = "./parallel-lint --exclude vendor --colors .";
73+
$argv = explode(" ", $commandLine);
74+
$settings = Settings::parseArguments($argv);
75+
76+
$expectedSettings = new Settings();
77+
$expectedSettings->colors = Settings::FORCED;
78+
79+
Assert::equal($expectedSettings->colors, $settings->colors);
80+
}
6981
}
7082

7183
$testCase = new SettingsParseArgumentsTest;

0 commit comments

Comments
 (0)