Skip to content

Instantly share code, notes, and snippets.

@avargas
Created May 6, 2012 22:08
Show Gist options
  • Save avargas/2624785 to your computer and use it in GitHub Desktop.
Save avargas/2624785 to your computer and use it in GitHub Desktop.
autorun command automatically
<?php
class Colors {
private $foreground_colors = array();
private $background_colors = array();
public function __construct() {
// Set up shell colors
$this->foreground_colors['black'] = '0;30';
$this->foreground_colors['dark_gray'] = '1;30';
$this->foreground_colors['blue'] = '0;34';
$this->foreground_colors['light_blue'] = '1;34';
$this->foreground_colors['green'] = '0;32';
$this->foreground_colors['light_green'] = '1;32';
$this->foreground_colors['cyan'] = '0;36';
$this->foreground_colors['light_cyan'] = '1;36';
$this->foreground_colors['red'] = '0;31';
$this->foreground_colors['light_red'] = '1;31';
$this->foreground_colors['purple'] = '0;35';
$this->foreground_colors['light_purple'] = '1;35';
$this->foreground_colors['brown'] = '0;33';
$this->foreground_colors['yellow'] = '1;33';
$this->foreground_colors['light_gray'] = '0;37';
$this->foreground_colors['white'] = '1;37';
$this->background_colors['black'] = '40';
$this->background_colors['red'] = '41';
$this->background_colors['green'] = '42';
$this->background_colors['yellow'] = '43';
$this->background_colors['blue'] = '44';
$this->background_colors['magenta'] = '45';
$this->background_colors['cyan'] = '46';
$this->background_colors['light_gray'] = '47';
}
// Returns colored string
public function getColoredString($string, $foreground_color = null, $background_color = null) {
$colored_string = "";
// Check if given foreground color found
if (isset($this->foreground_colors[$foreground_color])) {
$colored_string .= "\033[" . $this->foreground_colors[$foreground_color] . "m";
}
// Check if given background color found
if (isset($this->background_colors[$background_color])) {
$colored_string .= "\033[" . $this->background_colors[$background_color] . "m";
}
// Add string and end coloring
$colored_string .= $string . "\033[0m";
return $colored_string;
}
// Returns all foreground color names
public function getForegroundColors() {
return array_keys($this->foreground_colors);
}
// Returns all background color names
public function getBackgroundColors() {
return array_keys($this->background_colors);
}
}
$colors = new Colors;
function get_files ($dir)
{
$res = opendir($dir);
if (!$res) {
return array();
}
$files = array();
while (($file = readdir($res))) {
if (in_array($file, array('.', '..', '.git'))) {
continue;
}
$file = $dir . '/' . $file;
if (is_dir($file)) {
$files = array_merge($files, get_files($file));
} else if (is_file($file) && strpos($file, '.php') !== false) {
$files[$file] = filemtime($file);
}
}
closedir($res);
return $files;
}
function get_files_hash ($dir)
{
clearstatcache();
if (is_array($dir)) {
$md5 = '';
foreach ($dir as $e) {
$md5 .= get_files_hash($e);
}
return md5($md5);
}
$files = get_files($dir);
$md5 = '';
foreach ($files as $file=>$time) {
$md5 .= $file . ':' . $time;
}
return md5($md5);
}
function get_env_vars ()
{
return $_ENV;
}
function monitor ($dir, $command)
{
global $colors;
$lastcheck = microtime(true);
$last = get_files_hash($dir);
$i = 1;
$clear = implode('', array_map('chr', array(27, 91, 72, 27, 91, 50, 74)));
$line = str_repeat('-', exec('tput cols'));
echo $clear;
while (true) {
$cols = exec('tput cols');
echo $last . ' running for #' . $i++ . ': ' . $command ."\n";
echo $line;
$descs = array(array('pipe', 'r'), array('pipe', 'w'), array('pipe', 'w'));
$pipes = arraY();
$cwd = getcwd();
$env = get_env_vars();
$proc = proc_open($command, $descs, $pipes);
$read = array($pipes[1], $pipes[2]);
$write = null;
$except = null;
foreach ($read as $pipe) {
stream_set_timeout($pipe, 0, 100);
}
while (true) {
$read = array($pipes[1], $pipes[2]);
foreach ($read as $o=>$k) {
if (feof($k)) {
unset($read[$o]);
}
}
if (!$read) {
break;
}
stream_select($read, $write, $except, 0, 500000);
foreach ($read as $pipe) {
if ($pipe == $pipes[2]) {
$buff = fgets($pipe);
} else {
$buff = fread($pipe, 1);
}
if ($buff !== false) {
if ($pipe == $pipes[2]) {
echo $colors->getColoredString($buff, "white", "red");
} else {
echo $buff;
}
}
}
$newcheck = microtime(true);
if ($newcheck - $lastcheck < 1) {
continue;
}
$lastcheck = $newcheck;
$new = get_files_hash($dir);
if ($new != $last) {
echo "terminating ";
$info = proc_get_status($proc);
posix_kill($info['pid'], SIGKILL);
break;
}
}
echo $line;
echo "closing pipes ";
foreach ($pipes as $pipe) {
fclose($pipe);
}
echo "closing proc ";
$out = proc_close($proc);
echo "\n$line";
# success
echo "Exit status: $out\n";
# loop and wait til file changes so we can re-run it
while (true) {
$new = get_files_hash($dir);
if ($new != $last) {
break;
}
# sleep for 1 secs if previous run died, or else .4 seconds
usleep(500000);
}
$last = $new;
echo $clear;
}
}
$command = array_pop($argv);
array_shift($argv);
monitor($argv, $command);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment