-
Notifications
You must be signed in to change notification settings - Fork 115
Expand file tree
/
Copy pathBar.php
More file actions
115 lines (103 loc) · 3.64 KB
/
Bar.php
File metadata and controls
115 lines (103 loc) · 3.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<?php
/**
* PHP Command Line Tools
*
* This source file is subject to the MIT license that is bundled
* with this package in the file LICENSE.
*
* @author James Logsdon <dwarf@girsbrain.org>
* @copyright 2010 James Logsdom (http://girsbrain.org)
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
*/
namespace cli\progress;
use cli;
use cli\Notify;
use cli\Progress;
use cli\Shell;
use cli\Streams;
/**
* Displays a progress bar spanning the entire shell.
*
* Basic format:
*
* ^MSG PER% [======================= ] 00:00 / 00:00$
*/
class Bar extends Progress {
protected $_bars = '=>';
protected $_formatMessage = '{:msg} {:percent}% [';
protected $_formatTiming = '] {:elapsed} / {:estimated}';
protected $_format = '{:msg}{:bar}{:timing}';
/**
* Instantiates a Progress Bar.
*
* @param string $msg The text to display next to the Notifier.
* @param int $total The total number of ticks we will be performing.
* @param int $interval The interval in milliseconds between updates.
* @param string|null $formatMessage Optional format string for the message portion.
* @param string|null $formatTiming Optional format string for the timing portion.
* @param string|null $format Optional format string for the overall display.
*/
public function __construct($msg, $total, $interval = 100, $formatMessage = null, $formatTiming = null, $format = null) {
parent::__construct($msg, $total, $interval);
if ($formatMessage !== null) {
$this->_formatMessage = $formatMessage;
}
if ($formatTiming !== null) {
$this->_formatTiming = $formatTiming;
}
if ($format !== null) {
$this->_format = $format;
}
}
/**
* Prints the progress bar to the screen with percent complete, elapsed time
* and estimated total time.
*
* @param boolean $finish `true` if this was called from
* `cli\Notify::finish()`, `false` otherwise.
* @see cli\out()
* @see cli\Notify::formatTime()
* @see cli\Notify::elapsed()
* @see cli\Progress::estimated();
* @see cli\Progress::percent()
* @see cli\Shell::columns()
*/
public function display($finish = false) {
$_percent = $this->percent();
$percent = str_pad(floor($_percent * 100), 3);
$msg = $this->_message;
$current = $this->current();
$total = $this->total();
$msg = Streams::render($this->_formatMessage, compact('msg', 'percent', 'current', 'total'));
$estimated = $this->formatTime($this->estimated());
$elapsed = str_pad($this->formatTime($this->elapsed()), strlen($estimated));
$timing = Streams::render($this->_formatTiming, compact('elapsed', 'estimated', 'current', 'total', 'percent'));
$size = Shell::columns();
// On Windows, the cursor wraps to the next line if the output fills the entire width.
if ( Shell::is_windows() ) {
$size -= 1;
}
$size -= strlen($msg . $timing);
if ( $size < 0 ) {
$size = 0;
}
$bar = str_repeat($this->_bars[0], floor($_percent * $size)) . $this->_bars[1];
// substr is needed to trim off the bar cap at 100%
$bar = substr(str_pad($bar, $size, ' '), 0, $size);
Streams::out($this->_format, compact('msg', 'bar', 'timing', 'current', 'total', 'percent'));
}
/**
* This method augments the base definition from cli\Notify to optionally
* allow passing a new message.
*
* @param int $increment The amount to increment by.
* @param string $msg The text to display next to the Notifier. (optional)
* @see cli\Notify::tick()
*/
public function tick($increment = 1, $msg = null) {
if ($msg) {
$this->_message = $msg;
}
Notify::tick($increment);
}
}