The Accidental Rubyist

invalid byte sequence in UTF-8

colorizing bash output on terminal (non-curses)

leave a comment »

I’ve found that my ruby command-line scripts (gems) that put information into sqlite tables and then report from there, take time to load. If not loaded for an hour, the command can take as much as 8-10 seconds to load. I’ve tried faster-rubygems but that has made no difference (I am on 1.9.2 on OSX Lion).

The load time is okay, when i wish to add some info, but when I keep checking, its not acceptable. However, my earlier bash scripts that do similar reporting (and addition) respond instantly. Now i am not going back to writing stuff in bash, since the syntax keeps befuddling me after all these years. However, it takes only a few minutes to report data from sqlite, and get instant responses. You can also throw in 256 colors to get a neat presentable output. Here are some things that can help you.

This is the meat of the script. Define a temporary input and output file

echo ".mode column" > $file
echo "select id, title, priority, status from mytable where status != 'closed' order by priority , id;" >> $file
sqlite3 bugzy.sqlite < $file >> $out

Now your output is in the outfile. You can less or most the file.

If you want to color the rows here are some options.
colorize is a neat program that allows you to define regexes that work on a line basis and color the entire line. There’s a switch for "only color the pattern not entire line", but you can’t have both. So I stuck with the default.

cat <<ZZZ > colorize.cfg
/ cancel/ black
/ closed / black
/ started / blue on_yellow
/ P1 / reverse
/ P2 / bold white on_blue
/ P3 / yellow
/ P4 / magenta
/ P5 / bold blue
ZZZ
colorize --config=./colorize.cfg $out | less

So here I create a config file for colorize to use. It does the rest. Notice it has attribute, background and foreground, although it often does not work if you give both attribute and other options. Also, it does not seem to support 256 colors.

If you are willing to write a few lines of sed, you can add a variety of other colors or backgrounds rather than the garish 8 or 16. I like shades of grey.

I’ve put the basic color codes into a file that can be sourced. I’ve added some for the gray scale also. You need 256 color support in your Terminal (OSX’s Terminal has it, so does iTerm2). gnu-screen will have to be recompiled with 256 (see previous post).

I found a function that lets you get any color.

 function EXT_COLOR () { echo -ne "33[38;5;$1m"; }

You can use it as "$(EXT_COLOR 128) hello". Or use backticks.

Now use these combinations in sed (warning: blog has eaten up slashes and maybe more).

text=$(sed "
  s/(.* started .*)/${BLUE}${ON_WHITE}1${DEFAULT}/g;

  s/(.* canceled .*)/${BLACK}1${DEFAULT}/g;
  s/(.*P1.*)/${BLACK}${ON_YELLOW}1${DEFAULT}/g;

  s/(.*P2.*)/${YELLOW}${ON_GRAY236}1${DEFAULT}/g;
  s/(.*P3.*)/${CYAN}${ON_GRAY235}1${DEFAULT}/g;

  s/(.*P4.*)/${GREEN}${ON_GRAY234}1${DEFAULT}/g;
  s/(.*P5.*)/${BLUE}1${DEFAULT}/g;

  " $out)
echo  "$text" | less

There, you have instant display of data from an sqlite3 table. No ruby load times.
There's also a project called ansi-color on google-code, however, you would use that if you are generating the output. Let me know if you come across any project that allows for using 256 colors in bash in a cleaner way.

Also, let me know if you know how to get rubygems to load faster. I am sure if wrote the above program as a stand-alone ruby program it would respond immediately.

Here's a sample screenshot output, it's still garish and yucky, but i did it at 2am. Will improve the colors soon.

Advertisements

Written by totalrecall

November 14, 2011 at 1:06 pm

Posted in ruby

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: