ConsoleSectionOutput.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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\Output;
  11. use Symfony\Component\Console\Formatter\OutputFormatterInterface;
  12. use Symfony\Component\Console\Helper\Helper;
  13. use Symfony\Component\Console\Terminal;
  14. /**
  15. * @author Pierre du Plessis <pdples@gmail.com>
  16. * @author Gabriel Ostrolucký <gabriel.ostrolucky@gmail.com>
  17. */
  18. class ConsoleSectionOutput extends StreamOutput
  19. {
  20. /**
  21. * @var mixed[]
  22. */
  23. private $content = [];
  24. /**
  25. * @var int
  26. */
  27. private $lines = 0;
  28. /**
  29. * @var mixed[]
  30. */
  31. private $sections;
  32. /**
  33. * @var \Symfony\Component\Console\Terminal
  34. */
  35. private $terminal;
  36. /**
  37. * @var int
  38. */
  39. private $maxHeight = 0;
  40. /**
  41. * @param resource $stream
  42. * @param ConsoleSectionOutput[] $sections
  43. * @param int $verbosity
  44. * @param bool $decorated
  45. * @param \Symfony\Component\Console\Formatter\OutputFormatterInterface $formatter
  46. */
  47. public function __construct($stream, &$sections, $verbosity, $decorated, $formatter)
  48. {
  49. parent::__construct($stream, $verbosity, $decorated, $formatter);
  50. array_unshift($sections, $this);
  51. $this->sections = &$sections;
  52. $this->terminal = new Terminal();
  53. }
  54. /**
  55. * Defines a maximum number of lines for this section.
  56. *
  57. * When more lines are added, the section will automatically scroll to the
  58. * end (i.e. remove the first lines to comply with the max height).
  59. * @param int $maxHeight
  60. */
  61. public function setMaxHeight($maxHeight)
  62. {
  63. // when changing max height, clear output of current section and redraw again with the new height
  64. $existingContent = $this->popStreamContentUntilCurrentSection($this->maxHeight ? min($this->maxHeight, $this->lines) : $this->lines);
  65. $this->maxHeight = $maxHeight;
  66. parent::doWrite($this->getVisibleContent(), false);
  67. parent::doWrite($existingContent, false);
  68. }
  69. /**
  70. * Clears previous output for this section.
  71. *
  72. * @param int $lines Number of lines to clear. If null, then the entire output of this section is cleared
  73. *
  74. * @return void
  75. */
  76. public function clear($lines = null)
  77. {
  78. if (empty($this->content) || !$this->isDecorated()) {
  79. return;
  80. }
  81. if ($lines) {
  82. array_splice($this->content, -$lines);
  83. } else {
  84. $lines = $this->lines;
  85. $this->content = [];
  86. }
  87. $this->lines -= $lines;
  88. parent::doWrite($this->popStreamContentUntilCurrentSection($this->maxHeight ? min($this->maxHeight, $lines) : $lines), false);
  89. }
  90. /**
  91. * Overwrites the previous output with a new message.
  92. *
  93. * @return void
  94. * @param string|mixed[] $message
  95. */
  96. public function overwrite($message)
  97. {
  98. $this->clear();
  99. $this->writeln($message);
  100. }
  101. public function getContent()
  102. {
  103. return implode('', $this->content);
  104. }
  105. public function getVisibleContent()
  106. {
  107. if (0 === $this->maxHeight) {
  108. return $this->getContent();
  109. }
  110. return implode('', \array_slice($this->content, -$this->maxHeight));
  111. }
  112. /**
  113. * @internal
  114. * @param string $input
  115. * @param bool $newline
  116. */
  117. public function addContent($input, $newline = true)
  118. {
  119. $width = $this->terminal->getWidth();
  120. $lines = explode(\PHP_EOL, $input);
  121. $linesAdded = 0;
  122. $count = \count($lines) - 1;
  123. foreach ($lines as $i => $lineContent) {
  124. // re-add the line break (that has been removed in the above `explode()` for
  125. // - every line that is not the last line
  126. // - if $newline is required, also add it to the last line
  127. // - if it's not new line, but input ending with `\PHP_EOL`
  128. if ($i < $count || $newline || substr_compare($input, \PHP_EOL, -strlen(\PHP_EOL)) === 0) {
  129. $lineContent .= \PHP_EOL;
  130. }
  131. // skip line if there is no text (or newline for that matter)
  132. if ('' === $lineContent) {
  133. continue;
  134. }
  135. // For the first line, check if the previous line (last entry of `$this->content`)
  136. // needs to be continued (i.e. does not end with a line break).
  137. if (0 === $i
  138. && (false !== $lastLine = end($this->content))
  139. && substr_compare($lastLine, \PHP_EOL, -strlen(\PHP_EOL)) !== 0
  140. ) {
  141. // deduct the line count of the previous line
  142. $this->lines -= (int) ceil($this->getDisplayLength($lastLine) / $width) ?: 1;
  143. // concatenate previous and new line
  144. $lineContent = $lastLine.$lineContent;
  145. // replace last entry of `$this->content` with the new expanded line
  146. array_splice($this->content, -1, 1, $lineContent);
  147. } else {
  148. // otherwise just add the new content
  149. $this->content[] = $lineContent;
  150. }
  151. $linesAdded += (int) ceil($this->getDisplayLength($lineContent) / $width) ?: 1;
  152. }
  153. $this->lines += $linesAdded;
  154. return $linesAdded;
  155. }
  156. /**
  157. * @internal
  158. */
  159. public function addNewLineOfInputSubmit()
  160. {
  161. $this->content[] = \PHP_EOL;
  162. ++$this->lines;
  163. }
  164. /**
  165. * @return void
  166. * @param string $message
  167. * @param bool $newline
  168. */
  169. protected function doWrite($message, $newline)
  170. {
  171. if (!$this->isDecorated()) {
  172. parent::doWrite($message, $newline);
  173. return;
  174. }
  175. // Check if the previous line (last entry of `$this->content`) needs to be continued
  176. // (i.e. does not end with a line break). In which case, it needs to be erased first.
  177. $linesToClear = $deleteLastLine = ($lastLine = end($this->content) ?: '') && substr_compare($lastLine, \PHP_EOL, -strlen(\PHP_EOL)) !== 0 ? 1 : 0;
  178. $linesAdded = $this->addContent($message, $newline);
  179. if ($lineOverflow = $this->maxHeight > 0 && $this->lines > $this->maxHeight) {
  180. // on overflow, clear the whole section and redraw again (to remove the first lines)
  181. $linesToClear = $this->maxHeight;
  182. }
  183. $erasedContent = $this->popStreamContentUntilCurrentSection($linesToClear);
  184. if ($lineOverflow) {
  185. // redraw existing lines of the section
  186. $previousLinesOfSection = \array_slice($this->content, $this->lines - $this->maxHeight, $this->maxHeight - $linesAdded);
  187. parent::doWrite(implode('', $previousLinesOfSection), false);
  188. }
  189. // if the last line was removed, re-print its content together with the new content.
  190. // otherwise, just print the new content.
  191. parent::doWrite($deleteLastLine ? $lastLine.$message : $message, true);
  192. parent::doWrite($erasedContent, false);
  193. }
  194. /**
  195. * At initial stage, cursor is at the end of stream output. This method makes cursor crawl upwards until it hits
  196. * current section. Then it erases content it crawled through. Optionally, it erases part of current section too.
  197. * @param int $numberOfLinesToClearFromCurrentSection
  198. */
  199. private function popStreamContentUntilCurrentSection($numberOfLinesToClearFromCurrentSection = 0)
  200. {
  201. $numberOfLinesToClear = $numberOfLinesToClearFromCurrentSection;
  202. $erasedContent = [];
  203. foreach ($this->sections as $section) {
  204. if ($section === $this) {
  205. break;
  206. }
  207. $numberOfLinesToClear += $section->lines;
  208. if ('' !== $sectionContent = $section->getVisibleContent()) {
  209. if (substr_compare($sectionContent, \PHP_EOL, -strlen(\PHP_EOL)) !== 0) {
  210. $sectionContent .= \PHP_EOL;
  211. }
  212. $erasedContent[] = $sectionContent;
  213. }
  214. }
  215. if ($numberOfLinesToClear > 0) {
  216. // move cursor up n lines
  217. parent::doWrite(sprintf("\x1b[%dA", $numberOfLinesToClear), false);
  218. // erase to end of screen
  219. parent::doWrite("\x1b[0J", false);
  220. }
  221. return implode('', array_reverse($erasedContent));
  222. }
  223. /**
  224. * @param string $text
  225. */
  226. private function getDisplayLength($text)
  227. {
  228. return Helper::width(Helper::removeDecoration($this->getFormatter(), str_replace("\t", ' ', $text)));
  229. }
  230. }