2 namespace Consolidation\AnnotatedCommand\Options;
4 use Symfony\Component\Console\Application;
5 use Consolidation\AnnotatedCommand\CommandData;
6 use Consolidation\OutputFormatters\Options\FormatterOptions;
8 class PrepareTerminalWidthOption implements PrepareFormatter
10 /** var Application */
11 protected $application;
14 protected $defaultWidth;
17 protected $maxWidth = PHP_INT_MAX;
20 protected $minWidth = 0;
22 public function __construct($defaultWidth = 0)
24 $this->defaultWidth = $defaultWidth;
27 public function setApplication(Application $application)
29 $this->application = $application;
32 public function prepare(CommandData $commandData, FormatterOptions $options)
34 $width = $this->getTerminalWidth();
36 $width = $this->defaultWidth;
39 // Enforce minimum and maximum widths
40 $width = min($width, $this->getMaxWidth($commandData));
41 $width = max($width, $this->getMinWidth($commandData));
43 $options->setWidth($width);
46 protected function getTerminalWidth()
48 if (!$this->application) {
52 $dimensions = $this->application->getTerminalDimensions();
53 if ($dimensions[0] == null) {
57 return $dimensions[0];
60 protected function getMaxWidth(CommandData $commandData)
62 return $this->maxWidth;
65 protected function getMinWidth(CommandData $commandData)
67 return $this->minWidth;