<?php
/**
 * Some functions for producing color, clearing a line or a page
 * and reseting the terminal.
 *
 * @author: Niko Efthymiou <nefthy-console.php@nefthy.de>
 * @lisence: GPLv2 (no later version)
 */

define('CONSOLE_ESC', "\033");
$GLOBALS['console'] = array();
$GLOBALS['console']['colors'] = array(
	'black' => 0,
	'red' => 1,
	'green' => 2,
	'yellow' => 3,
	'blue' => 4,
	'magenta' => 5,
	'cyan' => 6,
	'white' => 7,
);

function console_fg_256($r, $g, $b) {
	$color_code = 36*$r + 6*$g + $b + 16;
	echo CONSOLE_ESC,'[38;5;',$color_code,'m';
	//echo CONSOLE_ESC,'[38;5;',$color_code,'m';
}

function console_fg($color, $bright = false) {
	echo CONSOLE_ESC,'[', ($bright ? 9 : 3),
		$GLOBALS['console']['colors'][$color],'m';
}

function console_bg($color, $bright = false) {
	echo CONSOLE_ESC, '[', ($bright ? 10 : 4),
		$GLOBALS['console']['colors'][$color],'m';
}

function console_reset() {
	echo CONSOLE_ESC . "[0m";
}

function console_underline($on = true) {
	if ($on) echo CONSOLE_ESC, "[4m";
	else echo CONSOLE_ESC, "[24m";
}

function console_clear() {
	echo CONSOLE_ESC, '[2J';
}

function console_clear_line() {
	echo CONSOLE_ESC, '[1M';
}

?>
