开发者

Is there a way to output text to a file in different colors on Linux?

I am outputting data to a txt file on Linux using C++. Is there a way to output a portion of a line of text in a different color?

For example, I'd like to be able to write 0.000 Watts in red.

开发者_高级运维

Total Power Usage of this model: 0.000 Watts


Plain text files (*.txt) don't support color in any way. You will have to use a different format, such as RTF or HTML.


If you are willing to embed ANSI terminal escape codes in your file then, sure.

Here is an example using Ruby for pseudocode. (The #{expr} syntax just formats expr with %s or %d and interpolates it into the string.)

def colorize s, code
  "#{code}#{s}\e[0m" 
end

def color s, n
  colorize s, "\e[#{31 + n % 7}m"
end

You could uses curses or termcap, but that would be a lot more complex and is unnecessary today, as every Linux (and Mac) terminal window is going to eat the ANSI codes with no problem.

If you then view this file or the direct output via less(1), then you will want to use less -R.


Updates: C++

#include <iostream>
using namespace std;

void color(const char *s, int n) {
  cout << "\e[" << (31 + n % 7) << "m" << s << "\e[0m";
}

int main() {
  color("How now", 0);
  color(" brown cow", 1);
  color(" Now is the", 2);
  color(" time.", 3);
  cout << endl;
  return 0;
}

And C99:

#include <stdio.h>

void colorize(const char *s, const char *code) {
  printf("%s%s\e[0m", code, s);
}

void color(const char *s, int n) {
  char t[32];

  sprintf(t, "\e[%dm", 31 + n % 7);
  colorize(s, t);
}

int main(void) {
  color("How now", 0);
  color(" brown cow", 1);
  color(" Now is the", 2);
  color(" time.\n", 3);
  return 0;
}


Best way to do this is through the curses library:

http://en.wikipedia.org/wiki/Curses_%28programming_library%29


If you just want to do simple ones, you can use code as in

http://deathray.us/code/color_output.html

const char COL_RESET[] = "\x1b[0m";
const char RED[]       = "\x1b[31m";
cout << RED << "Red looks good" << COL_RESET << endl;

more info on: http://en.wikipedia.org/wiki/ANSI_escape_code


For more information about ANSI escape codes (sometimes also known as VT100 escape codes) see http://en.wikipedia.org/wiki/ANSI_escape_code.


If you use something like vim to view your txt file. It's possible to add syntax file that match some particular words to a particular color. The good thing about this approach is that you don't have add any color information inside the txt file itself using html tags etc. However the color won't be portable since it's in your vim settings anyway.


It depends on what you're doing with your text file. If you're using cat or more (or even grep, with caveats), or otherwise outputting directly to a terminal window, you can get color by using the ANSI color codes like @DigitalRoss and several others suggest. But if you use pretty much anything else to view the file -- less, Vim, EMACS, gedit, Firefox, etc. -- there will be no colors, and instead there will be what looks like garbage in your text file.

So the answer to your question is "yes, but": Yes, there is a way to put color in your text files, but it doesn't work except when it's displayed directly, without interpretation, to a VT100-compatible terminal. So I wouldn't use control codes for a text file I wanted to give to other people, because I don't know how they'll be viewing it, and I seriously doubt they'd be using cat.

If you're intending your program to write directly to a terminal, however, go to town with the color codes. But it might be nice to add some way to turn them off, because someone else might want to redirect your program's output to a file without having to filter them out.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜