2017 © Pedro Peláez
 

library jm_console

Jm_Console is a library for terminal colors

image

metashock/jm_console

Jm_Console is a library for terminal colors

  • Wednesday, January 14, 2015
  • by hek2mgl
  • Repository
  • 2 Watchers
  • 4 Stars
  • 0 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 4 Open issues
  • 3 Versions
  • 0 % Grown

The README.md

J@m; Console

ANSI console library for PHP, (*1)

Installation

To install Jm_Console you can use the PEAR installer or get a tarball and install the files manually., (*2)


Using the PEAR installer

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

Manually download and install files

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)

Usage


Basics

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();

Printing output

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

Terminal colors

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

green text, (*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

green text, (*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

green text, (*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

green text, (*17)

Table: Available Graphics modes, (*18)

Colors Text Decorations
  • black
  • red
  • green
  • yellow
  • blue
  • purple
  • cyan
  • white
  • default
  • bold
  • light
  • italic
  • underline
  • blink
  • reverse
  • hidden
  • default

Cursor positioning

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

Example


Drawing a progress bar on terminal

<?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');
}

API documentation

The API docs can be found here: http://metashock.de/docs/api/Jm/Console/index.xhtml, (*21)

The Versions

14/01 2015

dev-realine_fixes

dev-realine_fixes

Jm_Console is a library for terminal colors

  Sources   Download

BSD-3

The Requires

 

02/09 2014

dev-master

9999999-dev

Jm_Console is a library for terminal colors

  Sources   Download

BSD-3

The Requires

 

04/03 2013

0.3.0

0.3.0.0

Jm_Console is a library for terminal colors

  Sources   Download

BSD-3

The Requires

  • pear-www.metashock.de/pear/jm_autoloader *