forked from backdrop-contrib/bee
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinput.inc
193 lines (176 loc) · 5.22 KB
/
input.inc
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
182
183
184
185
186
187
188
189
190
191
192
193
<?php
/**
* @file
* Functions for dealing with user input from the terminal.
*/
/**
* Ask the user the confirm a choice (i.e. a yes/no question).
*
* @param string $question
* The question to ask the user.
* @param bool $default
* The default answer (shown to the user in uppercase). Defaults to FALSE.
*
* @return bool
* TRUE if the user enters "y", FALSE if "n".
*/
function bee_confirm($question, $default = FALSE) {
global $_bee_yes_mode;
// Set the default option.
if ($default) {
$options = ' (Y/n): ';
}
else {
$options = ' (y/N): ';
}
// Display the question.
bee_render_text(array(
'value' => $question . $options,
'#color' => 'cyan',
'#bold' => TRUE,
), FALSE);
// Return TRUE if `yes_mode` is enabled.
if ($_bee_yes_mode) {
bee_render_text(array('value' => 'y'));
return TRUE;
}
// Get answer from user.
$answer = strtolower(trim(fgets(STDIN)));
// Process user input.
switch ($answer) {
case 'y':
case 'yes':
return TRUE;
case 'n':
case 'no':
return FALSE;
case '':
return $default;
default:
bee_render_text(array(
'value' => bt("Invalid input. Please enter either 'y' or 'n', or press Enter to accept the default."),
'#color' => 'red',
));
return bee_confirm($question, $default);
}
}
/**
* Ask the user to select an option from a list.
*
* @param array $options
* An associative array of choices to display to the user, where keys are
* return values (that will be sent back to the calling function) and values
* are translated choice names. The first option should generally be a special
* one (e.g. Cancel or All).
* @param string $message
* The message to display to the user, prompting for input.
* @param mixed $default
* The array key of the default answer (shown to the user in brackets).
* @param boolean $repeat
* Whether this choice is being reapeated (because of an invalid answer).
* Repeated choices just ask for input, rather than displaying the full list
* of options again. Defaults to FALSE.
*
* @return mixed
* The array key of the selected option.
*/
function bee_choice(array $options, $message, $default, $repeat = FALSE) {
$keys = array_keys($options);
$rows = array();
// Set the options.
foreach ($keys as $number => $key) {
$row = array(
array('value' => " $number "),
array('value' => $options[$key]),
);
if ($default === $key) {
$row[0]['value'] = "[$number]";
}
$rows[] = $row;
}
// Display the message and options.
if (!$repeat) {
bee_render_text(array(
'value' => $message,
'#color' => 'cyan',
'#bold' => TRUE,
), TRUE);
echo "\n";
bee_render_table(array(
'rows' => $rows,
'delimiter' => ' ',
'delimiter_left' => ' ',
'delimiter_right' => ' ',
), TRUE);
}
bee_render_text(array('value' => bt('Enter a number: ')), FALSE);
// Get answer from user.
$answer = trim(fgets(STDIN));
// Process user input.
if (is_numeric($answer)) {
$answer = (int) $answer;
if (isset($keys[$answer])) {
return $keys[$answer];
}
}
elseif ($answer == '') {
return $default;
}
// Prompt the user for input again.
bee_render_text(array(
'value' => bt('Invalid input. Please enter a number corresponding to the options above, or press Enter to accept the [default] option.'),
'#color' => 'red',
));
return bee_choice($options, $message, $default, TRUE);
}
/**
* Ask the user to enter a string of data (e.g. their name or email address).
*
* @param string $message
* The message to display to the user, prompting for input. A colon ':' will
* be automatically appended to the end of this string.
* @param string $default
* The default answer (shown to the user in brackets). Defaults to an empty
* string (i.e. no default value shown) - users can still just press Enter
* which will return an empty string (which may or may not be accepted,
* depending on the value of $required).
* @param bool $required
* Whether or not the user is required to provide an answer. This is only
* taken into account when $default is also empty. If TRUE, an empty string
* from the user will not be accepted and the user will be prompted again.
* Defaults to FALSE.
*
* @return string
* The user's raw input, or the value of $default. NOTE: it is the calling
* functions job to sanitize this text appropriately prior to using it.
*/
function bee_input($message, $default = '', $required = FALSE) {
// Add the default value in brackets (if any).
if (!empty($default)) {
$message .= " [$default]";
}
$message .= ': ';
// Display the message.
bee_render_text(array(
'value' => $message,
'#color' => 'cyan',
'#bold' => TRUE,
), FALSE);
// Get answer from user.
$answer = trim(fgets(STDIN));
// Process user input.
if ($answer == '') {
if ($default != '' || !$required) {
return $default;
}
}
else {
return $answer;
}
// Prompt the user for input again.
bee_render_text(array(
'value' => bt('Invalid input. This answer is required and cannot be left empty. Please try again.'),
'#color' => 'red',
));
return bee_input($message, $default, TRUE);
}