Ash Berlin

Posts Tagged “termcap”

Colorful Ubuntu init.d scripts

Posted on Aug 24 2010

Writing init.d scripts is fun. So much fun that its really easy to fall down rabbit holes >_>.

Looking in /lib/lsb/init-functions at the various log functions available I noticed that the status reports should be in colour - after all I’m using a terminal that is reporting as xterm-color. It turns out that the log_use_fancy_output function doesn’t care about that variable (so long as TERM isn’t reported as “dumb”) but instead looks at tput and various capabilities from the terminfo database, and more specifically hpa which sets the horizontal position to an absolute value.

And horror of horrors its missing from the standard terminfo database. If this prints something for you then you can stop reading now - HPA is working on your terminal.

ash@home:~$ tput hpa 20 && echo a

Luckily its easy enough to add by running this command:

(infocmp; printf '\thpa=\\E[%sG,\n' %i%p1%d) > $$ tmp-${$}.tic && \
  tic -s tmp-$$.tic && rm tmp-$$.tic

What this does is dumps the current terminfo, adds the capability for moving the cursor horizontally to an absolute position, and then compiles the entry descriptions to a format usable by ncurses and tput. (I used printf since the echo binary on OSX doesn’t support the -e flag to escape input.)

(If you want to do this system wide, and a -o/etc/terminfo option to the tic command.)

Now lets test it:

ash@home:~$ echo -n b && tput hpa 20 && echo a
b                   a

With any luck the init.d scripts on Ubuntu will now print [ OK ] in colour on the right hand side of your terminal.