Minor dependency updates
[yaffs-website] / vendor / consolidation / annotated-command / src / Options / PrepareTerminalWidthOption.php
1 <?php
2 namespace Consolidation\AnnotatedCommand\Options;
3
4 use Symfony\Component\Console\Application;
5 use Consolidation\AnnotatedCommand\CommandData;
6 use Consolidation\OutputFormatters\Options\FormatterOptions;
7
8 class PrepareTerminalWidthOption implements PrepareFormatter
9 {
10     /** var Application */
11     protected $application;
12
13     /** var int */
14     protected $defaultWidth;
15
16     /** var int */
17     protected $maxWidth = PHP_INT_MAX;
18
19     /** var int */
20     protected $minWidth = 0;
21
22     public function __construct($defaultWidth = 0)
23     {
24         $this->defaultWidth = $defaultWidth;
25     }
26
27     public function setApplication(Application $application)
28     {
29         $this->application = $application;
30     }
31
32     public function prepare(CommandData $commandData, FormatterOptions $options)
33     {
34         $width = $this->getTerminalWidth();
35         if (!$width) {
36             $width = $this->defaultWidth;
37         }
38
39         // Enforce minimum and maximum widths
40         $width = min($width, $this->getMaxWidth($commandData));
41         $width = max($width, $this->getMinWidth($commandData));
42
43         $options->setWidth($width);
44     }
45
46     protected function getTerminalWidth()
47     {
48         if (!$this->application) {
49             return 0;
50         }
51
52         $dimensions = $this->application->getTerminalDimensions();
53         if ($dimensions[0] == null) {
54             return 0;
55         }
56
57         return $dimensions[0];
58     }
59
60     protected function getMaxWidth(CommandData $commandData)
61     {
62         return $this->maxWidth;
63     }
64
65     protected function getMinWidth(CommandData $commandData)
66     {
67         return $this->minWidth;
68     }
69 }