0

I have an intel based embedded target system and Linux is running.

I saw that kernel commands' font color of screen output (in telnet-console) is adjusted automatically. For example, if xterm console background is light color, texts are black, and for black background console, texts are white.

I uploaded my application in c and run on the Linux prompt. Font color is fixed black so that I cannot see any printf messages on the black background xterm.

Can anyone tell me how to adjust in c program dynamically?

windflow
  • 3
  • 5
  • 1
    You don't, instead you change the settings of your terminal to either change background color or to change font color. – Some programmer dude Nov 26 '14 at 19:12
  • Probably the most solid approach is using the `ncurses` library for output. The quick and dirty is [using ANSI escape codes](http://stackoverflow.com/questions/7414983/how-to-use-the-ansi-escape-code-for-outputting-colored-text-on-console). – Paulo Scardine Nov 26 '14 at 19:14

1 Answers1

0

Check this site for color codes: http://misc.flogisoft.com/bash/tip_colors_and_formatting

And here is an example how you can use it.

#include <stdio.h>

int foreground[] = {
  39, 30, 31, 32, 33, 34,
  35, 36, 37, 90, 91, 92,
  93, 94, 95, 96, 97
};

int background[] = {
  49, 40, 41, 42, 43, 44,
  45, 46, 47, 100, 101, 102,
  103, 104, 105, 106, 107
};

int main() {
  int flen = sizeof(foreground)/sizeof(int);
  int blen = sizeof(background)/sizeof(int);

  char fcolor[10];
  char bcolor[10];

  char dfbcolor[] = "\e[39m\e[49m"; // default foreground and background color

  for (int i = 0; i < flen; i++) {
    for (int j = 0; j < flen; j++) {
      sprintf(fcolor, "\e[%dm", foreground[i]);
      sprintf(bcolor, "\e[%dm", background[j]);
      printf("%s%shello, world%s\n", fcolor, bcolor, dfbcolor);
    }
  }

  return 0;
}

int arrays foreground and background are color codes for foreground and background that I found in table on the site I gave you.

Have fun :)