stdlib和彩色输出

时间:2021-07-29 18:18:35

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

我正在制作一个简单的应用程序,需要彩色输出。如何使输出像emacs和bash那样着色?

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

我不关心Windows,因为我的应用程序只适用于UNIX系统。

6 个解决方案

#1


227  

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

所有现代终端模拟器都使用ANSI转义代码来显示颜色和其他东西。不要为库烦恼,代码真的很简单。

More info is here.

更多的信息在这里。

Example in C:

在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;
}

#2


11  

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.

我建议你试试使用ncurses。除了颜色,ncurses还可以用控制台UI做很多其他的事情。

#3


7  

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

编辑:我的原始版本使用了提示色码,它不能工作(这个可以(我测试过)。

#4


6  

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.

像wise一样,你可以选择不同的颜色代码,使它更通用。

#5


1  

Because you can't print a character with string formating. You can also think of adding a format with something like this

因为你不能打印一个字符串格式的字符。您还可以考虑添加这样的格式。

#define PRINTC(c,f,s) printf ("\033[%dm" f "\033[0m", 30 + c, s)

f is format as in printf

f是printf中的格式

PRINTC (4, "%s\n", "bar")

will print blue bar

将打印蓝色条

PRINTC (1, "%d", 'a')

will print red 97

将打印红97

#6


1  

If you use same color for whole program , you can define printf() function.

如果对整个程序使用相同的颜色,则可以定义printf()函数。

   #include<stdio.h>
   #define ah_red "\e[31m"
   #define printf(X) printf(ah_red "%s",X);
   #int main()
   {
        printf("Bangladesh");
        printf("\n");
        return 0;
   }

#1


227  

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

所有现代终端模拟器都使用ANSI转义代码来显示颜色和其他东西。不要为库烦恼,代码真的很简单。

More info is here.

更多的信息在这里。

Example in C:

在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;
}

#2


11  

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.

我建议你试试使用ncurses。除了颜色,ncurses还可以用控制台UI做很多其他的事情。

#3


7  

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

编辑:我的原始版本使用了提示色码,它不能工作(这个可以(我测试过)。

#4


6  

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.

像wise一样,你可以选择不同的颜色代码,使它更通用。

#5


1  

Because you can't print a character with string formating. You can also think of adding a format with something like this

因为你不能打印一个字符串格式的字符。您还可以考虑添加这样的格式。

#define PRINTC(c,f,s) printf ("\033[%dm" f "\033[0m", 30 + c, s)

f is format as in printf

f是printf中的格式

PRINTC (4, "%s\n", "bar")

will print blue bar

将打印蓝色条

PRINTC (1, "%d", 'a')

will print red 97

将打印红97

#6


1  

If you use same color for whole program , you can define printf() function.

如果对整个程序使用相同的颜色,则可以定义printf()函数。

   #include<stdio.h>
   #define ah_red "\e[31m"
   #define printf(X) printf(ah_red "%s",X);
   #int main()
   {
        printf("Bangladesh");
        printf("\n");
        return 0;
   }