1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpSchool\CliMenu\Dialogue; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* @author Aydin Hassan <[email protected]> |
7
|
|
|
*/ |
8
|
|
|
class Confirm extends Dialogue |
9
|
|
|
{ |
10
|
|
|
|
11
|
|
|
private $yesText = 'Yes'; |
12
|
|
|
|
13
|
|
|
private $noText = 'No'; |
14
|
|
|
|
15
|
|
|
private $optionValue = false; |
16
|
|
|
|
17
|
|
|
public function setYesText($text) |
18
|
|
|
{ |
19
|
|
|
$this->yesText = $text; |
20
|
|
|
|
21
|
|
|
return $this; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
public function setNoText($text) |
25
|
|
|
{ |
26
|
|
|
$this->noText = $text; |
27
|
|
|
|
28
|
|
|
return $this; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function getYesText() |
32
|
|
|
{ |
33
|
|
|
return sprintf(' <%s> ', $this->yesText); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function getNoText() |
37
|
|
|
{ |
38
|
|
|
return sprintf(' <%s> ', $this->noText); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
private function setOptionValue(bool $value) |
42
|
|
|
{ |
43
|
|
|
$this->optionValue = $value; |
44
|
|
|
|
45
|
|
|
return $this; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
private function getOptionValue() |
49
|
|
|
{ |
50
|
|
|
return $this->optionValue; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
private function displayBody() |
54
|
|
|
{ |
55
|
|
|
$this->terminal->moveCursorToRow($this->y); |
56
|
|
|
$this->emptyRow(); |
57
|
|
|
$this->write(sprintf( |
58
|
|
|
"%s%s%s%s%s\n", |
59
|
|
|
$this->style->getUnselectedSetCode(), |
60
|
|
|
str_repeat(' ', $this->style->getPadding()), |
61
|
|
|
$this->text, |
62
|
|
|
str_repeat(' ', $this->style->getPadding()), |
63
|
|
|
$this->style->getUnselectedUnsetCode() |
64
|
|
|
)); |
65
|
|
|
$this->emptyRow(); |
66
|
|
|
|
67
|
|
|
$promptWidth = mb_strlen($this->text) + 4; |
68
|
|
|
$fillWidth = $promptWidth - (mb_strlen($this->getYesText()) + mb_strlen($this->getNoText())); |
69
|
|
|
$placeHolderWidth = 0 == ($fillWidth % 2) ? 2 : 1;//中间位宽度 |
70
|
|
|
$fillWidth = ($fillWidth - $placeHolderWidth) / 2; |
71
|
|
|
|
72
|
|
|
$this->write(sprintf( |
73
|
|
|
'%s%s%s', |
74
|
|
|
$this->style->getUnselectedSetCode(), |
75
|
|
|
str_repeat(' ', $fillWidth), |
76
|
|
|
$this->style->getUnselectedSetCode() |
77
|
|
|
)); |
78
|
|
|
$this->write( |
79
|
|
|
sprintf( |
80
|
|
|
'%s%s%s', |
81
|
|
|
$this->getOptionValue() ? $this->style->getSelectedSetCode() : $this->style->getUnselectedSetCode(), |
82
|
|
|
$this->getYesText(), |
83
|
|
|
$this->getOptionValue() ? $this->style->getSelectedSetCode() : $this->style->getUnselectedSetCode() |
84
|
|
|
), |
85
|
|
|
-1 |
86
|
|
|
); |
87
|
|
|
$this->write( |
88
|
|
|
sprintf( |
89
|
|
|
'%s%s%s', |
90
|
|
|
$this->style->getUnselectedSetCode(), |
91
|
|
|
str_repeat(' ', $placeHolderWidth), |
92
|
|
|
$this->style->getUnselectedSetCode() |
93
|
|
|
), |
94
|
|
|
-1 |
95
|
|
|
); |
96
|
|
|
$this->write( |
97
|
|
|
sprintf( |
98
|
|
|
'%s%s%s', |
99
|
|
|
$this->getOptionValue() ? $this->style->getUnselectedSetCode() : $this->style->getSelectedSetCode(), |
100
|
|
|
$this->getNoText(), |
101
|
|
|
$this->getOptionValue() ? $this->style->getUnselectedSetCode() : $this->style->getSelectedSetCode() |
102
|
|
|
), |
103
|
|
|
-1 |
104
|
|
|
); |
105
|
|
|
$this->write(sprintf( |
106
|
|
|
"%s%s%s\n", |
107
|
|
|
$this->style->getUnselectedSetCode(), |
108
|
|
|
str_repeat(' ', $fillWidth), |
109
|
|
|
$this->style->getUnselectedSetCode() |
110
|
|
|
), -1); |
111
|
|
|
|
112
|
|
|
$this->write(sprintf( |
113
|
|
|
"%s%s%s%s%s\n", |
114
|
|
|
$this->style->getUnselectedSetCode(), |
115
|
|
|
str_repeat(' ', $this->style->getPadding()), |
116
|
|
|
str_repeat(' ', mb_strlen($this->text)), |
117
|
|
|
str_repeat(' ', $this->style->getPadding()), |
118
|
|
|
$this->style->getUnselectedUnsetCode() |
119
|
|
|
)); |
120
|
|
|
$this->terminal->moveCursorToTop(); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
public function show(callable $callable) |
124
|
|
|
{ |
125
|
|
|
$this->assertMenuOpen(); |
126
|
|
|
|
127
|
|
|
$this->displayBody(); |
128
|
|
|
$input = $this->terminal->getKeyedInput(); |
129
|
|
|
while ('enter' !== $input) { |
130
|
|
|
if (in_array($input, ['left', 'right'])) { |
131
|
|
|
$this->parentMenu->redraw(); |
132
|
|
|
$this->setOptionValue('left' == $input); |
133
|
|
|
$this->displayBody(); |
134
|
|
|
} |
135
|
|
|
$input = $this->terminal->getKeyedInput(); |
136
|
|
|
} |
137
|
|
|
$this->parentMenu->redraw(); |
138
|
|
|
$callable($this->getOptionValue()); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Display confirmation with a button with the given text |
144
|
|
|
* |
145
|
|
|
* @param string $confirmText |
146
|
|
|
*/ |
147
|
|
|
public function display($confirmText = 'OK') |
148
|
|
|
{ |
149
|
|
|
$this->assertMenuOpen(); |
150
|
|
|
|
151
|
|
|
$this->terminal->moveCursorToRow($this->y); |
152
|
|
|
|
153
|
|
|
$promptWidth = mb_strlen($this->text) + 4; |
154
|
|
|
|
155
|
|
|
$this->emptyRow(); |
156
|
|
|
|
157
|
|
|
$this->write(sprintf( |
158
|
|
|
"%s%s%s%s%s\n", |
159
|
|
|
$this->style->getUnselectedSetCode(), |
160
|
|
|
str_repeat(' ', $this->style->getPadding()), |
161
|
|
|
$this->text, |
162
|
|
|
str_repeat(' ', $this->style->getPadding()), |
163
|
|
|
$this->style->getUnselectedUnsetCode() |
164
|
|
|
)); |
165
|
|
|
|
166
|
|
|
$this->emptyRow(); |
167
|
|
|
|
168
|
|
|
$confirmText = sprintf(' < %s > ', $confirmText); |
169
|
|
|
$leftFill = ($promptWidth / 2) - (mb_strlen($confirmText) / 2); |
170
|
|
|
|
171
|
|
|
$this->write(sprintf( |
172
|
|
|
'%s%s%s', |
173
|
|
|
$this->style->getUnselectedSetCode(), |
174
|
|
|
str_repeat(' ', $leftFill), |
175
|
|
|
$this->style->getUnselectedSetCode() |
176
|
|
|
)); |
177
|
|
|
|
178
|
|
|
$this->write( |
179
|
|
|
sprintf( |
180
|
|
|
'%s%s%s', |
181
|
|
|
$this->style->getSelectedSetCode(), |
182
|
|
|
$confirmText, |
183
|
|
|
$this->style->getSelectedUnsetCode() |
184
|
|
|
), |
185
|
|
|
-1 |
186
|
|
|
); |
187
|
|
|
|
188
|
|
|
$this->write( |
189
|
|
|
sprintf( |
190
|
|
|
"%s%s%s\n", |
191
|
|
|
$this->style->getUnselectedSetCode(), |
192
|
|
|
str_repeat(' ', ceil($promptWidth - $leftFill - mb_strlen($confirmText))), |
193
|
|
|
$this->style->getSelectedUnsetCode() |
194
|
|
|
), |
195
|
|
|
-1 |
196
|
|
|
); |
197
|
|
|
|
198
|
|
|
$this->write(sprintf( |
199
|
|
|
"%s%s%s%s%s\n", |
200
|
|
|
$this->style->getUnselectedSetCode(), |
201
|
|
|
str_repeat(' ', $this->style->getPadding()), |
202
|
|
|
str_repeat(' ', mb_strlen($this->text)), |
203
|
|
|
str_repeat(' ', $this->style->getPadding()), |
204
|
|
|
$this->style->getUnselectedUnsetCode() |
205
|
|
|
)); |
206
|
|
|
|
207
|
|
|
$this->terminal->moveCursorToTop(); |
208
|
|
|
$input = $this->terminal->getKeyedInput(); |
209
|
|
|
|
210
|
|
|
while ($input !== 'enter') { |
211
|
|
|
$input = $this->terminal->getKeyedInput(); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
$this->parentMenu->redraw(); |
215
|
|
|
} |
216
|
|
|
} |
217
|
|
|
|