/
OutputStyle.php
181 lines (156 loc) · 5.03 KB
/
OutputStyle.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
<?php
namespace Concrete\Core\Console;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\ChoiceQuestion;
use Symfony\Component\Console\Question\Question;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Console\Terminal;
/**
* Concrete's output style.
* Inspiration goes to illuminate/console
*/
class OutputStyle extends SymfonyStyle
{
/** @var \Symfony\Component\Console\Output\OutputInterface */
private $output;
/**
* Create a new Console OutputStyle instance.
*
* @param \Symfony\Component\Console\Input\InputInterface $input
* @param \Symfony\Component\Console\Output\OutputInterface $output
* @return void
*/
public function __construct(InputInterface $input, OutputInterface $output)
{
$this->output = $output;
parent::__construct($input, $output);
}
/**
* Ask a question with autocompletion
*
* @param $question
* @param array $choices
* @param null $default
* @param null $attempts
* @param bool $strict
* @return string
*/
public function askWithCompletion($question, array $choices, $default = null, $attempts = null, $strict = false)
{
$question = new Question($question, $default);
$question->setMaxAttempts($attempts)->setAutocompleterValues($choices);
if ($strict) {
$question->setValidator(function($result) use ($choices) {
if (!in_array($result, $choices)) {
throw new \InvalidArgumentException(sprintf(
'The provided answer is ambiguous. Value should be one of %s',
implode(' or ', $choices)));
}
return $result;
});
}
return $this->askQuestion($question);
}
/**
* @see askSecretQuestion
*/
public function secret($question, $fallback = true)
{
return $this->askSecretQuestion($question, $fallback);
}
/**
* Ask a question while hiding the response
*
* @param $question
* @param bool $fallback
* @return string
*/
public function askSecretQuestion($question, $fallback = true)
{
$question = new Question($question);
$question->setHidden(true)->setHiddenFallback($fallback);
return $this->askQuestion($question);
}
/**
* Ask a question with a list of allowed answers
*
* @param string $question
* @param array $choices
* @param null $default
* @param null $attempts
* @param null $multiple
* @return mixed
*/
public function choice($question, array $choices, $default = null, $attempts = null, $multiple = null)
{
$question = new ChoiceQuestion($question, $choices, $default);
$question->setMaxAttempts($attempts)->setMultiselect($multiple);
return $this->askQuestion($question);
}
/**
* Create a table instance
*
* @see Table::initStyles()
* @param array $headers
* @param array $rows
* @param string $tableStyle
* @param array $columnStyles
*/
public function table(array $headers, array $rows, $tableStyle = 'default', array $columnStyles = [])
{
$table = new Table($this);
$table->setHeaders($headers)->setRows($rows)->setStyle($tableStyle);
foreach ($columnStyles as $columnIndex => $columnStyle) {
$table->setColumnStyle($columnIndex, $columnStyle);
}
$table->render();
}
/**
* Output in even width columns
*
* @param array $values
* @param int $width
* @param string $tableStyle
* @param array $columnStyles
*/
public function columns(array $values, $width = 5, $tableStyle = 'compact', array $columnStyles = [])
{
$table = new Table($this);
$table->setHeaders([])->setRows(iterator_to_array($this->chunk($values, $width)))->setStyle($tableStyle);
foreach ($columnStyles as $columnIndex => $columnStyle) {
$table->setColumnStyle($columnIndex, $columnStyle);
}
$columnWidths = $this->getColumnWidths($width);
$table->setColumnWidths($columnWidths);
$table->render();
}
protected function chunk(array $values, $size)
{
$chunk = [];
foreach ($values as $value) {
$chunk[] = $value;
if (count($chunk) === $size) {
yield $chunk;
$chunk = [];
}
}
yield $chunk;
}
private function getColumnWidths($width)
{
$terminal = new Terminal();
$totalWidth = $terminal->getWidth();
$left = $width;
$columns = [];
while ($left > 1) {
$columnSize = ceil($totalWidth / $left);
$columns[] = $columnSize;
$totalWidth -= $columnSize;
$left--;
}
$columns[] = $totalWidth;
return $columns;
}
}