| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace Jfcherng\Diff\Renderer\Text; |
| 6 |
|
| 7 |
use Jfcherng\Diff\Exception\UnsupportedFunctionException; |
| 8 |
use Jfcherng\Diff\Renderer\AbstractRenderer; |
| 9 |
use Jfcherng\Diff\Renderer\RendererConstant; |
| 10 |
use Jfcherng\Utility\CliColor; |
| 11 |
|
| 12 |
/** |
| 13 |
* Base renderer for rendering text-based diffs. |
| 14 |
*/ |
| 15 |
abstract class AbstractText extends AbstractRenderer |
| 16 |
{ |
| 17 |
/** |
| 18 |
* @var bool is this renderer pure text? |
| 19 |
*/ |
| 20 |
public const IS_TEXT_RENDERER = true; |
| 21 |
|
| 22 |
/** |
| 23 |
* @var string the diff output representing there is no EOL at EOF in the GNU diff tool |
| 24 |
*/ |
| 25 |
public const GNU_OUTPUT_NO_EOL_AT_EOF = '\ No newline at end of file'; |
| 26 |
|
| 27 |
/** |
| 28 |
* @var bool controls whether cliColoredString() is enabled or not |
| 29 |
*/ |
| 30 |
protected $isCliColorEnabled = false; |
| 31 |
|
| 32 |
public function setOptions(array $options): AbstractRenderer |
| 33 |
{ |
| 34 |
parent::setOptions($options); |
| 35 |
|
| 36 |
// determine $this->isCliColorEnabled |
| 37 |
if ($this->options['cliColorization'] === RendererConstant::CLI_COLOR_ENABLE) { |
| 38 |
$this->isCliColorEnabled = true; |
| 39 |
} elseif ($this->options['cliColorization'] === RendererConstant::CLI_COLOR_DISABLE) { |
| 40 |
$this->isCliColorEnabled = false; |
| 41 |
} else { |
| 42 |
$this->isCliColorEnabled = \PHP_SAPI === 'cli' && $this->hasColorSupport(\STDOUT); |
| 43 |
} |
| 44 |
|
| 45 |
return $this; |
| 46 |
} |
| 47 |
|
| 48 |
public function getResultForIdenticalsDefault(): string |
| 49 |
{ |
| 50 |
return ''; |
| 51 |
} |
| 52 |
|
| 53 |
protected function renderArrayWorker(array $differArray): string |
| 54 |
{ |
| 55 |
throw new UnsupportedFunctionException(__METHOD__); |
| 56 |
|
| 57 |
return ''; // make IDE not complain |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Colorize the string for CLI output. |
| 62 |
* |
| 63 |
* @param string $str the string |
| 64 |
* @param null|string $symbol the symbol |
| 65 |
* |
| 66 |
* @return string the (maybe) colorized string |
| 67 |
*/ |
| 68 |
protected function cliColoredString(string $str, ?string $symbol): string |
| 69 |
{ |
| 70 |
static $symbolToStyles = [ |
| 71 |
'@' => ['f_purple', 'bold'], // header |
| 72 |
'-' => ['f_red', 'bold'], // deleted |
| 73 |
'+' => ['f_green', 'bold'], // inserted |
| 74 |
'!' => ['f_yellow', 'bold'], // replaced |
| 75 |
]; |
| 76 |
|
| 77 |
$styles = $symbolToStyles[$symbol] ?? []; |
| 78 |
|
| 79 |
if (!$this->isCliColorEnabled || empty($styles)) { |
| 80 |
return $str; |
| 81 |
} |
| 82 |
|
| 83 |
return CliColor::color($str, $styles); |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* Returns true if the stream supports colorization. |
| 88 |
* |
| 89 |
* Colorization is disabled if not supported by the stream: |
| 90 |
* |
| 91 |
* This is tricky on Windows, because Cygwin, Msys2 etc emulate pseudo |
| 92 |
* terminals via named pipes, so we can only check the environment. |
| 93 |
* |
| 94 |
* Reference: Composer\XdebugHandler\Process::supportsColor |
| 95 |
* https://github.com/composer/xdebug-handler |
| 96 |
* |
| 97 |
* @see https://github.com/symfony/console/blob/647c51ff073300a432a4a504e29323cf0d5e0571/Output/StreamOutput.php#L81-L124 |
| 98 |
* |
| 99 |
* @param resource $stream |
| 100 |
* |
| 101 |
* @return bool true if the stream supports colorization, false otherwise |
| 102 |
* |
| 103 |
* @suppress PhanUndeclaredFunction |
| 104 |
*/ |
| 105 |
protected function hasColorSupport($stream): bool |
| 106 |
{ |
| 107 |
// Follow https://no-color.org/ |
| 108 |
if (isset($_SERVER['NO_COLOR']) || false !== getenv('NO_COLOR')) { |
| 109 |
return false; |
| 110 |
} |
| 111 |
|
| 112 |
if ('Hyper' === getenv('TERM_PROGRAM')) { |
| 113 |
return true; |
| 114 |
} |
| 115 |
|
| 116 |
if (\DIRECTORY_SEPARATOR === '\\') { |
| 117 |
return (\function_exists('sapi_windows_vt100_support') |
| 118 |
&& @sapi_windows_vt100_support($stream)) |
| 119 |
|| false !== getenv('ANSICON') |
| 120 |
|| 'ON' === getenv('ConEmuANSI') |
| 121 |
|| 'xterm' === getenv('TERM'); |
| 122 |
} |
| 123 |
|
| 124 |
if (\function_exists('stream_isatty')) { |
| 125 |
return @stream_isatty($stream); |
| 126 |
} |
| 127 |
|
| 128 |
if (\function_exists('posix_isatty')) { |
| 129 |
return @posix_isatty($stream); |
| 130 |
} |
| 131 |
|
| 132 |
$stat = @fstat($stream); |
| 133 |
|
| 134 |
// Check if formatted mode is S_IFCHR |
| 135 |
return $stat ? 0020000 === ($stat['mode'] & 0170000) : false; |
| 136 |
} |
| 137 |
} |