在UNIX终端应用彩色文本 [英] Color text in terminal applications in UNIX

查看:229
本文介绍了在UNIX终端应用彩色文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始写终端文本编辑器,就像第一个文本编辑器UNIX,如vi东西。我唯一​​的目标就是有一个好时机,但我希望能够以显示彩色文本,这样我就可以有语法高亮编辑源$ C ​​$ C。

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程序,说明了如何使用颜色codeS:

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

这篇关于在UNIX终端应用彩色文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆