Completed
Push — development ( 55d5bc...e768a0 )
by Alexander
03:54
created

CliProgressBar   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 126
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 8
dl 0
loc 126
ccs 40
cts 40
cp 1
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A update() 0 12 2
A getMemString() 0 6 1
A __construct() 0 8 1
A calcParams() 0 10 1
A getStatusString() 0 11 1
A getPosition() 0 5 2
1
<?php
2
/**
3
 * Progress bar for console applications
4
 *
5
 * @file      CliProgressBar.php
6
 *
7
 * PHP version 7.0+
8
 *
9
 * @author    Alexander Yancharuk <alex at itvault dot info>
10
 * @copyright © 2012-2018 Alexander Yancharuk
11
 * @date      Сбт Фев 16 20:07:56 2013
12
 * @license   The BSD 3-Clause License
13
 *            <https://tldrlegal.com/license/bsd-3-clause-license-(revised)>.
14
 */
15
16
namespace Veles\Tools;
17
18
use Veles\Validators\ByteValidator;
19
20
/**
21
 * Class CliProgressBar
22
 *
23
 * @author  Alexander Yancharuk <alex at itvault dot info>
24
 */
25
class CliProgressBar
26
{
27
	/** @var float Progress bar percent */
28
	protected $pb_percent;
29
	/** @var float Current progress bar percent  */
30
	protected $percent;
31
	/** @var float Progress bar initialization time  */
32
	protected $start_time;
33
	/** @var int Progress bar final value */
34
	protected $final_value;
35
	/** @var float Time when update calculation started */
36
	protected $curr_time;
37
	/** @var float Time when last update calculation finished */
38
	protected $last_update_time = 0.0;
39
	/** @var float Time between finishing last update and starting current update */
40
	protected $clean_process_time = 0.0;
41
	/** @var string Function for memory usage */
42
	protected $mem_usage_func = 'memory_get_usage';
43
	/** @var string Function for max memory usage */
44
	protected $mem_peak_func = 'memory_get_peak_usage';
45
	/** @var int Progress bar width */
46
	protected $width;
47
48
	/**
49
	 * Constructor
50
	 *
51
	 * @param int $final Final result quantity
52
	 * @param int $width ProgressBar width
53
	 */
54 9
	public function __construct($final, $width = 60)
55
	{
56 9
		$this->final_value = max($final, 1);
57 9
		$this->width       = $width;
58 9
		$this->pb_percent  = $width / 100;
59 9
		$this->percent     = $this->final_value / 100;
60 9
		$this->start_time  = microtime(true);
61 9
		$this->last_update_time = $this->start_time;
62 9
	}
63
64
	/**
65
	 * Progress bar update
66
	 *
67
	 * @param int $current Current process quantity
68
	 */
69 9
	public function update($current)
70
	{
71 9
		$this->curr_time = microtime(true);
72 9
		$this->clean_process_time += $this->curr_time - $this->last_update_time;
73
74 9
		list($end, $bar, $space_len, $status) = $this->calcParams($current);
75
76 9
		echo ($space_len > 0)
77 6
			? "\033[?25l[$bar>\033[{$space_len}C]$status$end"
78 3
			: "[$bar>]$status$end\033[?25h";
79
80 9
		$this->last_update_time = microtime(true);
81 9
	}
82
83
	/**
84
	 * Get string with statistic
85
	 *
86
	 * This method is public only for testing purposes. Logic with calculation
87
	 * must be moved into builder or class-calculator.
88
	 *
89
	 * @param int $current Current process quantity
90
	 *
91
	 * @return string
92
	 */
93 3
	public function getStatusString($current)
94
	{
95 3
		$current   = max($current, 1);
96 3
		$avg_speed = round($current / $this->clean_process_time);
97 3
		$estimated = number_format(
98 3
			($this->final_value - $current) *
99 3
			($this->curr_time - $this->start_time) /
100 3
			$current, 1
101
		);
102
103 3
		return " $current u | $avg_speed u/s | Est: $estimated s";
104
	}
105
106
	/**
107
	 * Get string with memory statistic
108
	 *
109
	 * @return string
110
	 */
111 3
	public function getMemString()
112
	{
113 3
		$mem = ByteValidator::format(call_user_func($this->mem_usage_func));
114 3
		$max_mem = ByteValidator::format(call_user_func($this->mem_peak_func));
115
116 3
		return " | Mem: $mem | Max: $max_mem";
117
	}
118
119
	/**
120
	 * Calculate bar-string params
121
	 *
122
	 * @param int $current Current process quantity
123
	 *
124
	 * @return array
125
	 */
126 9
	protected function calcParams($current)
127
	{
128 9
		$done = number_format($current / $this->percent, 2);
129 9
		list($position, $end) = $this->getPosition($done);
130
131
		return [
132 9
			$end,
133 9
			str_repeat('=', $position),
134 9
			$this->width - $position,
135 9
			$this->getStatusString($current) . $this->getMemString()
136
		];
137
	}
138
139
	/**
140
	 * Returns cursor position and last string-part for bar
141
	 *
142
	 * @param $done
143
	 *
144
	 * @return array
145
	 */
146 3
	protected function getPosition($done)
147
	{
148 3
		return ($done < 100)
149 3
			? [(int) floor($this->pb_percent * $done), "\033[K\r"]
150 3
			: [$this->width, "\033[K" . PHP_EOL];
151
	}
152
}
153