6c70b7c2400d8206a365823e81b87b668a4c3319
[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     protected $terminal;
14
15     /** var int */
16     protected $defaultWidth;
17
18     /** var int */
19     protected $maxWidth = PHP_INT_MAX;
20
21     /** var int */
22     protected $minWidth = 0;
23
24     /* var boolean */
25     protected $shouldWrap = true;
26
27     public function __construct($defaultWidth = 0)
28     {
29         $this->defaultWidth = $defaultWidth;
30     }
31
32     public function setApplication(Application $application)
33     {
34         $this->application = $application;
35     }
36
37     public function setTerminal($terminal)
38     {
39         $this->terminal = $terminal;
40     }
41
42     public function getTerminal()
43     {
44         if (!$this->terminal && class_exists('\Symfony\Component\Console\Terminal')) {
45             $this->terminal = new \Symfony\Component\Console\Terminal();
46         }
47         return $this->terminal;
48     }
49
50     public function enableWrap($shouldWrap)
51     {
52         $this->shouldWrap = $shouldWrap;
53     }
54
55     public function prepare(CommandData $commandData, FormatterOptions $options)
56     {
57         $width = $this->getTerminalWidth();
58         if (!$width) {
59             $width = $this->defaultWidth;
60         }
61
62         // Enforce minimum and maximum widths
63         $width = min($width, $this->getMaxWidth($commandData));
64         $width = max($width, $this->getMinWidth($commandData));
65
66         $options->setWidth($width);
67     }
68
69     protected function getTerminalWidth()
70     {
71         // Don't wrap if wrapping has been disabled.
72         if (!$this->shouldWrap) {
73             return 0;
74         }
75
76         $terminal = $this->getTerminal();
77         if ($terminal) {
78             return $terminal->getWidth();
79         }
80
81         return $this->getTerminalWidthViaApplication();
82     }
83
84     protected function getTerminalWidthViaApplication()
85     {
86         if (!$this->application) {
87             return 0;
88         }
89         $dimensions = $this->application->getTerminalDimensions();
90         if ($dimensions[0] == null) {
91             return 0;
92         }
93
94         return $dimensions[0];
95     }
96
97     protected function getMaxWidth(CommandData $commandData)
98     {
99         return $this->maxWidth;
100     }
101
102     protected function getMinWidth(CommandData $commandData)
103     {
104         return $this->minWidth;
105     }
106 }