0

I am writing a user command as an alias that use awk printing output in different colors.

The little snippet I came up with works fine in KDE Konsole but not in xterm or gnome-terminal.

My shell is a BASH on SLES11 and the terminal is set to XTERM

myuser@myhost:~> env |grep -i term
TERM=xterm
COLORTERM=1

The alias is defined as follows:

alias myCustomPS="\
   awk '{ \
      gsub(/\.CONTEXT/,\"\", \$11);  \
      split(\$11,v,\"_D\");  \
      split(\$13,h,\".\"); \
      if (\$1==\"usr1\") 
         printf \"pid [\033[1;37;48;2;0;0;128m %5s \033[0m] [\033[1;37;48;2;0;0;128m %.8s \033[0m] \033[1;37;48;2;0;0;128m %-8.10s \033[0m D%-8s\n\", \$2, \$1, h[1], v[2]; \
      else if (\$1==\"usr2\") \
         printf \"pid [\033[1;37;48;2;0;170;170m %5s \033[0m] [\033[1;37;48;2;0;170;170m %.8s \033[0m] \033[1;37;48;2;0;170;170m %-8.10s \033[0m D%-8s\n\", \$2, \$1, h[1], v[2]; }'"

the output I get is displaying correctly ONLY in Konsole.

Both xterm and gnome-terminal instead don't show it correctly. In Xterm no color at all is shown, in gnome-terminal only the font color is shown, no background.

any idea, or alternative ways to universally colorize the awk output? Thanks, Roberto

rpalmari
  • 1
  • 3
  • This is really hard to test without representative sample input. Could you provide some, or perhaps pare down the script to a bare minimum which attempts to colorize any random piece of text? – tripleee Feb 19 '15 at 10:18
  • Don't hard-code escape sequences. Use `tput` or similar to query the terminal for the sequence to use for the current terminal type. I'm going to assume that those sequences just aren't correct for xterm or gnome-terminal. I know that at least in supported colors `Konsole` supposedly has much broader support for what colors are available (any arbitrary color actually I believe). – Etan Reisner Feb 19 '15 at 11:11
  • If i remember correct you need to set TERM to xterm-color or something similar to get colors in xterm, try to look at http://stackoverflow.com/questions/10003136/what-is-the-difference-between-xterm-color-xterm-256color – rasmusm Feb 19 '15 at 13:45
  • You can't feel good about all that escaping - put this in a function or a shell script, not an alias. – Ed Morton Feb 19 '15 at 13:55
  • Well the alias is indeed defined into a shell script and used in the definition of a function in that same shell script. I'll try to make it much simpler and provide a usable snippet. – rpalmari Feb 19 '15 at 15:33

1 Answers1

1

The snippet uses the RGB flavor "2" in the escape sequence, which should work with xterm after patch #282 (which seems a little old, but SLSE 11 is a few months older than that).

If you must hardcode things, the RGB flavor "5" is more portable. But portable applications use something like tput rather than relying on constants.

The "2" referred to is in this string (repeated several times):

\033[1;37;48;2;0;0;128m
             ^

and is mentioned in XTerm Control Sequences in the paragraph discussing ISO-8613-6.

Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105