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