May 19, 2010

Add color to your linux console program

You can add color to your linux console print outs, such as usage.

#define CSI "\x1B\x5B"
printf("%s1;31m This is color text %s0m\n",CSI,CSI);

red text:
printf "\x1b\x5b1;31m Ignoring callcatcher \x1b\x5b0m\n";

green text:
printf "\x1b\x5b2;31m Ignoring callcatcher \x1b\x5b0m\n";

\x1B is Escape Char, \x5B is '['. They form the control string indicator.

1;31 are two commands
1 means bold color
31-37 are the colors

m: set display attributes command

0m: reset to normal display

color code list:
   Black      0;30       Dark Gray    1;30
  Red        0;31       Bold Red     1;31
  Green      0;32       Bold Green   1;32
  Yellow     0;33       Bold Yellow  1;33
  Blue       0;34       Bold Blue    1;34
  Purple     0;35       Bold Purple  1;35
  Cyan       0;36       Bold Cyan    1;36

  Light Gray 0;37       White        1;37

Other commands (from Wikipedia)
Code Effect Note
0 Reset / Normal all attributes off
1 Bright (increased intensity) or Bold
2 Faint (decreased intensity) not widely supported
3 Italic: on not widely supported. Sometimes treated as inverse.
4 Underline: Single
5 Blink: Slow less than 150 per minute
6 Blink: Rapid MS-DOS ANSI.SYS; 150 per minute or more
7 Image: Negative inverse or reverse; swap foreground and background
8 Conceal not widely supported
9 Crossed-out Characters legible, but marked for deletion.
10 Primary(default) font
11-19 n-th alternate font Select the n-th alternate font. 14 being the fourth alternate font, up to 19 being the 9th alternate font.
20 Fraktur
21 Underline: Double not widely supported
22 Normal color or intensity neither bright, bold nor faint
23 Not italic, not Fraktur
24 Underline: None not singly or doubly underlined
25 Blink: off
26 Reserved
27 Image: Positive
28 Reveal conceal off
29 Not crossed out
30–37 Set text color 3x, where x is from the color table below
38 Reserved
39 Default text color implementation defined (according to standard)
40–47 Set background color 4x, where x is from the color table below
48 Reserved
49 Default background color implementation defined (according to standard)
50 Reserved
51 Framed
52 Encircled
53 Overlined
54 Not framed or encircled
55 Not overlined
56-59 Reserved
60 ideogram underline or right side line
61 ideogram double underline or double line on the right side
62 ideogram overline or left side line
63 ideogram double overline or double line on the left side
64 ideogram stress marking
90–99 Set foreground color, high intensity aixterm (not in standard)
100–109 Set background color, high intensity aixterm (not in standard)

No comments:

Post a Comment