1010
1111namespace PHPCSDevTools \Scripts \FeatureComplete ;
1212
13+ use PHPCSDevTools \Scripts \Utils \HelpTextFormatter ;
1314use PHPCSDevTools \Scripts \Utils \Writer ;
1415use RuntimeException ;
1516
3536final 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}
0 commit comments