No Description

SymfonyStyle.php 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Console\Style;
  11. use Symfony\Component\Console\Exception\InvalidArgumentException;
  12. use Symfony\Component\Console\Exception\RuntimeException;
  13. use Symfony\Component\Console\Formatter\OutputFormatter;
  14. use Symfony\Component\Console\Helper\Helper;
  15. use Symfony\Component\Console\Helper\ProgressBar;
  16. use Symfony\Component\Console\Helper\SymfonyQuestionHelper;
  17. use Symfony\Component\Console\Helper\Table;
  18. use Symfony\Component\Console\Helper\TableCell;
  19. use Symfony\Component\Console\Helper\TableSeparator;
  20. use Symfony\Component\Console\Input\InputInterface;
  21. use Symfony\Component\Console\Output\OutputInterface;
  22. use Symfony\Component\Console\Output\TrimmedBufferOutput;
  23. use Symfony\Component\Console\Question\ChoiceQuestion;
  24. use Symfony\Component\Console\Question\ConfirmationQuestion;
  25. use Symfony\Component\Console\Question\Question;
  26. use Symfony\Component\Console\Terminal;
  27. /**
  28. * Output decorator helpers for the Symfony Style Guide.
  29. *
  30. * @author Kevin Bond <kevinbond@gmail.com>
  31. */
  32. class SymfonyStyle extends OutputStyle
  33. {
  34. public const MAX_LINE_LENGTH = 120;
  35. private $input;
  36. private $questionHelper;
  37. private $progressBar;
  38. private $lineLength;
  39. private $bufferedOutput;
  40. public function __construct(InputInterface $input, OutputInterface $output)
  41. {
  42. $this->input = $input;
  43. $this->bufferedOutput = new TrimmedBufferOutput(\DIRECTORY_SEPARATOR === '\\' ? 4 : 2, $output->getVerbosity(), false, clone $output->getFormatter());
  44. // Windows cmd wraps lines as soon as the terminal width is reached, whether there are following chars or not.
  45. $width = (new Terminal())->getWidth() ?: self::MAX_LINE_LENGTH;
  46. $this->lineLength = min($width - (int) (\DIRECTORY_SEPARATOR === '\\'), self::MAX_LINE_LENGTH);
  47. parent::__construct($output);
  48. }
  49. /**
  50. * Formats a message as a block of text.
  51. *
  52. * @param string|array $messages The message to write in the block
  53. * @param string|null $type The block type (added in [] on first line)
  54. * @param string|null $style The style to apply to the whole block
  55. * @param string $prefix The prefix for the block
  56. * @param bool $padding Whether to add vertical padding
  57. * @param bool $escape Whether to escape the message
  58. */
  59. public function block($messages, $type = null, $style = null, $prefix = ' ', $padding = false, $escape = true)
  60. {
  61. $messages = \is_array($messages) ? array_values($messages) : [$messages];
  62. $this->autoPrependBlock();
  63. $this->writeln($this->createBlock($messages, $type, $style, $prefix, $padding, $escape));
  64. $this->newLine();
  65. }
  66. /**
  67. * {@inheritdoc}
  68. */
  69. public function title($message)
  70. {
  71. $this->autoPrependBlock();
  72. $this->writeln([
  73. sprintf('<comment>%s</>', OutputFormatter::escapeTrailingBackslash($message)),
  74. sprintf('<comment>%s</>', str_repeat('=', Helper::strlenWithoutDecoration($this->getFormatter(), $message))),
  75. ]);
  76. $this->newLine();
  77. }
  78. /**
  79. * {@inheritdoc}
  80. */
  81. public function section($message)
  82. {
  83. $this->autoPrependBlock();
  84. $this->writeln([
  85. sprintf('<comment>%s</>', OutputFormatter::escapeTrailingBackslash($message)),
  86. sprintf('<comment>%s</>', str_repeat('-', Helper::strlenWithoutDecoration($this->getFormatter(), $message))),
  87. ]);
  88. $this->newLine();
  89. }
  90. /**
  91. * {@inheritdoc}
  92. */
  93. public function listing(array $elements)
  94. {
  95. $this->autoPrependText();
  96. $elements = array_map(function ($element) {
  97. return sprintf(' * %s', $element);
  98. }, $elements);
  99. $this->writeln($elements);
  100. $this->newLine();
  101. }
  102. /**
  103. * {@inheritdoc}
  104. */
  105. public function text($message)
  106. {
  107. $this->autoPrependText();
  108. $messages = \is_array($message) ? array_values($message) : [$message];
  109. foreach ($messages as $message) {
  110. $this->writeln(sprintf(' %s', $message));
  111. }
  112. }
  113. /**
  114. * Formats a command comment.
  115. *
  116. * @param string|array $message
  117. */
  118. public function comment($message)
  119. {
  120. $this->block($message, null, null, '<fg=default;bg=default> // </>', false, false);
  121. }
  122. /**
  123. * {@inheritdoc}
  124. */
  125. public function success($message)
  126. {
  127. $this->block($message, 'OK', 'fg=black;bg=green', ' ', true);
  128. }
  129. /**
  130. * {@inheritdoc}
  131. */
  132. public function error($message)
  133. {
  134. $this->block($message, 'ERROR', 'fg=white;bg=red', ' ', true);
  135. }
  136. /**
  137. * {@inheritdoc}
  138. */
  139. public function warning($message)
  140. {
  141. $this->block($message, 'WARNING', 'fg=black;bg=yellow', ' ', true);
  142. }
  143. /**
  144. * {@inheritdoc}
  145. */
  146. public function note($message)
  147. {
  148. $this->block($message, 'NOTE', 'fg=yellow', ' ! ');
  149. }
  150. /**
  151. * {@inheritdoc}
  152. */
  153. public function caution($message)
  154. {
  155. $this->block($message, 'CAUTION', 'fg=white;bg=red', ' ! ', true);
  156. }
  157. /**
  158. * {@inheritdoc}
  159. */
  160. public function table(array $headers, array $rows)
  161. {
  162. $style = clone Table::getStyleDefinition('symfony-style-guide');
  163. $style->setCellHeaderFormat('<info>%s</info>');
  164. $table = new Table($this);
  165. $table->setHeaders($headers);
  166. $table->setRows($rows);
  167. $table->setStyle($style);
  168. $table->render();
  169. $this->newLine();
  170. }
  171. /**
  172. * Formats a horizontal table.
  173. */
  174. public function horizontalTable(array $headers, array $rows)
  175. {
  176. $style = clone Table::getStyleDefinition('symfony-style-guide');
  177. $style->setCellHeaderFormat('<info>%s</info>');
  178. $table = new Table($this);
  179. $table->setHeaders($headers);
  180. $table->setRows($rows);
  181. $table->setStyle($style);
  182. $table->setHorizontal(true);
  183. $table->render();
  184. $this->newLine();
  185. }
  186. /**
  187. * Formats a list of key/value horizontally.
  188. *
  189. * Each row can be one of:
  190. * * 'A title'
  191. * * ['key' => 'value']
  192. * * new TableSeparator()
  193. *
  194. * @param string|array|TableSeparator ...$list
  195. */
  196. public function definitionList(...$list)
  197. {
  198. $style = clone Table::getStyleDefinition('symfony-style-guide');
  199. $style->setCellHeaderFormat('<info>%s</info>');
  200. $table = new Table($this);
  201. $headers = [];
  202. $row = [];
  203. foreach ($list as $value) {
  204. if ($value instanceof TableSeparator) {
  205. $headers[] = $value;
  206. $row[] = $value;
  207. continue;
  208. }
  209. if (\is_string($value)) {
  210. $headers[] = new TableCell($value, ['colspan' => 2]);
  211. $row[] = null;
  212. continue;
  213. }
  214. if (!\is_array($value)) {
  215. throw new InvalidArgumentException('Value should be an array, string, or an instance of TableSeparator.');
  216. }
  217. $headers[] = key($value);
  218. $row[] = current($value);
  219. }
  220. $table->setHeaders($headers);
  221. $table->setRows([$row]);
  222. $table->setHorizontal();
  223. $table->setStyle($style);
  224. $table->render();
  225. $this->newLine();
  226. }
  227. /**
  228. * {@inheritdoc}
  229. */
  230. public function ask($question, $default = null, $validator = null)
  231. {
  232. $question = new Question($question, $default);
  233. $question->setValidator($validator);
  234. return $this->askQuestion($question);
  235. }
  236. /**
  237. * {@inheritdoc}
  238. */
  239. public function askHidden($question, $validator = null)
  240. {
  241. $question = new Question($question);
  242. $question->setHidden(true);
  243. $question->setValidator($validator);
  244. return $this->askQuestion($question);
  245. }
  246. /**
  247. * {@inheritdoc}
  248. */
  249. public function confirm($question, $default = true)
  250. {
  251. return $this->askQuestion(new ConfirmationQuestion($question, $default));
  252. }
  253. /**
  254. * {@inheritdoc}
  255. */
  256. public function choice($question, array $choices, $default = null)
  257. {
  258. if (null !== $default) {
  259. $values = array_flip($choices);
  260. $default = $values[$default] ?? $default;
  261. }
  262. return $this->askQuestion(new ChoiceQuestion($question, $choices, $default));
  263. }
  264. /**
  265. * {@inheritdoc}
  266. */
  267. public function progressStart($max = 0)
  268. {
  269. $this->progressBar = $this->createProgressBar($max);
  270. $this->progressBar->start();
  271. }
  272. /**
  273. * {@inheritdoc}
  274. */
  275. public function progressAdvance($step = 1)
  276. {
  277. $this->getProgressBar()->advance($step);
  278. }
  279. /**
  280. * {@inheritdoc}
  281. */
  282. public function progressFinish()
  283. {
  284. $this->getProgressBar()->finish();
  285. $this->newLine(2);
  286. $this->progressBar = null;
  287. }
  288. /**
  289. * {@inheritdoc}
  290. */
  291. public function createProgressBar($max = 0)
  292. {
  293. $progressBar = parent::createProgressBar($max);
  294. if ('\\' !== \DIRECTORY_SEPARATOR || 'Hyper' === getenv('TERM_PROGRAM')) {
  295. $progressBar->setEmptyBarCharacter('░'); // light shade character \u2591
  296. $progressBar->setProgressCharacter('');
  297. $progressBar->setBarCharacter('▓'); // dark shade character \u2593
  298. }
  299. return $progressBar;
  300. }
  301. /**
  302. * @return mixed
  303. */
  304. public function askQuestion(Question $question)
  305. {
  306. if ($this->input->isInteractive()) {
  307. $this->autoPrependBlock();
  308. }
  309. if (!$this->questionHelper) {
  310. $this->questionHelper = new SymfonyQuestionHelper();
  311. }
  312. $answer = $this->questionHelper->ask($this->input, $this, $question);
  313. if ($this->input->isInteractive()) {
  314. $this->newLine();
  315. $this->bufferedOutput->write("\n");
  316. }
  317. return $answer;
  318. }
  319. /**
  320. * {@inheritdoc}
  321. */
  322. public function writeln($messages, $type = self::OUTPUT_NORMAL)
  323. {
  324. if (!is_iterable($messages)) {
  325. $messages = [$messages];
  326. }
  327. foreach ($messages as $message) {
  328. parent::writeln($message, $type);
  329. $this->writeBuffer($message, true, $type);
  330. }
  331. }
  332. /**
  333. * {@inheritdoc}
  334. */
  335. public function write($messages, $newline = false, $type = self::OUTPUT_NORMAL)
  336. {
  337. if (!is_iterable($messages)) {
  338. $messages = [$messages];
  339. }
  340. foreach ($messages as $message) {
  341. parent::write($message, $newline, $type);
  342. $this->writeBuffer($message, $newline, $type);
  343. }
  344. }
  345. /**
  346. * {@inheritdoc}
  347. */
  348. public function newLine($count = 1)
  349. {
  350. parent::newLine($count);
  351. $this->bufferedOutput->write(str_repeat("\n", $count));
  352. }
  353. /**
  354. * Returns a new instance which makes use of stderr if available.
  355. *
  356. * @return self
  357. */
  358. public function getErrorStyle()
  359. {
  360. return new self($this->input, $this->getErrorOutput());
  361. }
  362. private function getProgressBar(): ProgressBar
  363. {
  364. if (!$this->progressBar) {
  365. throw new RuntimeException('The ProgressBar is not started.');
  366. }
  367. return $this->progressBar;
  368. }
  369. private function autoPrependBlock(): void
  370. {
  371. $chars = substr(str_replace(\PHP_EOL, "\n", $this->bufferedOutput->fetch()), -2);
  372. if (!isset($chars[0])) {
  373. $this->newLine(); //empty history, so we should start with a new line.
  374. return;
  375. }
  376. //Prepend new line for each non LF chars (This means no blank line was output before)
  377. $this->newLine(2 - substr_count($chars, "\n"));
  378. }
  379. private function autoPrependText(): void
  380. {
  381. $fetched = $this->bufferedOutput->fetch();
  382. //Prepend new line if last char isn't EOL:
  383. if (!str_ends_with($fetched, "\n")) {
  384. $this->newLine();
  385. }
  386. }
  387. private function writeBuffer(string $message, bool $newLine, int $type): void
  388. {
  389. // We need to know if the last chars are PHP_EOL
  390. $this->bufferedOutput->write($message, $newLine, $type);
  391. }
  392. private function createBlock(iterable $messages, string $type = null, string $style = null, string $prefix = ' ', bool $padding = false, bool $escape = false): array
  393. {
  394. $indentLength = 0;
  395. $prefixLength = Helper::strlenWithoutDecoration($this->getFormatter(), $prefix);
  396. $lines = [];
  397. if (null !== $type) {
  398. $type = sprintf('[%s] ', $type);
  399. $indentLength = \strlen($type);
  400. $lineIndentation = str_repeat(' ', $indentLength);
  401. }
  402. // wrap and add newlines for each element
  403. foreach ($messages as $key => $message) {
  404. if ($escape) {
  405. $message = OutputFormatter::escape($message);
  406. }
  407. $decorationLength = Helper::strlen($message) - Helper::strlenWithoutDecoration($this->getFormatter(), $message);
  408. $messageLineLength = min($this->lineLength - $prefixLength - $indentLength + $decorationLength, $this->lineLength);
  409. $messageLines = explode(\PHP_EOL, wordwrap($message, $messageLineLength, \PHP_EOL, true));
  410. foreach ($messageLines as $messageLine) {
  411. $lines[] = $messageLine;
  412. }
  413. if (\count($messages) > 1 && $key < \count($messages) - 1) {
  414. $lines[] = '';
  415. }
  416. }
  417. $firstLineIndex = 0;
  418. if ($padding && $this->isDecorated()) {
  419. $firstLineIndex = 1;
  420. array_unshift($lines, '');
  421. $lines[] = '';
  422. }
  423. foreach ($lines as $i => &$line) {
  424. if (null !== $type) {
  425. $line = $firstLineIndex === $i ? $type.$line : $lineIndentation.$line;
  426. }
  427. $line = $prefix.$line;
  428. $line .= str_repeat(' ', max($this->lineLength - Helper::strlenWithoutDecoration($this->getFormatter(), $line), 0));
  429. if ($style) {
  430. $line = sprintf('<%s>%s</>', $style, $line);
  431. }
  432. }
  433. return $lines;
  434. }
  435. }