Skip to content

Commit 0cc3368

Browse files
committed
add showProgress
1 parent 917e306 commit 0cc3368

File tree

3 files changed

+110
-1
lines changed

3 files changed

+110
-1
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.DS_Store

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,12 @@ $color = PHPCli::prompt('What is your favorite color?');
5454
$color = PHPCli::prompt('What is your favourite color?', 'white');
5555
$ready = PHPCli::prompt('Are you ready?', array('y','n'));
5656

57+
// Displays a progress bar on the CLI. You must call it repeatedly to update it. Set $thisStep = false to erase the progress bar.
58+
for ($i=0; $i < 100; $i++) {
59+
PHPCli::showProgress($i,100);
60+
sleep(1);
61+
}
62+
5763
```
5864

5965

src/PHPCli.php

Lines changed: 103 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/*!
33
* PHP Cli
44
*
5-
* Version 0.0.1
5+
* Version 0.0.2
66
*
77
* Portions of this code were initially from the FuelPHP Framework,
88
* version 1.7.x, and used here under the MIT license they were
@@ -106,6 +106,26 @@ public static function getOptions()
106106
return static::$options;
107107
}
108108

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+
109129
public static function newLine(int $num = 1)
110130
{
111131
for ($i = 0; $i < $num; $i++)
@@ -146,6 +166,88 @@ public static function color(string $text, string $foreground, string $backgroun
146166
return $string;
147167
}
148168

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+
149251
public static function clearScreen()
150252
{
151253
static::isWindows()

0 commit comments

Comments
 (0)