SymfonyStyle.php 14 KB

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