Skip to content

Commit b59c267

Browse files
committed
Refactor FeatureComplete code for color support and formatting help text
This commit moves the code related to checking color and formatting the help text to a new helper class. This way it can be reused when the new DocCodeExamples script is introduced. Tests were added for the new HelpTextFormatter::format() method.
1 parent f0c7a64 commit b59c267

File tree

5 files changed

+257
-104
lines changed

5 files changed

+257
-104
lines changed

Scripts/FeatureComplete/Config.php

Lines changed: 4 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
namespace PHPCSDevTools\Scripts\FeatureComplete;
1212

13+
use PHPCSDevTools\Scripts\Utils\HelpTextFormatter;
1314
use PHPCSDevTools\Scripts\Utils\Writer;
1415
use RuntimeException;
1516

@@ -35,20 +36,6 @@
3536
final class Config
3637
{
3738

38-
/**
39-
* Max width for help text.
40-
*
41-
* @var int
42-
*/
43-
const MAX_WIDTH = 80;
44-
45-
/**
46-
* Margin for help options.
47-
*
48-
* @var string
49-
*/
50-
const LEFT_MARGIN = ' ';
51-
5239
/**
5340
* Writer for sending output.
5441
*
@@ -284,7 +271,7 @@ protected function processCliCommand()
284271

285272
if (empty($args)) {
286273
// No options set.
287-
$this->showColored = $this->isColorSupported();
274+
$this->showColored = HelpTextFormatter::isColorSupported();
288275

289276
return;
290277
}
@@ -296,7 +283,7 @@ protected function processCliCommand()
296283
} elseif (isset($argsFlipped['--colors'])) {
297284
$this->showColored = true;
298285
} else {
299-
$this->showColored = $this->isColorSupported();
286+
$this->showColored = HelpTextFormatter::isColorSupported();
300287
}
301288

302289
if (isset($argsFlipped['-h'])
@@ -373,41 +360,6 @@ static function ($subdir) {
373360
}
374361
}
375362

376-
/**
377-
* Detect whether or not the CLI supports colored output.
378-
*
379-
* @codeCoverageIgnore
380-
*
381-
* @return bool
382-
*/
383-
protected function isColorSupported()
384-
{
385-
// Windows.
386-
if (\DIRECTORY_SEPARATOR === '\\') {
387-
if (\getenv('ANSICON') !== false || \getenv('ConEmuANSI') === 'ON') {
388-
return true;
389-
}
390-
391-
if (\function_exists('sapi_windows_vt100_support')) {
392-
// phpcs:ignore PHPCompatibility.FunctionUse.NewFunctions.sapi_windows_vt100_supportFound
393-
return @\sapi_windows_vt100_support(\STDOUT);
394-
}
395-
396-
return false;
397-
}
398-
399-
if (\getenv('GITHUB_ACTIONS')) {
400-
return true;
401-
}
402-
403-
// Linux/MacOS.
404-
if (\function_exists('posix_isatty')) {
405-
return @\posix_isatty(\STDOUT);
406-
}
407-
408-
return false;
409-
}
410-
411363
/**
412364
* Retrieve the version number of this script.
413365
*
@@ -429,58 +381,6 @@ public function getVersion()
429381
*/
430382
private function getHelp()
431383
{
432-
$output = '';
433-
foreach ($this->helpTexts as $section => $options) {
434-
$longestOptionLength = 0;
435-
foreach ($options as $option) {
436-
if (isset($option['arg'])) {
437-
$longestOptionLength = \max($longestOptionLength, \strlen($option['arg']));
438-
}
439-
}
440-
441-
if ($this->showColored === true) {
442-
$output .= "\033[33m{$section}:\033[0m" . \PHP_EOL;
443-
} else {
444-
$output .= "{$section}:" . \PHP_EOL;
445-
}
446-
447-
$descWidth = (self::MAX_WIDTH - ($longestOptionLength + 1 + \strlen(self::LEFT_MARGIN)));
448-
$descBreak = \PHP_EOL . self::LEFT_MARGIN . \str_pad(' ', ($longestOptionLength + 1));
449-
450-
foreach ($options as $option) {
451-
if (isset($option['text'])) {
452-
$text = $option['text'];
453-
if ($this->showColored === true) {
454-
$text = \preg_replace('`(\[[^\]]+\])`', "\033[36m" . '$1' . "\033[0m", $text);
455-
}
456-
$output .= self::LEFT_MARGIN . $text . \PHP_EOL;
457-
}
458-
459-
if (isset($option['arg'])) {
460-
$arg = \str_pad($option['arg'], $longestOptionLength);
461-
if ($this->showColored === true) {
462-
$arg = \preg_replace('`(<[^>]+>)`', "\033[0m\033[36m" . '$1', $arg);
463-
$arg = "\033[32m{$arg}\033[0m";
464-
}
465-
466-
$descText = \wordwrap($option['desc'], $descWidth, $descBreak);
467-
$desc = \explode('. ', $option['desc']);
468-
if (\count($desc) > 1) {
469-
$descText = '';
470-
foreach ($desc as $key => $sentence) {
471-
$descText .= ($key === 0) ? '' : $descBreak;
472-
$descText .= \wordwrap($sentence, $descWidth, $descBreak);
473-
$descText = \rtrim($descText, '.') . '.';
474-
}
475-
}
476-
477-
$output .= self::LEFT_MARGIN . $arg . ' ' . $descText . \PHP_EOL;
478-
}
479-
}
480-
481-
$output .= \PHP_EOL;
482-
}
483-
484-
return $output;
384+
return HelpTextFormatter::format($this->helpTexts, $this->showColored);
485385
}
486386
}

Scripts/Utils/HelpTextFormatter.php

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
<?php
2+
/**
3+
* PHPCSDevTools, tools for PHP_CodeSniffer sniff developers.
4+
*
5+
* @package PHPCSDevTools
6+
* @copyright 2019 PHPCSDevTools Contributors
7+
* @license https://opensource.org/licenses/LGPL-3.0 LGPL3
8+
* @link https://github.com/PHPCSStandards/PHPCSDevTools
9+
*/
10+
11+
namespace PHPCSDevTools\Scripts\Utils;
12+
13+
/**
14+
* Helper class for formatting help text and detecting color support.
15+
*
16+
* ---------------------------------------------------------------------------------------------
17+
* This class is not part of the public API. Backward compatibility is not guaranteed.
18+
* ---------------------------------------------------------------------------------------------
19+
*
20+
* @since 2.0.0
21+
*/
22+
class HelpTextFormatter
23+
{
24+
/**
25+
* Max width for help text.
26+
*
27+
* @var int
28+
*/
29+
const MAX_WIDTH = 80;
30+
31+
/**
32+
* Margin for help options.
33+
*
34+
* @var string
35+
*/
36+
const LEFT_MARGIN = ' ';
37+
38+
/**
39+
* Format help text from a structured array of options.
40+
*
41+
* @param array<string, array<array<string, string>>> $helpTexts The help texts to format.
42+
* @param bool $showColored Whether to use colored output.
43+
*
44+
* @return string The formatted help text.
45+
*/
46+
public static function format(array $helpTexts, $showColored)
47+
{
48+
$output = '';
49+
foreach ($helpTexts as $section => $options) {
50+
$longestOptionLength = 0;
51+
foreach ($options as $option) {
52+
if (isset($option['arg'])) {
53+
$longestOptionLength = \max($longestOptionLength, \strlen($option['arg']));
54+
}
55+
}
56+
57+
if ($showColored === true) {
58+
$output .= "\033[33m{$section}:\033[0m" . \PHP_EOL;
59+
} else {
60+
$output .= "{$section}:" . \PHP_EOL;
61+
}
62+
63+
$descWidth = (self::MAX_WIDTH - ($longestOptionLength + 1 + \strlen(self::LEFT_MARGIN)));
64+
$descBreak = \PHP_EOL . self::LEFT_MARGIN . \str_pad(' ', ($longestOptionLength + 1));
65+
66+
foreach ($options as $option) {
67+
if (isset($option['text'])) {
68+
$text = $option['text'];
69+
if ($showColored === true) {
70+
$text = \preg_replace('`(\[[^\]]+\])`', "\033[36m" . '$1' . "\033[0m", $text);
71+
}
72+
$output .= self::LEFT_MARGIN . $text . \PHP_EOL;
73+
}
74+
75+
if (isset($option['arg'])) {
76+
$arg = \str_pad($option['arg'], $longestOptionLength);
77+
if ($showColored === true) {
78+
$arg = \preg_replace('`(<[^>]+>)`', "\033[0m\033[36m" . '$1', $arg);
79+
$arg = "\033[32m{$arg}\033[0m";
80+
}
81+
82+
$descText = \wordwrap($option['desc'], $descWidth, $descBreak);
83+
$desc = \explode('. ', $option['desc']);
84+
if (\count($desc) > 1) {
85+
$descText = '';
86+
foreach ($desc as $key => $sentence) {
87+
$descText .= ($key === 0) ? '' : $descBreak;
88+
$descText .= \wordwrap($sentence, $descWidth, $descBreak);
89+
$descText = \rtrim($descText, '.') . '.';
90+
}
91+
}
92+
93+
$output .= self::LEFT_MARGIN . $arg . ' ' . $descText . \PHP_EOL;
94+
}
95+
}
96+
97+
$output .= \PHP_EOL;
98+
}
99+
100+
return $output;
101+
}
102+
103+
/**
104+
* Detect whether or not the CLI supports colored output.
105+
*
106+
* @codeCoverageIgnore
107+
*
108+
* @return bool
109+
*/
110+
public static function isColorSupported()
111+
{
112+
// Windows.
113+
if (\DIRECTORY_SEPARATOR === '\\') {
114+
if (\getenv('ANSICON') !== false || \getenv('ConEmuANSI') === 'ON') {
115+
return true;
116+
}
117+
118+
if (\function_exists('sapi_windows_vt100_support')) {
119+
// phpcs:ignore PHPCompatibility.FunctionUse.NewFunctions.sapi_windows_vt100_supportFound
120+
return @\sapi_windows_vt100_support(\STDOUT);
121+
}
122+
123+
return false;
124+
}
125+
126+
if (\getenv('GITHUB_ACTIONS')) {
127+
return true;
128+
}
129+
130+
// Linux/MacOS.
131+
if (\function_exists('posix_isatty')) {
132+
return @\posix_isatty(\STDOUT);
133+
}
134+
135+
return false;
136+
}
137+
}

0 commit comments

Comments
 (0)