A modest collection of PHP libraries used at SparkFun.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

81 lines
2.0 KiB

12 years ago
12 years ago
  1. <?php
  2. namespace Sparklib;
  3. class NcursesProgressDialog {
  4. private $limit;
  5. // Middle window resource.
  6. private $window;
  7. // Position of the progress bar.
  8. private $bar_x = 1;
  9. /**
  10. * @param int $limit - the number of tasks to be completed, num db rows or whatever
  11. * @param string $title - the title of the window
  12. */
  13. public function __construct ($limit, $title='New Task')
  14. {
  15. // Fallback for if libncurses is missing. Just do nothing.
  16. if (! function_exists('ncurses_init')) return;
  17. // How many records will we be processing?
  18. $this->limit = $limit;
  19. ncurses_init();
  20. // Border the screen.
  21. ncurses_border(0,0, 0,0, 0,0, 0,0);
  22. // Get the size of the screen.
  23. ncurses_getmaxyx(STDSCR, $lines, $columns);
  24. // Make a window in the middle that's half the width of the screen.
  25. $width = $columns / 2;
  26. $this->window = ncurses_newwin(15, $width, $lines/4, $width/2);
  27. ncurses_wborder($this->window, 0,0, 0,0, 0,0, 0,0);
  28. // Title it.
  29. ncurses_mvwaddstr($this->window, 0, 5, $title);
  30. // Show progress bar boundaries.
  31. ncurses_mvwaddstr($this->window, 12, 1, '|');
  32. ncurses_mvwaddstr($this->window, 12, $width-2, '|');
  33. // Figure out what one character is worth based on the above.
  34. $this->unit = round($limit / ($width - 3));
  35. ncurses_refresh();
  36. }
  37. /**
  38. * @param int $place - number of the current task
  39. */
  40. public function update ($place)
  41. {
  42. if (! function_exists('ncurses_init')) return;
  43. ncurses_mvwaddstr($this->window, 5, 5, 'Processing record '.$place.' of '.$this->limit);
  44. $percent = (round($place / $this->limit, 2) * 100) . '%';
  45. ncurses_mvwaddstr($this->window, 10, 5, $percent);
  46. if ($place % $this->unit == 0) {
  47. $this->bar_x += 1;
  48. ncurses_mvwaddstr($this->window, 12, $this->bar_x, '=>');
  49. }
  50. ncurses_wrefresh($this->window);
  51. if ($place >= $this->limit) ncurses_end();
  52. }
  53. public function __destruct ()
  54. {
  55. if (! ncurses_isendwin()) {
  56. ncurses_end();
  57. }
  58. }
  59. }