libprogress.php
author Dan
Sun, 07 Dec 2008 17:45:50 -0500
changeset 32 236fd4f65752
parent 22 04c2f743c6ec
permissions -rw-r--r--
Added support for bold, /me, and of course, colors.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
22
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
     1
<?php
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
     2
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
     3
/**
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
     4
 * Class for drawing progress bars in a vt100 console.
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
     5
 * @author Dan Fuhry
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
     6
 * @license Public domain
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
     7
 */
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
     8
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
     9
class ProgressBar
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
    10
{
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
    11
  /**
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
    12
   * Shell escape character.
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
    13
   * @const string
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
    14
   */
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
    15
  
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
    16
  const SHELL_ESCAPE = "\x1B";
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
    17
  
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
    18
  /**
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
    19
   * Carriage return (0x0D)
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
    20
   * @const string
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
    21
   */
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
    22
  
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
    23
  const CARRIAGE_RETURN = "\r";
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
    24
  
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
    25
  /**
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
    26
   * Colors of the foreground, background, foreground text, and background text, respectively
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
    27
   * @var int
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
    28
   * @var int
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
    29
   * @var int
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
    30
   * @var int
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
    31
   */
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
    32
  
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
    33
  private $color_bar, $color_empty, $color_text, $color_emptytext;
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
    34
  
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
    35
  /**
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
    36
   * Text to the left of the bar.
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
    37
   * @var string
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
    38
   */
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
    39
  
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
    40
  private $bar_left;
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
    41
  
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
    42
  /**
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
    43
   * Text to the right of the bar.
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
    44
   * @var string
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
    45
   */
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
    46
  
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
    47
  private $bar_right;
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
    48
  
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
    49
  /**
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
    50
   * Text in the middle of the bar.
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
    51
   * @var string
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
    52
   */
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
    53
  
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
    54
  private $bar_text;
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
    55
  
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
    56
  /**
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
    57
   * The current location of the bar in %.
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
    58
   * @var int
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
    59
   */
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
    60
  
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
    61
  private $bar_pos = 0;
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
    62
  
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
    63
  /**
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
    64
   * Position where the text should start.
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
    65
   * @var int
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
    66
   */
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
    67
  
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
    68
  private $text_pos = 0;
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
    69
  
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
    70
  /**
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
    71
   * Width of the current terminal.
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
    72
   * @var int
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
    73
   */
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
    74
  
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
    75
  private $term_width = 80;
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
    76
  
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
    77
  /**
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
    78
   * Width of the actual bar.
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
    79
   * @var int
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
    80
   */
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
    81
  
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
    82
  private $bar_width = 0;
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
    83
  
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
    84
  /**
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
    85
   * State of the bar's color. Used to avoid echoing tons of color codes.
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
    86
   * @var int
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
    87
   */
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
    88
  
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
    89
  private $color_state = 0;
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
    90
  
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
    91
  /**
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
    92
   * Color state constants
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
    93
   * @const int
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
    94
   * @const int
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
    95
   * @const int
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
    96
   * @const int
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
    97
   * @const int
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
    98
   */
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
    99
  
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   100
  const COLOR_STATE_RESET = 0;
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   101
  const COLOR_STATE_FULL_HIDE = 1;
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   102
  const COLOR_STATE_FULL_SHOW = 2;
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   103
  const COLOR_STATE_EMPTY_HIDE = 3;
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   104
  const COLOR_STATE_EMPTY_SHOW = 4;
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   105
  
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   106
  /**
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   107
   * Constructor. All parameters are optional. Color choices are defined in color_to_code.
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   108
   * @param string $bar_left
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   109
   * @param string $bar_right
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   110
   * @param string $bar_text
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   111
   * @param string $color_bar
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   112
   * @param string $color_empty
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   113
   * @param string $color_text
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   114
   * @param string $color_emptytext
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   115
   */
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   116
  
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   117
  public function __construct($bar_left = '[', $bar_right = ']', $bar_text = '', $color_bar = 'red', $color_empty = 'black', $color_text = 'white', $color_emptytext = 'cyan')
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   118
  {
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   119
    $this->bar_left = $bar_left;
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   120
    $this->bar_right = $bar_right;
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   121
    $this->color_bar = $this->color_to_code($color_bar);
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   122
    $this->color_empty = $this->color_to_code($color_empty);
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   123
    $this->color_text = $this->color_to_code($color_text);
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   124
    $this->color_emptytext = $this->color_to_code($color_emptytext);
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   125
    
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   126
    if ( isset($_SERVER['COLUMNS']) )
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   127
    {
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   128
      $this->term_width = intval($_SERVER['COLUMNS']);
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   129
    }
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   130
    $this->bar_width = $this->term_width - strlen($this->bar_left) - strlen($this->bar_right);
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   131
    
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   132
    $this->update_text_quiet($bar_text);
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   133
  }
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   134
  
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   135
  /**
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   136
   * Updates the text on the progress bar and recalculates the position without redrawing.
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   137
   * @param string Text in the bar. If omitted, blanked.
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   138
   */
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   139
  
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   140
  public function update_text_quiet($bar_text = '')
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   141
  {
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   142
    $this->bar_text = strval($bar_text);
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   143
    
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   144
    if ( !empty($this->bar_text) )
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   145
    {
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   146
      $this->text_pos = round(( $this->bar_width / 2 ) - ( strlen($this->bar_text) / 2 ));
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   147
    }
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   148
  }
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   149
  
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   150
  /**
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   151
   * Updates the text on the progress bar, recalculates the position, and redraws.
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   152
   * @param string Text in the bar. If omitted, blanked.
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   153
   */
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   154
  
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   155
  function update_text($bar_text = '')
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   156
  {
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   157
    $this->update_text_quiet($bar_text);
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   158
    $this->set($this->bar_pos);
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   159
  }
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   160
  
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   161
  /**
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   162
   * Starts output of the bar.
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   163
   */
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   164
  
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   165
  function start()
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   166
  {
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   167
    echo self::CARRIAGE_RETURN;
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   168
    echo $this->bar_left;
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   169
  }
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   170
  
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   171
  /**
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   172
   * Closes the bar.
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   173
   */
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   174
  
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   175
  function end()
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   176
  {
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   177
    $this->set($this->bar_pos, $this->bar_width);
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   178
    echo "\n";
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   179
  }
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   180
  
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   181
  /**
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   182
   * Sets the position of the bar.
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   183
   * @param int Position in %. If a second parameter is set, this is treated as a numerator with the second parameter being the denominator and that is used to calculate position.
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   184
   * @param int Optional. Total number of units to allow fraction usage instead of percentage.
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   185
   */
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   186
  
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   187
  function set($pos, $max = 100)
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   188
  {
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   189
    // if our pos is higher than 100%, reduce it
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   190
    if ( $pos > $max )
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   191
      $pos = $max;
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   192
    
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   193
    // arithmetic one-liner
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   194
    // this is where we should stop showing the "full" color and instead use "empty"
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   195
    $bar_pos = round($this->bar_width * ( $pos / $max ));
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   196
    $this->bar_pos = 100 * ( $pos / $max );
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   197
    
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   198
    // reset the cursor
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   199
    echo self::CARRIAGE_RETURN . $this->bar_left;
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   200
    
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   201
    // print everything out
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   202
    for ( $i = 0; $i < $this->bar_width; $i++ )
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   203
    {
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   204
      $char = ' ';
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   205
      $hide = true;
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   206
      if ( !empty($this->bar_text) )
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   207
      {
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   208
        // we have some text to display in the middle; see where we are.
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   209
        $show_text = ( $i >= $this->text_pos && $i < ( $this->text_pos + strlen($this->bar_text) ) );
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   210
        if ( $show_text )
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   211
        {
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   212
          $char = substr($this->bar_text, $i - $this->text_pos, 1);
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   213
          if ( strlen($char) < 1 )
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   214
            $char = ' ';
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   215
          else
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   216
            $hide = false;
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   217
        }
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   218
      }
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   219
      // determine color
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   220
      if ( $i > $bar_pos )
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   221
      {
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   222
        $hide ? $this->set_color_empty_hide() : $this->set_color_empty_show();
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   223
      }
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   224
      else
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   225
      {
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   226
        $hide ? $this->set_color_full_hide() : $this->set_color_full_show();
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   227
      }
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   228
      echo $char;
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   229
    }
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   230
    $this->set_color_reset();
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   231
    echo $this->bar_right;
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   232
  }
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   233
  
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   234
  #
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   235
  # PRIVATE METHODS
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   236
  #
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   237
  
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   238
  function set_color_full_hide()
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   239
  {
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   240
    if ( $this->color_state == self::COLOR_STATE_FULL_HIDE )
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   241
      return;
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   242
    $this->color_state = self::COLOR_STATE_FULL_HIDE;
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   243
    
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   244
    $fgcolor = 30 + $this->color_bar;
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   245
    $bgcolor = $fgcolor + 10;
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   246
    echo self::SHELL_ESCAPE . "[0;{$fgcolor};{$bgcolor};8m";
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   247
  }
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   248
  
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   249
  function set_color_full_show()
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   250
  {
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   251
    if ( $this->color_state == self::COLOR_STATE_FULL_SHOW )
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   252
      return;
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   253
    $this->color_state = self::COLOR_STATE_FULL_SHOW;
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   254
    
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   255
    $fgcolor = 30 + $this->color_text;
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   256
    $bgcolor = 40 + $this->color_bar;
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   257
    echo self::SHELL_ESCAPE . "[0;1;{$fgcolor};{$bgcolor}m";
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   258
  }
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   259
  
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   260
  function set_color_empty_hide()
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   261
  {
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   262
    if ( $this->color_state == self::COLOR_STATE_EMPTY_HIDE )
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   263
      return;
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   264
    $this->color_state = self::COLOR_STATE_EMPTY_HIDE;
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   265
    
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   266
    $fgcolor = 30 + $this->color_empty;
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   267
    $bgcolor = $fgcolor + 10;
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   268
    echo self::SHELL_ESCAPE . "[0;{$fgcolor};{$bgcolor};8m";
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   269
  }
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   270
  
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   271
  function set_color_empty_show()
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   272
  {
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   273
    if ( $this->color_state == self::COLOR_STATE_EMPTY_SHOW )
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   274
      return;
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   275
    $this->color_state = self::COLOR_STATE_EMPTY_SHOW;
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   276
    
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   277
    $fgcolor = 30 + $this->color_emptytext;
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   278
    $bgcolor = 40 + $this->color_empty;
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   279
    echo self::SHELL_ESCAPE . "[0;1;{$fgcolor};{$bgcolor}m";
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   280
  }
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   281
  
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   282
  function set_color_reset()
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   283
  {
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   284
    if ( $this->color_state == self::COLOR_STATE_RESET )
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   285
      return;
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   286
    $this->color_state = self::COLOR_STATE_RESET;
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   287
    
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   288
    echo self::SHELL_ESCAPE . "[0m";
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   289
  }
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   290
  
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   291
  /**
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   292
   * Converts a color name to an ASCII color code. Valid color names are black, red, green, yellow, blue, magenta, cyan, and white.
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   293
   * @param string Color name
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   294
   * @return int
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   295
   */
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   296
  
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   297
  private function color_to_code($color)
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   298
  {
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   299
    static $colors = array(
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   300
      'black' => 0,
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   301
      'red' => 1,
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   302
      'green' => 2,
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   303
      'yellow' => 3,
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   304
      'blue' => 4,
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   305
      'magenta' => 5,
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   306
      'cyan' => 6,
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   307
      'white' => 7
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   308
    );
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   309
    return ( isset($colors[$color]) ) ? $colors[$color] : $colors['white'];
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   310
  }
04c2f743c6ec Added script for importing stats databases
Dan
parents:
diff changeset
   311
}