Printing to the terminal in color

Discussion in 'Mac Programming' started by titaniumdecoy, Jul 3, 2008.

  1. titaniumdecoy macrumors member

    Oct 13, 2005
    How can I print to the terminal in color? I have set CLICOLOR=1 so commands like ls display their output in color. How can I do this in my own command-line program?
  2. Cromulent macrumors 603


    Oct 2, 2006
    The Land of Hope and Glory
    Use the ncurses library.
  3. newb16 macrumors regular

    Feb 27, 2008
    Using ansi sequences, like echo -e "\e[33myellow" ,
    where \e is ESC (and clicolor is not required here ).
  4. titaniumdecoy thread starter macrumors member

    Oct 13, 2005
    Thanks. I am wondering, however, what built-in programs such as ls -G, vi, etc. use? Do these programs use either of the approaches mentioned?
  5. Enuratique macrumors 6502

    Apr 28, 2008
    I'm almost certain vi uses the ncurses library. As far as ls -G is concerned I don't know, it may be a bash/shell environment thing. ncurses isn't that hard to pick up - I had to use it for a lab assignment back in college for a Game of Life simulator. ncurses also has the ability to give menus and what not (think pine, putty, vi, etc) so it's not just line output, it's really a basic "windowing" system for the terminal.

Share This Page