Generating Command Line Colors with PHP

Generating ANSI (or ASCII) command line colors, or terminal colors, or whatever you call them, is easy with PHP. As I have said in my previous post, ANSI Command Line Colors under Windows, having colored text in the command line is a great help for spotting error or success messages. So here’s a quick and dirty function in PHP to do just that:


function colorize($text, $status) {
 $out = "";
 switch($status) {
  case "SUCCESS":
   $out = "[42m"; //Green background
   break;
  case "FAILURE":
   $out = "[41m"; //Red background
   break;
  case "WARNING":
   $out = "[43m"; //Yellow background
   break;
  case "NOTE":
   $out = "[44m"; //Blue background
   break;
  default:
   throw new Exception("Invalid status: " . $status);
 }
 return chr(27) . "$out" . "$text" . chr(27) . "[0m";
}

echo colorize("Your command has successfully executed...", "SUCCESS");

References

http://en.wikipedia.org/wiki/ANSI_escape_code

http://php.net/manual/en/function.chr.php

https://wiki.archlinux.org/index.php/Color_Bash_Prompt

ANSI Command Line Colors under Windows

Having colored text in the command line is a great help for spotting error or success messages. Unfortunately, those of us developing under Windows do not have this feature by default. Here’s how to enable it:

  1. Download ANSICON from http://adoxa.110mb.com/ansicon/index.html or https://github.com/adoxa/ansicon
  2. Extract the proper files (Depending on if you have a 32 or 64 bit machine) to c:\ansicon\ (For example). I have a 32 bit machine and hence I extracted the files from inside the x86 folder.
  3. Open a command line prompt and go to c:\ansicon, and then type “ansicon -i” without the quotes
  4. Done

You can now enjoy the colored output of PHPUnit for example.

ANSI Command Line Colors Example with PHPUnit

Note: I have installed ANSICON 1.3 under Windows 7. My best guess is that this process will work for other versions of Windows too.

Update: I have written a new post on how to Generate Command Line Colors with PHP

Changing the default Look and Feel of NetBeans

Why change the default Look and Feel?

Aesthetics apart, every developer should change the default Look and Feel of NetBeans. Why? because it has an annoying bug. Here’s what happens: you’re pumping code in the NetBeans code editor like crazy. You need to switch to another window for a quick copy / paste from your eternal library of super code. You press ALT + TAB, you go to your other window, you press CTRL + C, you press ALT + TAB, you’re in NetBeans again getting ready to kick your paste operation using CTRL + V… but… but… you’re no longer editing the code! You’re now navigating the menu! ANNOYING! SHOW STOPPER!

Luckily, it turns out this is a bug in the Windows Look and Feel of Java (Which NetBeans uses by default). Time to change it of course. Read on.

Note: I’m running NetBeans IDE 6.7.1 under Windows XP SP3 / Java SDK 1.6. If you have a different version, this bug might not manifest (Lucky you!).

8 Steps to change the default NetBeans Look and Feel

  1. Download the three .nbm files from NetBeans substance look and feel plugin
  2. Open NetBeans. Go to Tools -> Plugins -> Downloaded
  3. Click on Add Plugins and add the three downloaded .nbm files (You have to add them one by one)
  4. Make sure the three files are selected, click Install, and follow through
  5. Restart NetBeans
  6. Go to Tools -> Options -> Miscellaneous -> Look and Feel
  7. Choose your preferred Look and Feel and Restart NetBeans again
  8. Enjoy :)

References

ALT + TAB switching to/from netbeans on Windows
http://forums.netbeans.org/topic5465.html

NetBeans substance look and feel plugin (Project’s home page)
http://kenai.com/projects/nbsubstance/pages/Home

NetBeans IDE – Look and Feel
http://netbeanside61.blogspot.com/2008/05/netbeans-ide-look-and-feel.html

JTattoo
http://www.jtattoo.net

20+ Free Look and Feel Libraries for Java Swing
http://javabyexample.wisdomplug.com/component/content/article/37-core-java/65-20-free-look-and-feel-libraries-for-java-swings.html

Capture the output of var_dump in a string

Introduction

You are programming in PHP and you have an array variable that you’d like to explore at different execution paths. Of course, the best way is to use a PHP debugger like xdebug or Zend Debugger, but, what happens when you’re too lazy to install a debugger? What happens when you don’t want or can’t install a debugger and you just need to check the content of that array by dumping it in your log file? Well, you might think you’re stuck, but, read on…

What is var_dump?

The var_dump manual page states that var_dump displays structured information about one or more expressions that includes its type and value. Arrays and objects are explored recursively with values indented to show structure.

The problem is that var_dump outputs its result directly to the browser, how can you capture its output in a string variable? read on…

Using output control functions for the solution

Output control functions can be used to capture and redirect the standard output. For more details, read the PHP manual on output control functions, of course.

Here’s the solution:

function varDumpToString ($var)
{
    ob_start();
    var_dump($var);
    $result = ob_get_clean();
    return $result;
}
//
//Example usage:
//    $data = array('first', 'second', 'third');
//    $result = varDumpToString($data);
//

All you have to do now is call the varDumpToString function. Happy coding :)

Hide Gmail’s spam count

Does the spam count in Gmail bother you? It certainly does.

Are you too lazy to delete your spam messages every few minutes? Of course you are.

Would you like to preserve your spam messages just in case they might contain a legitimate email? Of course you would love to.

You hate using third party tools such as Greasemonkey scripts to accomplish this very simple task? Of course you do.

Well, you’re in luck. I found this quick tip at Lifehacker and thought it’s too good not to be shared. The idea is to create a filter telling Gmail that all incoming spam messages should be marked as read. Of course, Gmail will keep spam messages for 30 days, giving you enough time to rescue legitimate email when you get that angry call from a customer or friend (Ummm… euhh… why didn’t you reply to my email yet!!!).

Gmail Spam Filter Creation Screenshot

What you have to do is create a new filter, add “in:spam” in the “Has the words” field, click next, ignore the warning given by Gmail (because, well, contrary to what they say, it works!!!), choose “Mark as read”, and click create filter.

Simple, efficient, and clean. Enjoy :)

Searching...
Powered by Google