Skip to content

Commit f3e95b6

Browse files
committed
Initial commit
0 parents  commit f3e95b6

14 files changed

Lines changed: 676 additions & 0 deletions

.editorconfig

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
; This file is for unifying the coding style for different editors and IDEs.
2+
; More information at http://editorconfig.org
3+
4+
root = true
5+
6+
[*]
7+
charset = utf-8
8+
end_of_line = lf
9+
insert_final_newline = true
10+
indent_size = 4
11+
indent_style = space
12+
trim_trailing_whitespace = true

.gitattributes

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Automatically normalize line endings
2+
* text=auto
3+
4+
# Explicitly set diffs
5+
*.md diff=markdown
6+
*.php diff=php
7+
8+
# Ignore all test and documentation
9+
/.editorconfig export-ignore
10+
/.gitattributes export-ignore
11+
/.github export-ignore
12+
/.gitignore export-ignore
13+
/phpunit.xml.dist export-ignore
14+
/tests export-ignore

.github/CODEOWNERS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* @nickbeen
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
name: Bug Report
2+
description: File a bug report
3+
title: "[Bug]: "
4+
labels: ["bug"]
5+
assignees:
6+
- nickbeen
7+
body:
8+
- type: markdown
9+
attributes:
10+
value: |
11+
Thanks for taking the time to fill out this bug report!
12+
- type: input
13+
id: library-version
14+
attributes:
15+
label: Library version
16+
description: "What version of php-cli-progress-bar do you have installed?"
17+
placeholder: "1.0.0"
18+
validations:
19+
required: true
20+
- type: input
21+
id: php-version
22+
attributes:
23+
label: PHP version
24+
description: "What version of PHP do you have installed?"
25+
placeholder: "8.0.0"
26+
validations:
27+
required: true
28+
- type: textarea
29+
id: bug-description
30+
attributes:
31+
label: Bug description
32+
description: What went wrong? What did you expect to happen?
33+
validations:
34+
required: true
35+
- type: textarea
36+
id: sample-code
37+
attributes:
38+
label: Sample code
39+
description: If applicable, provide relevant PHP code.
40+
render: PHP
41+
- type: checkboxes
42+
id: terms
43+
attributes:
44+
label: Code of Conduct
45+
description: By submitting this issue, you agree to follow the [Code of Conduct](https://github.com/nickbeen/.github/blob/main/.github/CODE_OF_CONDUCT.md)
46+
options:
47+
- label: I agree to follow the Code of Conduct
48+
required: true

.github/ISSUE_TEMPLATE/config.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
blank_issues_enabled: false
2+
contact_links:
3+
- name: Help, questions and ideas
4+
url: https://github.com/nickbeen/php-cli-progressbar/discussions
5+
about: "Please start a new discussion"

.github/dependabot.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
version: 2
2+
updates:
3+
4+
- package-ecosystem: "github-actions"
5+
directory: "/"
6+
schedule:
7+
interval: "weekly"
8+
time: "11:11"
9+
day: "sunday"

.github/workflows/run-tests.yml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
name: Run tests
2+
3+
on:
4+
pull_request:
5+
6+
jobs:
7+
run-tests:
8+
9+
strategy:
10+
fail-fast: false
11+
matrix:
12+
php: [8.1, 8.0]
13+
os: [ubuntu-latest, windows-latest]
14+
15+
name: PHP ${{ matrix.php }} - ${{ matrix.os }}
16+
runs-on: ${{ matrix.os }}
17+
18+
steps:
19+
- name: Checkout code
20+
uses: actions/checkout@v3
21+
22+
- name: Setup PHP
23+
uses: shivammathur/setup-php@v2
24+
with:
25+
php-version: ${{ matrix.php }}
26+
coverage: none
27+
28+
- name: Setup problem matcher
29+
run: echo "::add-matcher::${{ runner.tool_cache }}/phpunit.json"
30+
31+
- name: Install dependencies
32+
run: composer update --prefer-dist
33+
34+
- name: Run tests
35+
run: vendor/bin/phpunit

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.idea
2+
.phpunit.result.cache
3+
composer.lock
4+
phpunit.xml
5+
vendor

LICENSE.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2022 Nick Been
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
# PHP CLI Progress Bar
2+
3+
[![Latest version](https://img.shields.io/packagist/v/nickbeen/php-cli-progress-bar)](https://packagist.org/packages/nickbeen/php-cli-progress-bar)
4+
[![Build status](https://img.shields.io/github/workflow/status/nickbeen/php-cli-progress-bar/Run%20tests)](https://packagist.org/packages/nickbeen/php-cli-progress-bar)
5+
[![Total downloads](https://img.shields.io/packagist/dt/nickbeen/php-cli-progress-bar)](https://packagist.org/packages/nickbeen/php-cli-progress-bar)
6+
[![PHP Version](https://img.shields.io/packagist/php-v/nickbeen/php-cli-progress-bar)](https://packagist.org/packages/nickbeen/php-cli-progress-bar)
7+
[![License](https://img.shields.io/packagist/l/nickbeen/php-cli-progress-bar)](https://packagist.org/packages/nickbeen/php-cli-progress-bar)
8+
9+
For creating minimal progress bars in PHP CLI scripts.
10+
It has no dependencies, but requires PHP 8.0 or higher.
11+
This library is mainly built for iterating to countable variables, but also works easily with ticking through less structured step-by-step scripts.
12+
13+
Many PHP CLI progress bars have been written in the past, but most haven't been updated in years.
14+
This library uses the latest PHP features such as return types, named arguments and constructor property promotions.
15+
For creating richer, more customizable progress bars, check alternatives such as the progress bar helper included in the [symfony/console](https://symfony.com/doc/current/components/console/helpers/progressbar.html) package.
16+
17+
## Requirements
18+
- PHP 8.0 or higher
19+
20+
## Installation
21+
22+
Install the library into your project with Composer.
23+
24+
```
25+
composer require nickbeen/php-cli-progress-bar
26+
```
27+
28+
## Usage
29+
30+
With this library you can display a progress bar in a PHP CLI script to indicate the script is doing its work and how far it has progressed.
31+
All you need to do is start displaying the progress bar, tick through the steps the script goes through and finish the display of the progress bar.
32+
33+
```
34+
1/100 [#...........................] 1% (00:00:16)
35+
```
36+
37+
```
38+
64/100 [##################..........] 64% (00:00:07)
39+
```
40+
41+
```
42+
100/100 [############################] 100%
43+
```
44+
45+
### Manually progressing
46+
47+
It is possible to tick through the steps of your scripts manually when the steps in your script cannot be looped.
48+
Each tick adds one progression, but you can override the progression made by including an integer in `tick()`.
49+
50+
You do need to set `maxProgress` for the progress bar to display the correct numbers by including it in the constructor.
51+
If you don't know the maxProgress during initialization, you can set it later with the `setMaxProgress()` method.
52+
53+
```php
54+
$progressBar = new \NickBeen\ProgressBar\ProgressBar(maxProgress: 62);
55+
$progressBar->start();
56+
57+
doSomething();
58+
$progressBar->tick();
59+
60+
doSomethingElse();
61+
$progressBar->tick();
62+
63+
doSixtyTasks();
64+
$progressBar->tick(60);
65+
66+
$progressBar->finish();
67+
```
68+
69+
If you have a little more structure in your step-by-step code, you can easily place `tick()` in a for loop.
70+
There is however a more convenient method when dealing with e.g. arrays.
71+
72+
### Iterating through arrays or traversable instances
73+
74+
This class method works with anything of the pseudo-type [iterable](https://www.php.net/manual/en/language.types.iterable.php) which includes any array or any instance of [Traversable](https://www.php.net/manual/en/class.traversable.php).
75+
The `iterate()` method automatically handles starting the progress bar, managing ticking through the iteration and finally finish displaying the progress bar.
76+
77+
```php
78+
$array = [
79+
1 => 'A',
80+
2 => 'B',
81+
3 => 'C',
82+
];
83+
84+
$progressBar = new \NickBeen\ProgressBar\ProgressBar();
85+
86+
foreach ($progressBar->iterate($array as $key => $value);) {
87+
echo "$key: $value" . PHP_EOL;
88+
}
89+
```
90+
91+
### Interact with the progress bar
92+
93+
It is possible to interact with the progress bar during its run.
94+
You can retrieve the estimated time to finish, the progress it has made, the maximum progress that has been set and the amount of completion in percentage.
95+
You can use this information e.g. for notifications or other tasks in the background.
96+
97+
```php
98+
foreach ($progressBar->iterate($array);) {
99+
// Some custom notification
100+
sendToDiscord($progressBar->getEstimatedTime());
101+
102+
// Some custom task application
103+
syncWithCloud($progressBar->getPercentage())
104+
105+
// Some other custom application
106+
sendToRaspberryPiDisplay($progressBar->getProgress(), $progressBar->getMaxProgress())
107+
}
108+
```
109+
110+
## License
111+
112+
This library is licensed under the MIT License (MIT). See the [LICENSE](LICENSE.md) for more details.

0 commit comments

Comments
 (0)