Created
October 1, 2010 16:56
-
-
Save birkir/606481 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
system("stty -icanon"); | |
define('LINE_RETURN', "\n"); | |
class Console { | |
// Input prompt | |
public static $pi = "\033[36m>>\033[37m "; | |
// Return prompt | |
public static $po = "=> "; | |
// Private variables | |
private $history = array(); | |
private $cache = ''; | |
private $catch_cursor = array(); | |
/** | |
* Factory of the class | |
* | |
* @return void | |
*/ | |
public static function factory() | |
{ | |
print("\033[1;30m"); | |
print("The ruby-like console for PHP\nCopyright 2010 - Birkir R Gudjonsson ([email protected])\n"); | |
print(self::$pi); | |
return new self; | |
} | |
/** | |
* Parse command | |
* | |
* @param string Command or line | |
* @return void | |
*/ | |
public function command($cmd = NULL) | |
{ | |
if (substr($cmd,-1) == ';') | |
{ | |
$cmd = substr($cmd, 0, strlen($cmd)-1); | |
} | |
$cmd = trim($cmd); | |
$this->history[] = $cmd; | |
switch ($cmd) | |
{ | |
case "clear": | |
Command::_clear(); | |
break; | |
case "exit": | |
Command::_exit(); | |
break; | |
case "test": | |
Command::_test(); | |
break; | |
default: | |
$cmd .= ';'; | |
if ($cmd != ';') | |
{ | |
$this->result = NULL; | |
eval('$this->result = '.$cmd); | |
print("\033[1;31m"); | |
print((string) self::$po); | |
print("\033[1;37m"); | |
is_object($this->result) ? print((string) $this->result) : var_dump($this->result); | |
print("\033[37m"); | |
} | |
break; | |
} | |
} | |
public function handlechar($char = NULL) | |
{ | |
$this->catch_cursor[] = (string) dechex(ord($char)); | |
$arrow = $this->catch_cursor[count($this->catch_cursor)-3].$this->catch_cursor[count($this->catch_cursor)-2].$this->catch_cursor[count($this->catch_cursor)-1]; | |
$backspace = substr($arrow, -2); | |
if ($backspace == '7f' AND strlen($this->cache) > 1) | |
{ | |
printf("%c[D", 27); printf("%c[D", 27); printf("%c[D", 27); printf("%c[K", 27); | |
$this->cache = substr($this->cache, 0, strlen($this->cache)-1); | |
return FALSE; | |
} | |
elseif ($backspace == '7f') | |
{ | |
printf("%c[D", 27); printf("%c[D", 27); printf("%c[K", 27); | |
return FALSE; | |
} | |
switch ($arrow) | |
{ | |
case "1b5b41": // Up arrow | |
printf("%c[D", 27);printf("%c[D", 27);printf("%c[D", 27);printf("%c[D", 27);printf("%c[K", 27); | |
return false; | |
break; | |
case "1b5b42": // Down arrow | |
printf("%c[D", 27);printf("%c[D", 27);printf("%c[D", 27);printf("%c[D", 27);printf("%c[K", 27); | |
break; | |
case "1b5b43": // Right arrow | |
echo "HELLO RIGHT WORLD"; | |
break; | |
case "1b5b44": // Left arrow | |
printf("%c[D", 27);printf("%c[D", 27);printf("%c[D", 27);printf("%c[D", 27);printf("%c[D", 27);printf("%c[K", 27); | |
break; | |
} | |
switch ($char) | |
{ | |
case "\n": | |
self::command($this->cache); | |
$this->cache = ''; | |
echo self::$pi; | |
return FALSE; | |
break; | |
} | |
$this->cache .= $char; | |
} | |
} | |
class Command { | |
public function _clear() | |
{ | |
array_map(create_function('$a', 'print chr($a);'), array(27, 91, 72, 27, 91, 50, 74)); | |
} | |
public function _exit() | |
{ | |
exit; | |
} | |
public function _test() | |
{ | |
echo 'my function'; | |
} | |
} | |
$console = Console::factory(); | |
while ($c = fread(STDIN, 1)) | |
{ | |
$console->handlechar($c); | |
} | |
echo 'I just died'; | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment