C语言 UNIX 终端应用程序中的彩色文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3585846/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 06:19:12  来源:igfitidea点击:

Color text in terminal applications in UNIX

cunixterminal

提问by Nisanio

I started to write a terminal text editor, something like the first text editors for UNIX, such as vi. My only goal is to have a good time, but I want to be able to show text in color, so I can have syntax highlighting for editing source code.

我开始编写终端文本编辑器,类似于 UNIX 的第一个文本编辑器,例如 vi。我唯一的目标是玩得开心,但我希望能够以彩色显示文本,这样我就可以在编辑源代码时突出显示语法。

How can I achieve this? Is there some special POSIX API for this, or do I have to use ncurses? (I'd rather not)

我怎样才能做到这一点?是否有一些特殊的 POSIX API,或者我必须使用 ncurses?(我宁愿不)

Any advice? Maybe some textbooks on the UNIX API?

有什么建议吗?也许有一些关于 UNIX API 的教科书?

回答by karlphillip

This is a little C program that illustrates how you could use color codes:

这是一个小 C 程序,用于说明如何使用颜色代码:

#include <stdio.h>

#define KNRM  "\x1B[0m"
#define KRED  "\x1B[31m"
#define KGRN  "\x1B[32m"
#define KYEL  "\x1B[33m"
#define KBLU  "\x1B[34m"
#define KMAG  "\x1B[35m"
#define KCYN  "\x1B[36m"
#define KWHT  "\x1B[37m"

int main()
{
    printf("%sred\n", KRED);
    printf("%sgreen\n", KGRN);
    printf("%syellow\n", KYEL);
    printf("%sblue\n", KBLU);
    printf("%smagenta\n", KMAG);
    printf("%scyan\n", KCYN);
    printf("%swhite\n", KWHT);
    printf("%snormal\n", KNRM);

    return 0;
}

回答by David Guyon

Different solution that I find more elegant

我发现更优雅的不同解决方案

Here's another way to do it. Some people will prefer this as the code is a bit cleaner. There are no %sand a RESETcolor to end the coloration.

这是另一种方法。有些人会更喜欢这个,因为代码更简洁一些。没有%sRESET结束着色的颜色。

#include <stdio.h>

#define RED   "\x1B[31m"
#define GRN   "\x1B[32m"
#define YEL   "\x1B[33m"
#define BLU   "\x1B[34m"
#define MAG   "\x1B[35m"
#define CYN   "\x1B[36m"
#define WHT   "\x1B[37m"
#define RESET "\x1B[0m"

int main() {
  printf(RED "red\n"     RESET);
  printf(GRN "green\n"   RESET);
  printf(YEL "yellow\n"  RESET);
  printf(BLU "blue\n"    RESET);
  printf(MAG "magenta\n" RESET);
  printf(CYN "cyan\n"    RESET);
  printf(WHT "white\n"   RESET);

  return 0;
}

This program gives the following output:

该程序提供以下输出:

enter image description here

在此处输入图片说明



Simple example with multiple colors

具有多种颜色的简单示例

This way, it's easy to do something like:

这样,很容易执行以下操作:

printf("This is " RED "red" RESET " and this is " BLU "blue" RESET "\n");

This line produces the following output:

此行产生以下输出:

execution's output

执行的输出

回答by nmichaels

You probably want ANSI color codes. Most *nix terminals support them.

您可能需要ANSI 颜色代码。大多数 *nix 终端都支持它们。

回答by Vivin Paliath

Use ANSI escape sequences. This articlegoes into some detail about them. You can use them with printfas well.

使用 ANSI 转义序列。本文详细介绍了它们。您也可以使用它们printf