/
NcursesProgressDialog.php
81 lines (59 loc) · 1.98 KB
/
NcursesProgressDialog.php
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
<?php
namespace Sparklib;
class NcursesProgressDialog {
private $limit;
// Middle window resource.
private $window;
// Position of the progress bar.
private $bar_x = 1;
/**
* @param int $limit - the number of tasks to be completed, num db rows or whatever
* @param string $title - the title of the window
*/
public function __construct ($limit, $title='New Task')
{
// Fallback for if libncurses is missing. Just do nothing.
if (! function_exists('ncurses_init')) return;
// How many records will we be processing?
$this->limit = $limit;
ncurses_init();
// Border the screen.
ncurses_border(0,0, 0,0, 0,0, 0,0);
// Get the size of the screen.
ncurses_getmaxyx(STDSCR, $lines, $columns);
// Make a window in the middle that's half the width of the screen.
$width = $columns / 2;
$this->window = ncurses_newwin(15, $width, $lines/4, $width/2);
ncurses_wborder($this->window, 0,0, 0,0, 0,0, 0,0);
// Title it.
ncurses_mvwaddstr($this->window, 0, 5, $title);
// Show progress bar boundaries.
ncurses_mvwaddstr($this->window, 12, 1, '|');
ncurses_mvwaddstr($this->window, 12, $width-2, '|');
// Figure out what one character is worth based on the above.
$this->unit = round($limit / ($width - 3));
ncurses_refresh();
}
/**
* @param int $place - number of the current task
*/
public function update ($place)
{
if (! function_exists('ncurses_init')) return;
ncurses_mvwaddstr($this->window, 5, 5, 'Processing record '.$place.' of '.$this->limit);
$percent = (round($place / $this->limit, 2) * 100) . '%';
ncurses_mvwaddstr($this->window, 10, 5, $percent);
if ($place % $this->unit == 0) {
$this->bar_x += 1;
ncurses_mvwaddstr($this->window, 12, $this->bar_x, '=>');
}
ncurses_wrefresh($this->window);
if ($place >= $this->limit) ncurses_end();
}
public function __destruct ()
{
if (! ncurses_isendwin()) {
ncurses_end();
}
}
}