UNIX 终端应用程序中的彩色文本 [英] Color text in terminal applications in UNIX
本文介绍了UNIX 终端应用程序中的彩色文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我开始编写终端文本编辑器,类似于 UNIX 的第一个文本编辑器,例如 vi.我唯一的目标是玩得开心,但我希望能够以彩色显示文本,这样我就可以在编辑源代码时突出显示语法.
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.
我怎样才能做到这一点?是否有一些特殊的 POSIX API,或者我必须使用 ncurses?(我宁愿不要)
How can I achieve this? Is there some special POSIX API for this, or do I have to use ncurses? (I'd rather not)
有什么建议吗?也许有一些关于 UNIX API 的教科书?
Any advice? Maybe some textbooks on the UNIX API?
推荐答案
这是一个说明如何使用颜色代码的小 C 程序:
This is a little C program that illustrates how you could use color codes:
#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
", KRED);
printf("%sgreen
", KGRN);
printf("%syellow
", KYEL);
printf("%sblue
", KBLU);
printf("%smagenta
", KMAG);
printf("%scyan
", KCYN);
printf("%swhite
", KWHT);
printf("%snormal
", KNRM);
return 0;
}
这篇关于UNIX 终端应用程序中的彩色文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文