Tuesday, July 28, 2009

New Feature at Paste Ninja

Some of you may have noticed last week a new feature was rolled out on Paste Ninja, the premier PHP-powered paste bin-- patches! Here's how it works:
  1. Update an existing paste

  2. Click the Compare link that appears in the updated paste's header

  3. In the Compare dialog that opens, select your desired current and target revisions; a colorized unified diff is displayed so you can verify the changes

  4. Click the dialog's Download button

It’s that simple!

Tuesday, July 21, 2009

Seamless Error Highlighting

A lot of output can be generated when you compiling large projects. When it breaks, it can be difficult to identify the particular spot in the build-process where things when wrong. Highlighting the error messages can help them stand out from the rest of the output.

ANSI escape sequences can be used to modify how your terminal window displays its text. For example, outputting the sequence \033[41;37mHello World\033[0m would result in "Hello World" displayed in white text against a red background. Escape sequences begin with an escape character (ASCII 27, octal 033) and bracket. The control values are then given (multiple values are semicolon-separated) and the entire sequence closes with m.

You can highlight certain messages by routing the STDOUT and STDERR streams to sed and performing a replacement.
s,(.*error.*|.*fail.*|.*undef.*),\033[41;37m\1\033[0m,gi
The values you match are of course entirely up to your discretion.

The tricky part is quoting and escaping the expression correctly so various meta-characters aren't intercepted by the shell. And some implementations of sed won't correctly convert \033 to an escape character, so you may need to enter it directly by typing CTRL+V, CTRL+[. Depending on your terminal, an actual escape character may be displayed as ^[ or a special glyph like ESC when you enter it.
make install 2>&1 | sed -e \
's,\(.*error.*\|.*fail.*\|.*undef.*\),ESC[41;37m\1ESC[0m,gi'
If you find yourself using such highlighting often, you may want to define a function to save yourself some typing. For example, with bash you can add something like this to your .bashrc file:
function make() {
/usr/bin/make $@ 2>&1 | sed -e \
's,\(.*error.*\|.*fail.*\|.*undef.*\),ESC[41;37m\1ESC[0m,gi';
}
You can type make install at the prompt like you normally would. bash will call the new make() function, which in turn calls the actual make utility with any arguments (such as install) and colorizes the output. Error highlighting is now seamless and automatic!

Tuesday, July 14, 2009

OOP in JS

When people would ask me about Object Oriented Programming in JavaScript, I would always send them to two pages written by Gavin Kistner. The other day when I went to visit them myself to quickly check something, they were gone! What happened, Gavin?!

I promptly dug the pages up from a search engine's cache to mirror them, but all the original copyrights still belong to Gavin.