#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2003
    Posts
    4
    Rep Power
    0

    Question writing with colors in linux


    i looked everywere, but i havent found simple solutions to this problem.

    i want the output of printf to be colorful, and not gray. i have looked for answers in google, but these answers seemed to be to complicated...

    so basicly , my question is , is there a way to write in colors using a simple function or something?
  2. #2
  3. *bounce*
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2002
    Location
    Delft, The Netherlands
    Posts
    514
    Rep Power
    42
    I don't know the whole thing behind it, but after disecting some BASH shell prompts, I more or less figured out how it works. Here's a proof-of-concept:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int
    main (int argc, char **argv)
    {
            printf("\033[1;32mHello World!\033[0m\n");
    }
    The \033 sequences denote the escape character. You can seperate ANSI control commands with semicolons, and the [ and 'm' are used to signal the start and end of an ANSI control sequence.

    So basically the first bit, \033[1;32m, contains two ansi control commands, 1 and 32. 1 selects boldface, 32 selects green.

    Similarly, the ANSI control command at the end, \033[0m, contains just the 0 command, which turns off all ANSI controls (giving you boring grey again :))

    There are a few colors, somewhere between eight and sixteen. Try writing a for-loop that runs through 30-39 or something, that should give you an idea.

    Good luck :)
    "A poor programmer is he who blames his tools."
    http://analyser.oli.tudelft.nl/
  4. #3
  5. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,156
    Rep Power
    2222
    So bash supports the ANSI display driver. The last I saw of that was on my first PC (an XT circa 1984); I never used it because it clashed disasterously with a utility that came with the PC.

    If you are going to use that a lot, you could create a library wrapper functions that you would call with easy and meaningful names and that would output or insert the ANSI codes needed. Or do a search to see if it's already been done.

    And to think I considered suggesting curses or ncurses.
  6. #4
  7. *bounce*
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2002
    Location
    Delft, The Netherlands
    Posts
    514
    Rep Power
    42
    Well, there is a big plus to using curses over some ANSI controls:

    The terminal you're using needs to support it. The linux terminal does, as do some other terminal types. But a whole lot of them don't.

    So yes, you can use ANSI directly, but your software won't run on properly on a lot of *nix boxen. Curses and ncurses were written precisely to tackle the problem of having to program for all those different terminals.
    "A poor programmer is he who blames his tools."
    http://analyser.oli.tudelft.nl/
  8. #5
  9. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2003
    Posts
    4
    Rep Power
    0
    mkay , thanks for the help ppl!
  10. #6
  11. not a fan of fascism (n00b)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Feb 2003
    Location
    ct
    Posts
    2,756
    Rep Power
    95
    stumbled across a very in depth answer to ur problem while teaching myself some scripting techiques, check out this link:

    http://www.tldp.org/LDP/abs/html/colorizing.html

IMN logo majestic logo threadwatch logo seochat tools logo