Skip to content
Browse files

Replaced write and added writeln function. Now write won't break the …

…line
  • Loading branch information...
1 parent 3a908c8 commit ba0eedeaf9a93ed79fdeedfedb61125842908701 @darlanalves darlanalves committed Feb 5, 2013
Showing with 46 additions and 7 deletions.
  1. +2 −2 Phakefile
  2. +44 −5 lib/bash_colors.php
View
4 Phakefile
@@ -1,8 +1,8 @@
<?php
desc('Show some colors');
task('colors', function() {
- write('Running a test of color codes...');
- write(
+ writeln('Running a test of color codes...');
+ writeln(
red('Red star,'), "\n",
green('green leaf,'), "\n",
blue('blue sky,'), "\n",
View
49 lib/bash_colors.php
@@ -22,6 +22,12 @@
// Const for TTY detection result
define('OUTPUT_IS_TTY', posix_isatty(STDOUT));
+/**
+ * Generates a string with ANSI format codes
+ * @param string $str
+ * @param string color
+ * @param bool $bold
+ */
function color($str, $color, $bold = false) {
global $_COLORS;
$code = $_COLORS[$color];
@@ -71,17 +77,50 @@ function black($str, $bold = false) {
return color($str, 'black', $bold);
}
-function write() {
+/**
+ * Gets a list of strings and return a escaped string to output
+ * @private
+ * @param string message...
+ * @return string
+ */
+function cleanupStrings($args) {
$str = array();
- foreach(func_get_args() as $part) {
+ foreach($args as $part) {
$str[] = trim($part);
}
+
// just in case of a line with double quotes
$str = implode(' ', $str);
- $str = trim(addcslashes($str, '"'));
+ $str = trim($str);
+ return $str;
+}
+
+/**
+ * Send output to pipe or stdout
+ * @private
+ */
+function output($str) {
if (OUTPUT_IS_TTY) {
- echo `echo "$str"`;
+ fwrite(\STDOUT, $str);
} else {
- echo stripslashes("$str\n");
+ echo stripslashes($str);
}
}
+
+/**
+ * Writes output
+ * @param string $str...
+ */
+function write() {
+ $str = cleanupStrings(func_get_args());
+ output($str);
+}
+
+/**
+ * Writes output and starts a new line
+ * @param string $str...
+ */
+function writeln() {
+ $str = cleanupStrings(func_get_args()) . "\n";
+ output($str);
+}

0 comments on commit ba0eede

Please sign in to comment.
Something went wrong with that request. Please try again.