简体   繁体   中英

How to adjust font color for xterm in Linux application in c

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?

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 :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM