From c2827eae30d3fe35812c9c903f6d1c6593e087bf Mon Sep 17 00:00:00 2001 From: zeebinz Date: Mon, 25 Mar 2013 21:16:59 +0000 Subject: [PATCH] Improve check for console color support Allows color output on Windows consoles that support ANSI escape sequences. --- classes/Kohana/Minion/CLI.php | 22 +++++++++- tests/minion/cli.php | 81 +++++++++++++++++++++++++++++++++++ 2 files changed, 102 insertions(+), 1 deletion(-) create mode 100644 tests/minion/cli.php diff --git a/classes/Kohana/Minion/CLI.php b/classes/Kohana/Minion/CLI.php index 13c1de6..3448ffc 100644 --- a/classes/Kohana/Minion/CLI.php +++ b/classes/Kohana/Minion/CLI.php @@ -285,7 +285,7 @@ public static function wait($seconds = 0, $countdown = false) public static function color($text, $foreground, $background = null) { - if (Kohana::$is_windows) + if ( ! Minion_CLI::supports_ansi()) { return $text; } @@ -312,4 +312,24 @@ public static function color($text, $foreground, $background = null) return $string; } + /** + * Checks whether ANSI escape sequences are supported by the console. + * + * Windows ANSICON and tty consoles support ANSI escape sequences. + * @link http://adoxa.110mb.com/ansicon/ + * + * Adapted from Symfony's Console Component: + * @author Fabien Potencier + * @copyright (c) Fabien Potencier + * @link https://github.com/symfony/Console + * @return bool + */ + public static function supports_ansi() + { + if (Kohana::$is_windows AND getenv('ANSICON')) + return TRUE; + + return (function_exists('posix_isatty') AND @posix_isatty(STDOUT)); + } + } diff --git a/tests/minion/cli.php b/tests/minion/cli.php new file mode 100644 index 0000000..2157911 --- /dev/null +++ b/tests/minion/cli.php @@ -0,0 +1,81 @@ +assertTrue(Minion_CLI::supports_ansi()); + + // Restore the environment + Kohana::$is_windows = $is_windows; + $ansicon = $ansicon ? 'ANSICON='.$ansicon : 'ANSICON'; + putenv($ansicon); + } + + /** + * Tests color output. + * + * @dataProvider provider_color_output + * @covers Minion_CLI::color + * @param string $colored The string with color codes + * @param string $text The original string + * @param string $foreground The foreground color + * @param string $background The background color + */ + public function test_color_output($colored, $text, $foreground, $background) + { + try + { + $actual = Minion_CLI::color($text, $foreground, $background); + } + catch (Kohana_Exception $e) + { + $this->assertRegExp('/Invalid CLI (foreground|background) color/', $e->getMessage()); + return; + } + + $expected = Minion_CLI::supports_ansi() ? $colored : $text; + $this->assertSame($expected, $actual); + } + + /** + * Provides test data for test_color_output. + * + * @return array + */ + public function provider_color_output() + { + return array( + array("\033[0;31m\033[42mtext\033[0m", 'text', 'red', 'green'), + array("\033[0;31mtext\033[0m", 'text', 'red', NULL), + // Invalid colors: + array(NULL, NULL, 'puce', 'lemon'), + array(NULL, NULL, 'red', 'lemon'), + array(NULL, NULL, 'puce', NULL), + array(NULL, NULL, NULL, NULL), + ); + } + +}