Jump to content

[SOLVED] Color the CMD line


benphelps

Recommended Posts

I have checked everywhere I know on the interwebs looking for a function to color the CMD line output.  I know it can be done, I have done it before, but I lost my snippets folder and now I need the function.

 

Does anyone here on PHPFreaks have this function, or know how it can be done?  It wasn't alot of code, maybe 15 lines in total.

Link to comment
Share on other sites

I don't usually bump things, but I really really need this function.  With limited knowledge of the Linux CMD line, I have no idea where to start.

 

If someone could help me, I can offer (good) hosting (Web or VPS) in exchange.

 

 

(if me offering something in return is out of the TOS for this forum category, feel free to move it)

Link to comment
Share on other sites

I'm not sure how I could explain it any better.

 

Instead of plain white text in the command line, color the output.

 

I start a php script from the command line that prints out some text, I would like to color that text.

 

I have done this before with a simple function, maybe 15 lines of code max.  I have just lost my snippets folder, so in that, I lost the function.  I found the function on the a blog, which I found via. StumbleUpon. I have tried (for days now) to find this snippet, or any other snippet that might work with no luck.

Link to comment
Share on other sites

Heres the colour codes http://www.linux.com/archive/articles/113860

$output = "Hello World\n";
print "\033[31m".$output;

Should be really easy to write a function. I use the above to print red text. I then use the following function to clear the screen so you get a blink effect when a process is going through a loop i.e. Cleaning database records.

function clearscreen() {
$clearscreen = chr(27)."[H".chr(27)."[2J";
print $clearscreen;
}

Link to comment
Share on other sites

THANK YOU!

 

Here is the function I wrote to make it easy.  I'm sure this can be compressed down to a smaller function, but it does the job for me.

 

function cmdColor($text, $color = "32"){
$color = strtolower($color);
switch($color){
	case "30":
	case "black":
		$prefx = "\033[30m";
	break;
	case "31":
	case "red":
		$prefx = "\033[31m";
	break;
	case "32":
	case "green":
		$prefx = "\033[32m";
	break;
	case "33":
	case "yellow":
		$prefx = "\033[33m";
	break;
	case "34":
	case "blue":
		$prefx = "\033[34m";
	break;
	case "35":
	case "magenta":
		$prefx = "\033[35m";
	break;
	case "36":
	case "cyan":
		$prefx = "\033[36m";
	break;
	case "37":
	case "white":
		$prefx = "\033[37m";
	break;		
}
print $prefx.$text."\33[0m";
}

 

 

I added "\33[0m" to the end so it sets the color back to default, this way, if the script terms, your not stuck in a colored cmd line.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.