dev-realine_fixes
dev-realine_fixesJm_Console is a library for terminal colors
BSD-3
The Requires
dev-master
9999999-devJm_Console is a library for terminal colors
BSD-3
The Requires
Wallogit.com
2017 © Pedro Peláez
Jm_Console is a library for terminal colors
ANSI console library for PHP, (*1)
To install Jm_Console you can use the PEAR installer or get a tarball and install the files manually., (*2)
If you haven't discovered the metashock pear channel yet you'll have to do it. Also you should issue a channel update:, (*3)
pear channel-discover metashock.de/pear pear channel-update metashock
After this you can install Jm_Console. The following command will install the lastest stable version with its dependencies:, (*4)
pear install -a metashock/Jm_Console
If you want to install a specific version or a beta version you'll have to specify this version on the command line. For example:, (*5)
pear install -a metashock/Jm_Console-0.3.0
Alternatively, you can just download the package from http://www.metashock.de/pear and put into a folder listed in your include_path. Please refer to the php.net documentation of the include_path directive., (*6)
Before accessing Jm_Console's functions you'll first get an object reference to it. Jm_Console is a singleton class meaning there is just a single reference available. To get the reference call:, (*7)
// require Jm_Autoloader require_once 'Jm/Autoloader.php'; // get an instance of Jm_Console $console = Jm_Console::singleton();
Jm_Console provides write access to STDOUT and STDERR. Output is done using the following functions:, (*8)
$console->write('foo'); // writes foo to stdout
$console->writeln('foo'); // writes foo to stdout and adds a newline
$console->error('foo'); // writes foo to stderr
$console->errorln('foo'); // writes foo to stderr and adds a newline
The ANSI Terminal standard allows to define a foreground color, a background color and choose a text decoration mode. Jm_Console aims to provide an intuitive access to terminal colors when printing text., (*9)
The simplest thing is to just specifiy a foreground color:, (*10)
$console->writeln('hello, world!', 'green'); // writes green text to stdout
$console->errorln('an error occured!', 'red'); // writes red text to stderr
, (*11)
or just specify a text decoration:, (*12)
$console->writeln('Booh!', 'bold'); // writes bold text to stdout
$console->writeln('I\'m a link!', 'underline'); // writes underlined text to stdout
, (*13)
or specify both a foreground color and a text decoration:, (*14)
$console->writeln('Booh!', 'blue,bold'); // writes bold blue text to stdout
$console->writeln('I\'m a link!', 'yellow, underline'); // writes underlined yellow text to stdout
, (*15)
If want to set the background color you'll have to use the prefix bg: in front of the color. Otherwise Jm_Console couldn't make a difference between foreground color and background color:, (*16)
$console->writeln('Booh!', 'white,bg:blue'); // writes white text on a blue background to stdout
, (*17)
Table: Available Graphics modes, (*18)
| Colors | Text Decorations |
|---|---|
|
|
ANSI terminals support positioning of the cursor., (*19)
$console->cursorPosition(0, 0); // positioning the cursor at upper left corner
It is also possible to store the cursor position and restore it later:, (*20)
$console->savecursor(); // saves the cursor position // ... $console->restorecursor(); // restores the cursor position
<?php
require_once 'Jm/Autoloader.php';
Jm_Autoloader::singleton()->prependPath('lib/php');
$console = Jm_Console::singleton();
for($a = 0; $a < 3; $a++) {
$s = rand(1, 50000);
$console->savecursor();
$total = rand(1,100);
for($i = 0; $i <= $total; $i++) {
if($console->stdout()->assumeIsatty()) {
$console->stdout()->eraseln();
$console->restorecursor();
$console->write('importing: ');
progressbar($i, $total);
printf(" %s/%s", $i, $total);
} else {
printf("importing: %s/%s", $i, $total);
echo PHP_EOL;
}
usleep($s);
}
$console->writeln();
}
/**
* Renders the progressbar
*/
function progressbar($now, $total, $w=35) {
$console = Jm_Console::singleton();
$console->write('[', 'white,light');
$n = floor($now * $w / $total);
$console->write(str_repeat('+', $n), 'green,light');
if($n < $w) {
$console->write(']', 'green,light');
$console->write(str_repeat('-', $w - $n -1), 'red,light');
}
$console->write(']', 'white,light');
}
The API docs can be found here: http://metashock.de/docs/api/Jm/Console/index.xhtml, (*21)
Jm_Console is a library for terminal colors
BSD-3
Jm_Console is a library for terminal colors
BSD-3