Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!


Click here to return to the 'better highlight' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
better highlight
Authored by: mzs on Jun 29, '04 03:38:36PM
Wow thanks wallybear!

I do not use Geektool, but I like your clever awk scripts just the same. Why use perl when awk will do :) I will have to create a little script from your post.

From the command-line I have a little shell script snippet I use in my scripts for text standout:


if tty -s <&1; then
        rev=`tput rev`
        sgr0=`tput sgr0`
else
        rev="_^H"
        sgr0=""
fi
This is a bourne shell snippet of course. What does this snippet do? The test redirects STDOUT to STDIN and tests to see if it is a terminal. This will do the right thing when the command is invoked in a pipeline depending on whether this was the last command or not.

In the case of a terminal do the tput commands. rev is the escape sequence for reverse video mode and sgr0 is for standard graphics mode. (the default for a terminal) The tput command uses curses to see what the right codes are for your current terminal.

Over the years I have learned a few tricks here. What you really want is:


        rev=`NCURSES_NO_PADDING=1 tput rev`
        sgr0=`NCURSES_NO_PADDING=1 tput sgr0`
I have seen some really horrible terminal DBs, and the environment variable does not hurt if you are using a historical curses either.

Also, I used to do a test of the exit status of the tput command, something like if tput os; then ... to see if overstrike was available. I learned the hard way that different systems do different things to the exit status for the tput command, so I just do not check and use reverse video mode instead.

Phew, now onto the other case, not a terminal. In that case I assume that you are doing a pipe, using a pager, or a hardcopy. I just do the underline for hardcopy device. The trick to getting a backspace into a file is to do this command from a shell:


cat >filename
Now type Ctrl+V Ctrl+H Return Ctrl+D Then just copy and paste the odd character where ever you need it in whatever editor you happen to use for the ^H BS character. Another thing that is useful is to use bold mode. On old daisy-wheel printers this was naturally character backspace character but the simple snippet I have above will not do here because you have to duplicate each character and that depends on what it actually is. Specifically, A is A^HA while B is B^HB.

Now in awk I do some thing like this whenever I need a standout character:


printf("%s%c%s", rev, c, sgr0)
Inline in a shell script just do the appropriate quoting and "$rev" and "$sgr0". To get this to work right in all cases you need to escape every character you wish to standout because the old hardcopy trick only affects one character. That is often the simplest thing to do and does not make a difference for simple shell scripts. Of course you can be fancy and check to see if the length of rev is non-zero and do other more complicated things like grouping all of the standout character in one rev/sgr0 pair in the case of a terminal if you care to.

I typically invoke these in an awk script like this:


/usr/bin/awk "$foo" rev="$rev" sgr0="$sgr0"
Now what if you do not want any standout characters, say you are outputting to a log file. That is simple enough, just put a col -bx at the end of your pipeline. That will strip-off the standout characters. That col command is very handy, it is a quick way to get nicely readable text files from a man command without all the embellishment in case you do not like the pretty printing in postscript output, and you do not have to care about whether it is a historical roff or GNU roff on the system you are using. Also, piping into the less command gives very good results, with whatever you wanted in standout mode appearing either as reverse video or underlined depending on your terminal configuration.

Finally I just noticed this about the posted hint that was the start of this long comment. My hunch is that the 'echo backticks' are extraneous in the original hint posted by wallybear :)

[ Reply to This | # ]