Skip to content

Commit 132cc71

Browse files
committed
initial commit
Signed-off-by: Dion Purushotham <mail@dion.codes>
0 parents  commit 132cc71

File tree

4 files changed

+118
-0
lines changed

4 files changed

+118
-0
lines changed

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Script Progress
2+
This is a super simple library for php-cli script progress output. It’s easy-to-use and can print the estimated time that is left until your script is done.
3+
4+
`20% done. Appr. 00m 16s remaining.`
5+
6+
That’s it.
7+
8+
## Example Usage
9+
```php
10+
$numberOfItems = 20;
11+
$progress = new TinyApps\ScriptProgress\Progress($numberOfItems);
12+
13+
for ($i=0; $i <= $numberOfItems; $i++) {
14+
// do something
15+
$progress->update($i);
16+
}
17+
```
18+
19+
There also is an example in the corresponding folder.
20+
21+
More features might be added soon.

composer.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"name": "tinyapps/script-progress",
3+
"description": "Super simple library for live-updating php-cli script progress output with estimated time that is left.",
4+
"type": "library",
5+
"version": "1.0.0",
6+
"require": {
7+
"php": ">=7.0.0"
8+
},
9+
"autoload": {
10+
"psr-4": {
11+
"TinyApps\\ScriptProgress\\": "src/"
12+
}
13+
},
14+
"license": "MIT",
15+
"authors": [
16+
{
17+
"name": "Dion Purushotham",
18+
"email": "hello@tinyapps.de"
19+
}
20+
]
21+
}

examples/example.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
use TinyApps\ScriptProgress\Progress;
4+
5+
require __DIR__ . '/../vendor/autoload.php';
6+
7+
$progress = new Progress(20);
8+
9+
for ($i=0; $i <= 20; $i++) {
10+
$progress->update($i);
11+
sleep(1);
12+
}

src/Progress.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
namespace TinyApps\ScriptProgress;
4+
5+
/**
6+
* @author Dion Purushotham <hello@tinyapps.de>
7+
*/
8+
class Progress {
9+
10+
protected int $total;
11+
protected int $startTime;
12+
protected int $lastPrintedTime;
13+
protected int $interval = 1;
14+
15+
/**
16+
* Initialize live-updating progress text
17+
*
18+
* @param integer $total Total amount of items (matching 100% progress)
19+
* @param integer $interval How many seconds should at least (!) pass between output/update
20+
*/
21+
public function __construct(int $total, int $interval = 1) {
22+
$this->total = $total;
23+
$this->startTime = time();
24+
$this->lastPrintedTime = time();
25+
$this->interval = $interval;
26+
27+
echo "\n0% done. Please wait...";
28+
}
29+
30+
/**
31+
* Prints and updates the command line output (considering interval)
32+
*
33+
* @param integer $done number of items done (for calculating progress)
34+
*
35+
* @return void
36+
*/
37+
public function update(int $done) {
38+
if ($done >= $this->total) {
39+
$timePassed = time() - $this->startTime;
40+
$minutes = floor($timePassed / 60);
41+
$seconds = floor($timePassed % 60);
42+
43+
echo "\r";
44+
echo '100% done in ' . $minutes . ' min. and ' . $seconds . ' sec.';
45+
echo str_repeat(' ', 5) . "\n";
46+
47+
return;
48+
}
49+
50+
if ($done > 0 && $this->lastPrintedTime < time() - $this->interval) {
51+
$timePassed = time() - $this->startTime;
52+
$averageTimeEach = $timePassed / $done;
53+
$timeRemaining = $averageTimeEach * ($this->total - $done);
54+
$minutesRemaining = floor($timeRemaining / 60);
55+
$secondsRemaining = floor($timeRemaining % 60);
56+
57+
$percentage = floor(($done / $this->total) * 100);
58+
59+
echo "\r";
60+
echo ($percentage < 10 ? '0' . $percentage : $percentage) . '% done. ';
61+
echo 'Appr. ' . ($minutesRemaining > 9 ? $minutesRemaining : '0' . $minutesRemaining) . 'm ' . ($secondsRemaining > 9 ? $secondsRemaining : '0' . $secondsRemaining) . 's remaining. ';
62+
}
63+
}
64+
}

0 commit comments

Comments
 (0)