-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconsole.php
191 lines (162 loc) · 4.84 KB
/
console.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
182
183
184
185
186
187
188
189
190
191
<?php
/**
* Copyright (c) 2006 Richard Heyes (http://www.phpguru.org/)
*
* All rights reserved.
*
* This script is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* The GNU General Public License can be found at
* http://www.gnu.org/copyleft/gpl.html.
*
* This script is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
/**
* Common functionality for console scripts
*/
class Console
{
/**
* Stdin file pointer
*/
public static $stdin;
/**
* Returns stdin file pointer
*
* @return resource STDIN file pointer
*/
private static function GetStdin()
{
if (!self::$stdin) {
self::$stdin = fopen('php://stdin', 'r');
}
return self::$stdin;
}
/**
* Pauses execution until enter is pressed
*/
public static function Pause()
{
fgets(self::GetStdin(), 8192);
}
/**
* Asks a boolean style yes/no question. Valid input is:
*
* o Yes: 1/y/yes/true
* o No: 0/n/no/false
*
* @param string $question The string to print. Should be a yes/no
* question the user can answer. The following
* will be appended to the question:
* "[Yes]/No"
* @param bool $default The default answer if only enter is pressed.
*/
public static function BooleanQuestion($question, $default = null)
{
if (!is_null($default)) {
$defaultStr = $default ? '[Yes]/No' : 'Yes/[No]';
} else {
$defaultStr = 'Yes/No';
}
$fp = self::GetStdin();
while (true) {
echo $question, " ", $defaultStr, ": ";
$response = trim(fgets($fp, 8192));
if (!is_null($default) AND $response == '') {
return $default;
}
switch (strtolower($response)) {
case 'y':
case '1':
case 'yes':
case 'true':
return true;
case 'n':
case '0':
case 'no':
case 'false':
return false;
default:
continue;
}
}
}
/**
* Clears the screen. Specific to Linux (and possibly bash too)
*/
public static function ClearScreen()
{
echo chr(033), "c";
}
/**
* Returns a line of input from the screen with the corresponding
* LF character appended (if appropriate).
*
* @param int $buffer Line buffer. Defaults to 8192
*/
public static function GetLine($buffer = 8192)
{
return fgets(self::GetStdin(), $buffer);
}
/**
* Shows a console menu
*
* @param array $items The menu items. Should be a two dimensional array. 2nd
* dimensional should be associative containing the following
* keys:
* o identifier - The key/combo the user should enter to activate
* this menu. Usually a single character or number.
* This is lower cased when used for comparison, so
* mixing upper/lower case identifiers will not work.
* o text - The description associated with this menu item.
* o callback - Optional. If specified this callback is called
* using call_user_func(). If not specified then the
* identifier is returned instead, (after the callback
* has run the identifier is returned also). The callback
* is given one argument, which is the identifier of the
* menu item.
* @param bool $clear Whether to clear the screen before printing the menu.
* Defaults to false.
*/
public static function ShowMenu($items, $clear = false)
{
// Find the longest identifier
$max_length = 0;
foreach ($items as $k => $v) {
$identifiers[strtolower($v['identifier'])] = $k;
$max_length = max(strlen($v['identifier']), $max_length);
}
while (true) {
if ($clear) {
self::ClearScreen();
}
echo "Please select one of the following options:\n\n";
// Print the menu
foreach ($items as $k => $v) {
echo str_pad($v['identifier'], $max_length, ' ', STR_PAD_LEFT), ") ", $v['text'], "\n";
}
echo "\nSelect: ";
$input = trim(strtolower(self::GetLine()));
// Invalid menu item chosen
if (!isset($identifiers[$input])) {
echo "Invalid menu item chosen...\n";
sleep(1);
continue;
// Valid menu item chosen
} else {
$item = $items[$identifiers[$input]];
if (!empty($item['callback']) AND is_callable($item['callback'])) {
call_user_func($item['callback'], $input);
}
return $input;
}
}
}
}
?>