SymfonyStyle.php 14 KB

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