aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLaurent Bachelier <laurent@bachelier.name>2011-01-05 00:15:06 +0100
committerLaurent Bachelier <laurent@bachelier.name>2011-01-05 00:15:06 +0100
commit6c4912d273c7169393264e6a815a9af9690c2692 (patch)
tree07462cd1a968d12a88d8e8efd026afef4d7beaa3
parentRewrite the configuration file between restarts (diff)
downloadsymfttpd-6c4912d273c7169393264e6a815a9af9690c2692.tar.xz
Color class and first color usage in spawn
The color support is properly disabled when outside of a terminal.
-rw-r--r--lib/Color.php128
-rwxr-xr-xspawn16
2 files changed, 139 insertions, 5 deletions
diff --git a/lib/Color.php b/lib/Color.php
new file mode 100644
index 0000000..0c47789
--- /dev/null
+++ b/lib/Color.php
@@ -0,0 +1,128 @@
+<?php
+/*
+ * No "get" prefix on the class methods as pretty much
+ * all of them would have it.
+ */
+class Color
+{
+ protected static $enabled = false;
+
+ protected static $fgcolors = array(
+ 'black' => 30,
+ 'blue' => 34,
+ 'brown' => 33,
+ 'cyan' => 36,
+ 'green' => 32,
+ 'grey' => 37,
+ 'purple' => 35,
+ 'red' => 31,
+ 'yellow' => 33,
+ );
+
+ protected static $styles = array(
+ 'normal' => 0, // also "reset"
+ 'bright' => 1, // also "bold"
+ 'underline' => 4,
+ 'blink' => 5,
+ 'inverse' => 6, // also "reverse"
+ );
+
+ protected static $bgcolors = array(
+ 'black' => 40,
+ 'blue' => 44,
+ 'brown' => 43,
+ 'cyan' => 46,
+ 'green' => 42,
+ 'grey' => 47,
+ 'purple' => 45,
+ 'red' => 41,
+ 'yellow' => 43,
+ );
+
+ /**
+ * Get a control code for UNIX-like terminals, if color support is enabled.
+ * @param integer $number
+ * @return string
+ *
+ * @author Laurent Bachelier <laurent@bachelier.name>
+ */
+ public static function controlCode($number)
+ {
+
+ return self::$enabled ? "\033[".$number.'m' : '';
+ }
+
+ /**
+ * Get a control number from its type and name.
+ *
+ * @param array $list Type, e.g. Color::$fgcolor
+ * @param string $name Name, e.g. 'red'
+ * @return integer Control number, e.g. 31
+ *
+ * @author Laurent Bachelier <laurent@bachelier.name>
+ */
+ protected static function numberFromList($list, $name)
+ {
+
+ return $list[$name];
+ }
+
+ /**
+ * Get the control code for a foreground color.
+ * @param string $color Color name, e.g. 'red'
+ * @return string Escape sequence
+ *
+ * @author Laurent Bachelier <laurent@bachelier.name>
+ */
+ public static function fgColor($name)
+ {
+
+ return self::controlCode(self::numberFromList(self::$fgcolors, $name));
+ }
+
+ /**
+ * Get the control code for a background color.
+ * @param string $color Color name, e.g. 'red'
+ * @return string Escape sequence
+ *
+ * @author Laurent Bachelier <laurent@bachelier.name>
+ */
+ public static function bgColor($name)
+ {
+
+ return self::controlCode(self::numberFromList(self::$bgcolors, $name));
+ }
+
+ /**
+ * Get the control code for a style.
+ * @param string $color Color name, e.g. 'bold'
+ * @return string Escape sequence for this color
+ *
+ * @author Laurent Bachelier <laurent@bachelier.name>
+ */
+ public static function style($name)
+ {
+
+ return self::controlCode(self::numberFromList(self::$styles, $name));
+ }
+
+ /**
+ * Enable color support.
+ *
+ * @author Laurent Bachelier <laurent@bachelier.name>
+ */
+ public static function enable()
+ {
+ self::$enabled = true;
+ }
+
+ /**
+ * Disable color support.
+ *
+ * @author Laurent Bachelier <laurent@bachelier.name>
+ */
+ public static function disable()
+ {
+ self::$enabled = false;
+ }
+}
diff --git a/spawn b/spawn
index 942e093..9bfd24e 100755
--- a/spawn
+++ b/spawn
@@ -11,6 +11,7 @@ require dirname(__FILE__).'/lib/PosixTools.php';
require dirname(__FILE__).'/lib/MultiConfig.php';
require dirname(__FILE__).'/lib/Symfony.php';
require dirname(__FILE__).'/lib/Tail.php';
+require dirname(__FILE__).'/lib/Color.php';
$project_path = Symfony::getProjectPath();
$options = MultiConfig::get();
@@ -34,6 +35,11 @@ if ($options['fork'] && !function_exists('pcntl_fork'))
. 'symfttpd will run in single-process mode.');
$options['fork'] = false;
}
+$options['color'] = !Argument::get('C', 'no-color', false) && posix_isatty(STDOUT);
+if ($options['color'])
+{
+ Color::enable();
+}
if (Argument::get('K', 'kill', false))
{
@@ -67,7 +73,7 @@ try
}
catch (ExecutableNotFoundError $e)
{
- log_message("Required executable not found.", false);
+ log_message('Required executable not found.', false);
log_message($e->getMessage()
. ' not found in the specified paths: '
. implode(', ', PosixTools::getPaths()), false);
@@ -80,7 +86,7 @@ Template::writeConfig($options);
log_message('lighttpd started on '
. (strlen($options['bind']) ? $options['bind'] : 'all interfaces')
. ', port '.$options['port'].'.');
-log_message("\nAvailable applications");
+log_message("\nAvailable applications:");
$apps = array();
foreach (new DirectoryIterator($project_path.'/web') as $file)
{
@@ -110,8 +116,8 @@ else
if ($options['tail'])
{
$multitail = new MultiTail();
- $multitail->add('lighttpd-access', new Tail($options['log_dir'].'/access.log'));
- $multitail->add('lighttpd-error', new Tail($options['log_dir'].'/error.log'));
+ $multitail->add(Color::fgColor('blue').'lighttpd-access'.Color::style('normal'), new Tail($options['log_dir'].'/access.log'));
+ $multitail->add(Color::fgColor('red').'lighttpd-error'.Color::style('normal'), new Tail($options['log_dir'].'/error.log'));
// We have to do it before the fork to capture the startup messages
$multitail->consume();
}
@@ -160,7 +166,7 @@ else
}
else
{
- log_message('Something in web/ changed. Restarting lighttpd.');
+ log_message(Color::style('bright').'Something in web/ changed. Restarting lighttpd.'.Color::style('normal'));
Template::writeConfig($options);
}
} while (file_exists($options['restartfile']));