Navigation Menu

Skip to content

Commit

Permalink
new file
Browse files Browse the repository at this point in the history
  • Loading branch information
Casey Dentinger committed Feb 2, 2012
1 parent bc964b7 commit f4b86af
Showing 1 changed file with 74 additions and 0 deletions.
74 changes: 74 additions & 0 deletions NcursesProgressDialog.php
@@ -0,0 +1,74 @@
<?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();
}

}

0 comments on commit f4b86af

Please sign in to comment.