Browse Source

Fixed formatting

tags/2.0.0-beta
Antonio Ramirez 12 years ago
parent
commit
a20bd45645
  1. 243
      framework/helpers/base/Console.php

243
framework/helpers/base/Console.php

@ -55,9 +55,9 @@ class Console
* If the cursor is already at the edge of the screen, this has no effect. * If the cursor is already at the edge of the screen, this has no effect.
* @param integer $rows number of rows the cursor should be moved up * @param integer $rows number of rows the cursor should be moved up
*/ */
public static function moveCursorUp($rows=1) public static function moveCursorUp($rows = 1)
{ {
echo "\033[" . (int) $rows . 'A'; echo "\033[" . (int)$rows . 'A';
} }
/** /**
@ -65,9 +65,9 @@ class Console
* If the cursor is already at the edge of the screen, this has no effect. * If the cursor is already at the edge of the screen, this has no effect.
* @param integer $rows number of rows the cursor should be moved down * @param integer $rows number of rows the cursor should be moved down
*/ */
public static function moveCursorDown($rows=1) public static function moveCursorDown($rows = 1)
{ {
echo "\033[" . (int) $rows . 'B'; echo "\033[" . (int)$rows . 'B';
} }
/** /**
@ -75,9 +75,9 @@ class Console
* If the cursor is already at the edge of the screen, this has no effect. * If the cursor is already at the edge of the screen, this has no effect.
* @param integer $steps number of steps the cursor should be moved forward * @param integer $steps number of steps the cursor should be moved forward
*/ */
public static function moveCursorForward($steps=1) public static function moveCursorForward($steps = 1)
{ {
echo "\033[" . (int) $steps . 'C'; echo "\033[" . (int)$steps . 'C';
} }
/** /**
@ -85,27 +85,27 @@ class Console
* If the cursor is already at the edge of the screen, this has no effect. * If the cursor is already at the edge of the screen, this has no effect.
* @param integer $steps number of steps the cursor should be moved backward * @param integer $steps number of steps the cursor should be moved backward
*/ */
public static function moveCursorBackward($steps=1) public static function moveCursorBackward($steps = 1)
{ {
echo "\033[" . (int) $steps . 'D'; echo "\033[" . (int)$steps . 'D';
} }
/** /**
* Moves the terminal cursor to the beginning of the next line by sending ANSI control code CNL to the terminal. * Moves the terminal cursor to the beginning of the next line by sending ANSI control code CNL to the terminal.
* @param integer $lines number of lines the cursor should be moved down * @param integer $lines number of lines the cursor should be moved down
*/ */
public static function moveCursorNextLine($lines=1) public static function moveCursorNextLine($lines = 1)
{ {
echo "\033[" . (int) $lines . 'E'; echo "\033[" . (int)$lines . 'E';
} }
/** /**
* Moves the terminal cursor to the beginning of the previous line by sending ANSI control code CPL to the terminal. * Moves the terminal cursor to the beginning of the previous line by sending ANSI control code CPL to the terminal.
* @param integer $lines number of lines the cursor should be moved up * @param integer $lines number of lines the cursor should be moved up
*/ */
public static function moveCursorPrevLine($lines=1) public static function moveCursorPrevLine($lines = 1)
{ {
echo "\033[" . (int) $lines . 'F'; echo "\033[" . (int)$lines . 'F';
} }
/** /**
@ -113,12 +113,12 @@ class Console
* @param integer $column 1-based column number, 1 is the left edge of the screen. * @param integer $column 1-based column number, 1 is the left edge of the screen.
* @param integer|null $row 1-based row number, 1 is the top edge of the screen. if not set, will move cursor only in current line. * @param integer|null $row 1-based row number, 1 is the top edge of the screen. if not set, will move cursor only in current line.
*/ */
public static function moveCursorTo($column, $row=null) public static function moveCursorTo($column, $row = null)
{ {
if ($row === null) { if ($row === null) {
echo "\033[" . (int) $column . 'G'; echo "\033[" . (int)$column . 'G';
} else { } else {
echo "\033[" . (int) $row . ';' . (int) $column . 'H'; echo "\033[" . (int)$row . ';' . (int)$column . 'H';
} }
} }
@ -127,9 +127,9 @@ class Console
* New lines are added at the bottom. This is not supported by ANSI.SYS used in windows. * New lines are added at the bottom. This is not supported by ANSI.SYS used in windows.
* @param int $lines number of lines to scroll up * @param int $lines number of lines to scroll up
*/ */
public static function scrollUp($lines=1) public static function scrollUp($lines = 1)
{ {
echo "\033[".(int)$lines."S"; echo "\033[" . (int)$lines . "S";
} }
/** /**
@ -137,9 +137,9 @@ class Console
* New lines are added at the top. This is not supported by ANSI.SYS used in windows. * New lines are added at the top. This is not supported by ANSI.SYS used in windows.
* @param int $lines number of lines to scroll down * @param int $lines number of lines to scroll down
*/ */
public static function scrollDown($lines=1) public static function scrollDown($lines = 1)
{ {
echo "\033[".(int)$lines."T"; echo "\033[" . (int)$lines . "T";
} }
/** /**
@ -255,18 +255,18 @@ class Console
$args = func_get_args(); $args = func_get_args();
array_shift($args); array_shift($args);
$code = implode(';', $args); $code = implode(';', $args);
return "\033[0m" . ($code !== '' ? "\033[" . $code . "m" : '') . $string."\033[0m"; return "\033[0m" . ($code !== '' ? "\033[" . $code . "m" : '') . $string . "\033[0m";
} }
//const COLOR_XTERM256 = 38;// http://en.wikipedia.org/wiki/Talk:ANSI_escape_code#xterm-256colors //const COLOR_XTERM256 = 38;// http://en.wikipedia.org/wiki/Talk:ANSI_escape_code#xterm-256colors
public static function xterm256ColorFg($i) // TODO naming! public static function xterm256ColorFg($i) // TODO naming!
{ {
return '38;5;'.$i; return '38;5;' . $i;
} }
public static function xterm256ColorBg($i) // TODO naming! public static function xterm256ColorBg($i) // TODO naming!
{ {
return '48;5;'.$i; return '48;5;' . $i;
} }
/** /**
@ -277,7 +277,7 @@ class Console
public static function getScreenSize() public static function getScreenSize()
{ {
// TODO implement // TODO implement
return array(150,50); return array(150, 50);
} }
/** /**
@ -304,34 +304,78 @@ class Console
public static function ansiToHtml($string) public static function ansiToHtml($string)
{ {
$tags = 0; $tags = 0;
return preg_replace_callback('/\033\[[\d;]+m/', function($ansi) use (&$tags) { return preg_replace_callback(
'/\033\[[\d;]+m/',
function ($ansi) use (&$tags) {
$styleA = array(); $styleA = array();
foreach(explode(';', $ansi) as $controlCode) foreach (explode(';', $ansi) as $controlCode) {
{ switch ($controlCode) {
switch($controlCode) case static::FG_BLACK:
{ $style = array('color' => '#000000');
case static::FG_BLACK: $style = array('color' => '#000000'); break; break;
case static::FG_BLUE: $style = array('color' => '#000078'); break; case static::FG_BLUE:
case static::FG_CYAN: $style = array('color' => '#007878'); break; $style = array('color' => '#000078');
case static::FG_GREEN: $style = array('color' => '#007800'); break; break;
case static::FG_GREY: $style = array('color' => '#787878'); break; case static::FG_CYAN:
case static::FG_PURPLE: $style = array('color' => '#780078'); break; $style = array('color' => '#007878');
case static::FG_RED: $style = array('color' => '#780000'); break; break;
case static::FG_YELLOW: $style = array('color' => '#787800'); break; case static::FG_GREEN:
case static::BG_BLACK: $style = array('background-color' => '#000000'); break; $style = array('color' => '#007800');
case static::BG_BLUE: $style = array('background-color' => '#000078'); break; break;
case static::BG_CYAN: $style = array('background-color' => '#007878'); break; case static::FG_GREY:
case static::BG_GREEN: $style = array('background-color' => '#007800'); break; $style = array('color' => '#787878');
case static::BG_GREY: $style = array('background-color' => '#787878'); break; break;
case static::BG_PURPLE: $style = array('background-color' => '#780078'); break; case static::FG_PURPLE:
case static::BG_RED: $style = array('background-color' => '#780000'); break; $style = array('color' => '#780078');
case static::BG_YELLOW: $style = array('background-color' => '#787800'); break; break;
case static::BOLD: $style = array('font-weight' => 'bold'); break; case static::FG_RED:
case static::ITALIC: $style = array('font-style' => 'italic'); break; $style = array('color' => '#780000');
case static::UNDERLINE: $style = array('text-decoration' => array('underline')); break; break;
case static::OVERLINED: $style = array('text-decoration' => array('overline')); break; case static::FG_YELLOW:
case static::CROSSED_OUT:$style = array('text-decoration' => array('line-through')); break; $style = array('color' => '#787800');
case static::BLINK: $style = array('text-decoration' => array('blink')); break; break;
case static::BG_BLACK:
$style = array('background-color' => '#000000');
break;
case static::BG_BLUE:
$style = array('background-color' => '#000078');
break;
case static::BG_CYAN:
$style = array('background-color' => '#007878');
break;
case static::BG_GREEN:
$style = array('background-color' => '#007800');
break;
case static::BG_GREY:
$style = array('background-color' => '#787878');
break;
case static::BG_PURPLE:
$style = array('background-color' => '#780078');
break;
case static::BG_RED:
$style = array('background-color' => '#780000');
break;
case static::BG_YELLOW:
$style = array('background-color' => '#787800');
break;
case static::BOLD:
$style = array('font-weight' => 'bold');
break;
case static::ITALIC:
$style = array('font-style' => 'italic');
break;
case static::UNDERLINE:
$style = array('text-decoration' => array('underline'));
break;
case static::OVERLINED:
$style = array('text-decoration' => array('overline'));
break;
case static::CROSSED_OUT:
$style = array('text-decoration' => array('line-through'));
break;
case static::BLINK:
$style = array('text-decoration' => array('blink'));
break;
case static::NEGATIVE: // ??? case static::NEGATIVE: // ???
case static::CONCEALED: case static::CONCEALED:
case static::ENCIRCLED: case static::ENCIRCLED:
@ -340,7 +384,7 @@ class Console
break; break;
case 0: // ansi reset case 0: // ansi reset
$return = ''; $return = '';
for($n=$tags; $tags>0; $tags--) { for ($n = $tags; $tags > 0; $tags--) {
$return .= '</span>'; $return .= '</span>';
} }
return $return; return $return;
@ -349,15 +393,17 @@ class Console
$styleA = ArrayHelper::merge($styleA, $style); $styleA = ArrayHelper::merge($styleA, $style);
} }
$styleString[] = array(); $styleString[] = array();
foreach($styleA as $name => $content) { foreach ($styleA as $name => $content) {
if ($name === 'text-decoration') { if ($name === 'text-decoration') {
$content = implode(' ', $content); $content = implode(' ', $content);
} }
$styleString[] = $name.':'.$content; $styleString[] = $name . ':' . $content;
} }
$tags++; $tags++;
return '<span' . (!empty($styleString) ? 'style="' . implode(';', $styleString) : '') . '>'; return '<span' . (!empty($styleString) ? 'style="' . implode(';', $styleString) : '') . '>';
}, $string); },
$string
);
} }
/** /**
@ -450,19 +496,19 @@ class Console
*/ */
public static function renderColoredString($string, $colored = true) public static function renderColoredString($string, $colored = true)
{ {
static $conversions = array ( // static so the array doesn't get built static $conversions = array( // static so the array doesn't get built
// everytime // everytime
// %y - yellow, and so on... {{{ // %y - yellow, and so on... {{{
'%y' => array('color' => 'yellow'), '%y' => array('color' => 'yellow'),
'%g' => array('color' => 'green' ), '%g' => array('color' => 'green'),
'%b' => array('color' => 'blue' ), '%b' => array('color' => 'blue'),
'%r' => array('color' => 'red' ), '%r' => array('color' => 'red'),
'%p' => array('color' => 'purple'), '%p' => array('color' => 'purple'),
'%m' => array('color' => 'purple'), '%m' => array('color' => 'purple'),
'%c' => array('color' => 'cyan' ), '%c' => array('color' => 'cyan'),
'%w' => array('color' => 'grey' ), '%w' => array('color' => 'grey'),
'%k' => array('color' => 'black' ), '%k' => array('color' => 'black'),
'%n' => array('color' => 'reset' ), '%n' => array('color' => 'reset'),
'%Y' => array('color' => 'yellow', 'style' => 'light'), '%Y' => array('color' => 'yellow', 'style' => 'light'),
'%G' => array('color' => 'green', 'style' => 'light'), '%G' => array('color' => 'green', 'style' => 'light'),
'%B' => array('color' => 'blue', 'style' => 'light'), '%B' => array('color' => 'blue', 'style' => 'light'),
@ -474,13 +520,13 @@ class Console
'%K' => array('color' => 'black', 'style' => 'light'), '%K' => array('color' => 'black', 'style' => 'light'),
'%N' => array('color' => 'reset', 'style' => 'light'), '%N' => array('color' => 'reset', 'style' => 'light'),
'%3' => array('background' => 'yellow'), '%3' => array('background' => 'yellow'),
'%2' => array('background' => 'green' ), '%2' => array('background' => 'green'),
'%4' => array('background' => 'blue' ), '%4' => array('background' => 'blue'),
'%1' => array('background' => 'red' ), '%1' => array('background' => 'red'),
'%5' => array('background' => 'purple'), '%5' => array('background' => 'purple'),
'%6' => array('background' => 'cyan' ), '%6' => array('background' => 'cyan'),
'%7' => array('background' => 'grey' ), '%7' => array('background' => 'grey'),
'%0' => array('background' => 'black' ), '%0' => array('background' => 'black'),
// Don't use this, I can't stand flashing text // Don't use this, I can't stand flashing text
'%F' => array('style' => 'blink'), '%F' => array('style' => 'blink'),
'%U' => array('style' => 'underline'), '%U' => array('style' => 'underline'),
@ -493,8 +539,11 @@ class Console
if ($colored) { if ($colored) {
$string = str_replace('%%', '% ', $string); $string = str_replace('%%', '% ', $string);
foreach ($conversions as $key => $value) { foreach ($conversions as $key => $value) {
$string = str_replace($key, static::color($value), $string = str_replace(
$string); $key,
static::color($value),
$string
);
} }
$string = str_replace('% ', '%', $string); $string = str_replace('% ', '%', $string);
@ -570,7 +619,7 @@ class Console
* @param mixed $stream * @param mixed $stream
* @return bool true if the stream supports ANSI colors, otherwise false. * @return bool true if the stream supports ANSI colors, otherwise false.
*/ */
public static function streamSupportsAnsiColors( $stream ) public static function streamSupportsAnsiColors($stream)
{ {
return DIRECTORY_SEPARATOR == '\\' return DIRECTORY_SEPARATOR == '\\'
? null !== getenv('ANSICON') ? null !== getenv('ANSICON')
@ -658,7 +707,7 @@ class Console
*/ */
public static function input($prompt = null) public static function input($prompt = null)
{ {
if(isset($prompt)) { if (isset($prompt)) {
static::stdout($prompt); static::stdout($prompt);
} }
return static::stdin(); return static::stdin();
@ -692,13 +741,16 @@ class Console
*/ */
public static function prompt($text, $options = array()) public static function prompt($text, $options = array())
{ {
$options = ArrayHelper::merge($options, array( $options = ArrayHelper::merge(
$options,
array(
'required' => false, 'required' => false,
'default' => null, 'default' => null,
'pattern' => null, 'pattern' => null,
'validator' => null, 'validator' => null,
'error' => 'Invalid input.', 'error' => 'Invalid input.',
)); )
);
$error = null; $error = null;
top: top:
@ -717,7 +769,8 @@ class Console
static::output($options['error']); static::output($options['error']);
goto top; goto top;
} elseif ($options['validator'] && } elseif ($options['validator'] &&
!call_user_func_array($options['validator'], array($input, &$error))) { !call_user_func_array($options['validator'], array($input, &$error))
) {
static::output(isset($error) ? $error : $options['error']); static::output(isset($error) ? $error : $options['error']);
goto top; goto top;
} }
@ -736,7 +789,7 @@ class Console
{ {
top: top:
$input = strtolower(static::input("$prompt [y/n]: ")); $input = strtolower(static::input("$prompt [y/n]: "));
if (!in_array(substr($input,0,1), array('y', 'n'))) { if (!in_array(substr($input, 0, 1), array('y', 'n'))) {
static::output("Please, type 'y' or 'n'"); static::output("Please, type 'y' or 'n'");
goto top; goto top;
} }
@ -763,7 +816,9 @@ class Console
} }
echo " ? - Show help\n"; echo " ? - Show help\n";
goto top; goto top;
} elseif (!in_array($input, array_keys($options))) goto top; } elseif (!in_array($input, array_keys($options))) {
goto top;
}
return $input; return $input;
} }
@ -775,51 +830,51 @@ class Console
* @param int $size the size of the status bar (optional) * @param int $size the size of the status bar (optional)
* @see http://snipplr.com/view/29548/ * @see http://snipplr.com/view/29548/
*/ */
public static function showProgress($done, $total, $size=30) public static function showProgress($done, $total, $size = 30)
{ {
static $start; static $start;
// if we go over our bound, just ignore it // if we go over our bound, just ignore it
if($done > $total) { if ($done > $total) {
return; return;
} }
if(empty($start)) { if (empty($start)) {
$start=time(); $start = time();
} }
$now = time(); $now = time();
$percent=(double)($done/$total); $percent = (double)($done / $total);
$bar=floor($percent*$size); $bar = floor($percent * $size);
$status="\r["; $status = "\r[";
$status.=str_repeat("=", $bar); $status .= str_repeat("=", $bar);
if($bar<$size){ if ($bar < $size) {
$status.=">"; $status .= ">";
$status.=str_repeat(" ", $size-$bar); $status .= str_repeat(" ", $size - $bar);
} else { } else {
$status.="="; $status .= "=";
} }
$display=number_format($percent*100, 0); $display = number_format($percent * 100, 0);
$status.="] $display% $done/$total"; $status .= "] $display% $done/$total";
$rate = ($now-$start)/$done; $rate = ($now - $start) / $done;
$left = $total - $done; $left = $total - $done;
$eta = round($rate * $left, 2); $eta = round($rate * $left, 2);
$elapsed = $now - $start; $elapsed = $now - $start;
$status.= " remaining: ".number_format($eta)." sec. elapsed: ".number_format($elapsed)." sec."; $status .= " remaining: " . number_format($eta) . " sec. elapsed: " . number_format($elapsed) . " sec.";
static::stdout("$status "); static::stdout("$status ");
flush(); flush();
// when done, send a newline // when done, send a newline
if($done == $total) { if ($done == $total) {
echo "\n"; echo "\n";
} }
} }

Loading…
Cancel
Save