|
2 | 2 | /*! |
3 | 3 | * PHP Cli |
4 | 4 | * |
5 | | - * Version 0.0.1 |
| 5 | + * Version 0.0.2 |
6 | 6 | * |
7 | 7 | * Portions of this code were initially from the FuelPHP Framework, |
8 | 8 | * version 1.7.x, and used here under the MIT license they were |
@@ -106,6 +106,26 @@ public static function getOptions() |
106 | 106 | return static::$options; |
107 | 107 | } |
108 | 108 |
|
| 109 | + public static function getOptionString(): string |
| 110 | + { |
| 111 | + if (! count(static::$options)) |
| 112 | + { |
| 113 | + return ''; |
| 114 | + } |
| 115 | + $out = ''; |
| 116 | + foreach (static::$options as $name => $value) |
| 117 | + { |
| 118 | + // If there's a space, we need to group |
| 119 | + // so it will pass correctly. |
| 120 | + if (mb_strpos($value, ' ') !== false) |
| 121 | + { |
| 122 | + $value = '"'.$value.'"'; |
| 123 | + } |
| 124 | + $out .= "-{$name} $value "; |
| 125 | + } |
| 126 | + return $out; |
| 127 | + } |
| 128 | + |
109 | 129 | public static function newLine(int $num = 1) |
110 | 130 | { |
111 | 131 | for ($i = 0; $i < $num; $i++) |
@@ -146,6 +166,88 @@ public static function color(string $text, string $foreground, string $backgroun |
146 | 166 | return $string; |
147 | 167 | } |
148 | 168 |
|
| 169 | + public static function getWidth(int $default = 80): int |
| 170 | + { |
| 171 | + if (static::isWindows()) |
| 172 | + { |
| 173 | + return $default; |
| 174 | + } |
| 175 | + return (int)shell_exec('tput cols'); |
| 176 | + } |
| 177 | + |
| 178 | + public static function getHeight(int $default = 32): int |
| 179 | + { |
| 180 | + if (static::isWindows()) |
| 181 | + { |
| 182 | + return $default; |
| 183 | + } |
| 184 | + return (int)shell_exec('tput lines'); |
| 185 | + } |
| 186 | + |
| 187 | + public static function showProgress($thisStep = 1, int $totalSteps = 10) |
| 188 | + { |
| 189 | + static $inProgress = false; |
| 190 | + // restore cursor position when progress is continuing. |
| 191 | + if ($inProgress !== false && $inProgress <= $thisStep) |
| 192 | + { |
| 193 | + fwrite(STDOUT, "\033[1A"); |
| 194 | + } |
| 195 | + $inProgress = $thisStep; |
| 196 | + if ($thisStep !== false) |
| 197 | + { |
| 198 | + // Don't allow div by zero or negative numbers.... |
| 199 | + $thisStep = abs($thisStep); |
| 200 | + $totalSteps = $totalSteps < 1 ? 1 : $totalSteps; |
| 201 | + $percent = intval(($thisStep / $totalSteps) * 100); |
| 202 | + $step = (int)round($percent / 10); |
| 203 | + // Write the progress bar |
| 204 | + fwrite(STDOUT, "[\033[32m".str_repeat('#', $step).str_repeat('.', 10 - $step)."\033[0m]"); |
| 205 | + // Textual representation... |
| 206 | + fwrite(STDOUT, sprintf(" %3d%% Complete", $percent).PHP_EOL); |
| 207 | + } |
| 208 | + else |
| 209 | + { |
| 210 | + fwrite(STDOUT, "\007"); |
| 211 | + } |
| 212 | + } |
| 213 | + |
| 214 | + public static function wrap(string $string = null, int $max = 0, int $pad_left = 0): string |
| 215 | + { |
| 216 | + if (empty($string)) |
| 217 | + { |
| 218 | + return ''; |
| 219 | + } |
| 220 | + if ($max == 0) |
| 221 | + { |
| 222 | + $max = CLI::getWidth(); |
| 223 | + } |
| 224 | + if (CLI::getWidth() < $max) |
| 225 | + { |
| 226 | + $max = CLI::getWidth(); |
| 227 | + } |
| 228 | + $max = $max - $pad_left; |
| 229 | + $lines = wordwrap($string, $max); |
| 230 | + if ($pad_left > 0) |
| 231 | + { |
| 232 | + $lines = explode(PHP_EOL, $lines); |
| 233 | + $first = true; |
| 234 | + array_walk($lines, function (&$line, $index) use ($max, $pad_left, &$first) |
| 235 | + { |
| 236 | + if ( ! $first) |
| 237 | + { |
| 238 | + $line = str_repeat(" ", $pad_left).$line; |
| 239 | + } |
| 240 | + else |
| 241 | + { |
| 242 | + $first = false; |
| 243 | + } |
| 244 | + }); |
| 245 | + $lines = implode(PHP_EOL, $lines); |
| 246 | + } |
| 247 | + return $lines; |
| 248 | + } |
| 249 | + |
| 250 | + |
149 | 251 | public static function clearScreen() |
150 | 252 | { |
151 | 253 | static::isWindows() |
|
0 commit comments