views:

149

answers:

4

I am making a simple application which requires colored output. How can I make my output colored like emacs and bash do?

I don't care about Windows, as my application is only for UNIX systems.

Thanks.

+2  A: 

You can output special color control codes to get colored terminal output, here's a good resource on how to print colors.

For example:

printf("\033[22;34mHello, world!\033[0m");  // shows a blue hello world

EDIT: My original one used prompt color codes, which doesn't work :( This one does (I tested it).

Stephen
`edition.c: In function ‘int main(int, const char**)’:edition.c:4: error: unknown escape sequence '\]'edition.c:4: error: unknown escape sequence '\]'edition.c edition.c~`Nothing more than a bunch of compile errors :(
Time Machine
Also, my application should not be dependent on BASH.
Time Machine
@Koning : This isn't BASH dependent, it works in other shells too (but I'm sure not all). I verified in ksh, and csh. Note I edited the control code to work properly.
Stephen
It won't be. It depends on terminal emulation. If it is ANSI understanding ANSI escape sequences, then you'll have your colours, bold, or whatever.
ShinTakezou
+3  A: 

Dealing with colour sequences can get messy and different systems might use different Colour Sequence Indicators.

I would suggest you try using ncurses. Other than colour, ncurses can do many other neat things with console UI.

Moron
I am unable to find any explanation for the downvote...
Moron
A: 

You can assign one color to every functionality to make it more useful.

#define Color_Red "\33[0:31m\\]" // Color Start
#define Color_end "\33[0m\\]" // To flush out prev settings
#define LOG_RED(X) printf("%s %s %s",Color_Red,X,Color_end)

foo()
{
LOG_RED("This is in Red Color");
}

Like wise you can select different color codes and make this more generic.

Praveen S
+6  A: 

All modern terminal emulators use ANSI escape codes to show colours and other things.
Don't bother with libraries, the code is really simple.

More info is here.

Example in C:

#include <stdio.h>

#define ANSI_COLOR_RED     "\x1b[31m"
#define ANSI_COLOR_GREEN   "\x1b[32m"
#define ANSI_COLOR_YELLOW  "\x1b[33m"
#define ANSI_COLOR_BLUE    "\x1b[34m"
#define ANSI_COLOR_MAGENTA "\x1b[35m"
#define ANSI_COLOR_CYAN    "\x1b[36m"
#define ANSI_COLOR_RESET   "\x1b[0m"

int main (int argc, char const *argv[]) {

  printf(ANSI_COLOR_RED     "This text is RED!"     ANSI_COLOR_RESET "\n");
  printf(ANSI_COLOR_GREEN   "This text is GREEN!"   ANSI_COLOR_RESET "\n");
  printf(ANSI_COLOR_YELLOW  "This text is YELLOW!"  ANSI_COLOR_RESET "\n");
  printf(ANSI_COLOR_BLUE    "This text is BLUE!"    ANSI_COLOR_RESET "\n");
  printf(ANSI_COLOR_MAGENTA "This text is MAGENTA!" ANSI_COLOR_RESET "\n");
  printf(ANSI_COLOR_CYAN    "This text is CYAN!"    ANSI_COLOR_RESET "\n");

  return 0;
}
Andrejs Cainikovs
Nice wiki link for color codes.
Praveen S
But I suppose I can run bash in a terminal emulation that does not support ANSI escape seqs.
ShinTakezou
Nice flag :) Just a nit that the question is tagged 'c', not 'c++'
Stephen
@ShinTakezou: Yes.. In this case all the ANSI escape codes will be ignored, and you will have no options to make the fancy output.
Andrejs Cainikovs
@Stephen: Ok, I'm lazy right now, but I will port it :-)
Andrejs Cainikovs
ANSI escape sequences are not interpreted by shells, they are interpreted by terminal emulators.
ninjalj
@ninjalj: Correct. I've updated the answer.
Andrejs Cainikovs
Will this work in xterm? What happens if I change my terminal settings and run this code? Seems pretty silly to do your own terminal escaping when you dont have to. Anyway, might well be good enough for OP.
Moron
@Moron: Yes, it will work with xterm. Indeed, ANSI escaping looks somewhat non-trivial, but this is the way how it works :-)
Andrejs Cainikovs
@Andrejs: I know how it works. My claim is that this approach is not a portable enough method and very restrictive (work only with ANSI compatible terminals). Just because the code is 'simple' does not mean you have to use it. Add a few more terminals to support and the code will become complicated and hard to maintain. It is best to use an already available library. In fact even with ANSI, the CSI might be different between terminals, so it might not even work will all ANSI compatible terminals (not so sure about this though).
Moron
kthx this is awesome =D
Time Machine
@Moron: I thought real CSI (0x9b) is not recognized by some terminal emulators, but any sane mostly ANSI/ECMA-48 compatible terminal emulator recognized 7-bit CSI (ESC [)?
ninjalj
@ninjalj: Probably, but like I said, I am not very sure.
Moron