PHPackages                             metashock/jm\_console - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. metashock/jm\_console

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

metashock/jm\_console
=====================

Jm\_Console is a library for terminal colors

0.3.0(13y ago)58[4 issues](https://github.com/metashock/Jm_Console/issues)BSD-3PHP

Since Mar 4Pushed 11y ago2 watchersCompare

[ Source](https://github.com/metashock/Jm_Console)[ Packagist](https://packagist.org/packages/metashock/jm_console)[ RSS](/packages/metashock-jm-console/feed)WikiDiscussions master Synced 2mo ago

READMEChangelogDependencies (1)Versions (3)Used By (0)

J@m; Console
============

[](#jm-console)

ANSI console library for PHP

Installation
------------

[](#installation)

To install Jm\_Console you can use the PEAR installer or get a tarball and install the files manually.

---

### Using the PEAR installer

[](#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:

```
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:

```
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:

```
pear install -a metashock/Jm_Console-0.3.0

```

---

### Manually download and install files

[](#manually-download-and-install-files)

Alternatively, you can just download the package from  and put into a folder listed in your include\_path. Please refer to the php.net documentation of the include\_path directive.

Usage
-----

[](#usage)

---

### Basics

[](#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:

```
// require Jm_Autoloader
require_once 'Jm/Autoloader.php';

// get an instance of Jm_Console
$console = Jm_Console::singleton();
```

---

### Printing output

[](#printing-output)

Jm\_Console provides write access to STDOUT and STDERR. Output is done using the following functions:

```
$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

[](#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.

The simplest thing is to just specifiy a foreground color:

```
$console->writeln('hello, world!', 'green');   // writes green text to stdout
$console->errorln('an error occured!', 'red'); // writes red text to stderr
```

[![green text](res/colors.png)](res/colors.png)

or just specify a text decoration:

```
$console->writeln('Booh!', 'bold');              // writes bold text to stdout
$console->writeln('I\'m a link!', 'underline');  // writes underlined text to stdout
```

[![green text](res/decorations.png)](res/decorations.png)

or specify both a foreground color and a text decoration:

```
$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](res/decorations2.png)](res/decorations2.png)

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:

```
$console->writeln('Booh!', 'white,bg:blue');             // writes white text on a blue background to stdout
```

[![green text](res/background-color.png)](res/background-color.png)

Table: *Available Graphics modes*

  Colors Text Decorations   - black
- red
- green
- yellow
- blue
- purple
- cyan
- white
- default

 - bold
- light
- italic
- underline
- blink
- reverse
- hidden
- default

 ---

### Cursor positioning

[](#cursor-positioning)

ANSI terminals support positioning of the cursor.

```
$console->cursorPosition(0, 0); // positioning the cursor at upper left corner
```

It is also possible to store the cursor position and restore it later:

```
$console->savecursor(); // saves the cursor position
// ...
$console->restorecursor(); // restores the cursor position
```

Example
-------

[](#example)

---

### Drawing a progress bar on terminal

[](#drawing-a-progress-bar-on-terminal)

```
