Code Monkey home page Code Monkey logo

phpterminalprogressbar's Introduction

Flexible ascii progress bar.

Installation

Include the PHPTerminalProgressBar class

Usage

First we create a ProgressBar, giving it a format string as well as the total, telling the progress bar when it will be considered complete. After that all we need to do is tick() appropriately.

// example/basic.php
include('PHPTerminalProgressBar.php');

$pg = new PHPTerminalProgressBar(1000);

for ($i = 0; $i < 1000; $i++) {
	usleep(10000);
	$pg->tick();
}

You can also use update(amount) to set the current tick value instead of ticking each time there is an increment:

// example/update.php
include('PHPTerminalProgressBar.php');

$pg = new PHPTerminalProgressBar(1000);

for ($i = 0; $i < 1000; $i++) {
	usleep(10000);
	$pg->update($i);
}

Options

These are properties in the object you can read/set:

  • symbolComplete completion character defaulting to "="
  • symbolIncomplete incomplete character defaulting to " "
  • throttle minimum time between updates in seconds defaulting to 0.016
  • current current tick
  • total same value passed in when initialising
  • percent (read only) current percentage completion
  • eta (read only) estimate seconds until completion
  • rate (read only) number of ticks per second
  • elapsed (read only) seconds since initialisation

Tokens

These are tokens you can use in the format of your progress bar.

  • :bar the progress bar itself
  • :current current tick number
  • :total total ticks
  • :elapsed time elapsed in seconds
  • :percent completion percentage
  • :eta estimated completion time in seconds
  • :rate rate of ticks per second

Format example

// example/format.php
// Full options
new PHPTerminalProgressBar(10, "Progress: [:bar] - :current/:total - :percent% - Elapsed::elapseds - ETA::etas - Rate::rate/s");
// example/format_percent.php
// Just percentage plus the bar
new PHPTerminalProgressBar(10, ":bar :percent%");
// example/format_no_bar.php
// You don't even have to have a bar
new PHPTerminalProgressBar(10, "Look mum, no bar! :current/:total - :percent% - Elapsed::elapseds - ETA::etas - Rate::rate/s");

Interrupt example

To display a message during progress bar execution, use interrupt()

// example/interrupt.php
$pg = new PHPTerminalProgressBar(1000);

for ($i = 0; $i < 1000; $i++) {
	usleep(10000);
	if ($i % 100 == 0) {
		// Interupt every 100th tick
		$pg->interupt($i);
	}
	$pg->tick();
}

Symbols example

To change the symbols used on the progress bar

// example/symbols.php
$pg = new PHPTerminalProgressBar(1000);
$pg->symbolComplete = "#";
$pg->symbolIncomplete = "-";

Throttle example

If you are ticking several hundred or thousands of times per second, the throttle setting will be prevent the progress bar from slowing down execution time too much, however, 16ms is quite optimistic, so you may wish to increase it on slower machines.

// example/throttle.php
$pg = new PHPTerminalProgressBar(1000);
// Set a 100 millisecond threshold
$pg->throttle = 0.1;

License

See LICENSE

phpterminalprogressbar's People

Contributors

macroman avatar marios88 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.