scanner/ncurses/ncurses.progressbar.php

82 lines
1.9 KiB
PHP
Raw Permalink Normal View History

<?php
/**
* Progress Bar
*
* @package Nc
*/
class NcProgressBar extends NcWindow {
/**
* @var float Maximum value
*/
var $max;
/**
* @var float Current value
*/
var $value;
/**
* @var boolean Show percentage
*/
var $show_pct;
/**
* Constructor
*
* Creates a new progress bar widget
*
* @param NcWindow $parent Parent window
* @param integer $width Width
* @param integer $y Row
* @param integer $x Column
* @param float $max Maximum value
* @param boolean $show_pct Display the percentage alongside the progress bar
*/
public function __construct( &$parent, $width, $y, $x, $max = 100, $show_pct = true ) {
parent::__construct( $parent, 1, $width, $y, $x, false );
$this->enabled = false; // Cannot accept focus
$this->max = $max;
$this->show_pct = $show_pct;
$this->pos( 0 );
}
/**
* Set or get the current value
*
* @param float $value New value
* @return float Current value
*/
public function pos( $value = false ) {
if( $value === false ) {
return $this->value;
} else {
$value = $value > $this->max ? $this->max : $value;
$this->value = $value;
$this->write( 0, 0, '[', false );
$width = $this->width - ( $this->show_pct ? 7 : 2 );
$width = $width >= 0 ? $width : 0;
$complete = floor( $width * $value / $this->max );
$this->write( 0, 1, str_repeat( '#', $complete ), false );
$this->write( 0, 1 + $complete, str_repeat( ' ', $width - $complete ), false );
$this->write( 0, $width + 1, ']', false );
if( $this->show_pct ) {
$this->write( 0, $width + 3, sprintf( '%3d%%', floor( $value / $this->max * 100 ) ), false );
}
ncurses_update_panels();
ncurses_doupdate();
}
}
/**
* Get or set maximum value
*
* @param float $value New maximum
* @return float Current maximum
*/
public function max( $value = false ) {
if( $value === false ) {
return $this->max;
} else {
$this->max = $value > 0 ? $value : $this->max;
$this->pos( $this->value );
}
}
}
?>