如何使用ANSI Escape代码在Console上输出彩色文本

时间:2021-08-07 03:07:16

I read about ANSI-C escape codes here. Tried to use it in C/C++ printf/cout to colorize the text outputted to consolde but without sucess.

我在这里阅读有关ANSI-C转义码的内容。试图在C / C ++ printf / cout中使用它来为输出的文本着色,但没有成功。

Code:

#include <iostream>

 #include <cstdio>

int main()
{

    int a=3, b=5;
    int &ref = a;

    ref = b;

    //cout << "\155\32\m" << a << b <<'\n'; //here it prints m→m 5, no colored text
    printf("\155\32\m %d",a); //here to it prints same - m→m 5, 

    getchar();

}

How to use these escape codes to output colored text to console?

如何使用这些转义码将彩色文本输出到控制台?

Am i missing something?

我错过了什么吗?

EDIT: In some C++ code I saw a call to this function

编辑:在一些C ++代码中,我看到了对此函数的调用

textcolor(10);

But it gives compilation errors in g++ and in Visual Studio. Which compiler had this function available? Any details?

但它在g ++和Visual Studio中提供了编译错误。哪个编译器有此功能?任何细节?

5 个解决方案

#1


8  

I'm afraid you forgot the ESC character:

我怕你忘记了ESC的角色:

#include <cstdio>

int main()
{
    printf("%c[%dmHELLO!\n", 0x1B, 32);
}

Unfortunately it will only work on consoles that support ANSI escape sequences (like a linux console using bash, or old Windows consoles that used ansi.sys)

不幸的是,它只适用于支持ANSI转义序列的控制台(比如使用bash的linux控制台,或使用ansi.sys的旧Windows控制台)

#2


6  

I created a very simple text-management library some time ago, being multiplatform, it uses native API calls for Windows and ANSI escape sequences for the rest of the platforms. It is fully documented and you can also browse the source code.

我不久前创建了一个非常简单的文本管理库,它是多平台的,它使用Windows的本机API调用和其他平台的ANSI转义序列。它已完整记录,您还可以浏览源代码。

About your specific question, I think you are missing some codes. For example, in order to change the color of text, you should use something like:

关于您的具体问题,我认为您缺少一些代码。例如,为了更改文本的颜色,您应该使用以下内容:

static const char * CSI = "\33[";
printf( "%s%s", CSI, "31m" );   // RED

Hope this helps.

希望这可以帮助。

#3


3  

ANSI escape codes worked on DOS using the ansi.sys device driver. They won't work windows xp or higher. You need to use the console API SetConsoleTextAttribute()

ANSI转义代码使用ansi.sys设备驱动程序在DOS上工作。它们不能在windows xp或更高版本上运行。您需要使用控制台API SetConsoleTextAttribute()

textcolor was available in the borland turbo c++ compiler.

textcolor在borland turbo c ++编译器中可用。

#4


1  

ANSI formatting codes aren't supported in windows.

Windows中不支持ANSI格式代码。

http://en.wikipedia.org/wiki/ANSI_escape_code

#5


0  

A note to anybody reading this post: Re-read https://en.wikipedia.org/wiki/ANSI_escape_code#Windows "In 2016 with Windows 10 "Threshold 2" Microsoft unexpectedly started supporting ANSI escape sequences in the console app, making the porting of software from (or remote access to) Unix much easier. " Windows does support ANSI codes now, even if only on newer versions.

任何阅读这篇文章的人都要注意:重新阅读https://en.wikipedia.org/wiki/ANSI_escape_code#Windows“2016年Windows 10”阈值2“微软意外开始在控制台应用程序中支持ANSI转义序列,从(或远程访问)Unix更容易移植软件。“Windows现在支持ANSI代码,即使只是在新版本上。

#1


8  

I'm afraid you forgot the ESC character:

我怕你忘记了ESC的角色:

#include <cstdio>

int main()
{
    printf("%c[%dmHELLO!\n", 0x1B, 32);
}

Unfortunately it will only work on consoles that support ANSI escape sequences (like a linux console using bash, or old Windows consoles that used ansi.sys)

不幸的是,它只适用于支持ANSI转义序列的控制台(比如使用bash的linux控制台,或使用ansi.sys的旧Windows控制台)

#2


6  

I created a very simple text-management library some time ago, being multiplatform, it uses native API calls for Windows and ANSI escape sequences for the rest of the platforms. It is fully documented and you can also browse the source code.

我不久前创建了一个非常简单的文本管理库,它是多平台的,它使用Windows的本机API调用和其他平台的ANSI转义序列。它已完整记录,您还可以浏览源代码。

About your specific question, I think you are missing some codes. For example, in order to change the color of text, you should use something like:

关于您的具体问题,我认为您缺少一些代码。例如,为了更改文本的颜色,您应该使用以下内容:

static const char * CSI = "\33[";
printf( "%s%s", CSI, "31m" );   // RED

Hope this helps.

希望这可以帮助。

#3


3  

ANSI escape codes worked on DOS using the ansi.sys device driver. They won't work windows xp or higher. You need to use the console API SetConsoleTextAttribute()

ANSI转义代码使用ansi.sys设备驱动程序在DOS上工作。它们不能在windows xp或更高版本上运行。您需要使用控制台API SetConsoleTextAttribute()

textcolor was available in the borland turbo c++ compiler.

textcolor在borland turbo c ++编译器中可用。

#4


1  

ANSI formatting codes aren't supported in windows.

Windows中不支持ANSI格式代码。

http://en.wikipedia.org/wiki/ANSI_escape_code

#5


0  

A note to anybody reading this post: Re-read https://en.wikipedia.org/wiki/ANSI_escape_code#Windows "In 2016 with Windows 10 "Threshold 2" Microsoft unexpectedly started supporting ANSI escape sequences in the console app, making the porting of software from (or remote access to) Unix much easier. " Windows does support ANSI codes now, even if only on newer versions.

任何阅读这篇文章的人都要注意:重新阅读https://en.wikipedia.org/wiki/ANSI_escape_code#Windows“2016年Windows 10”阈值2“微软意外开始在控制台应用程序中支持ANSI转义序列,从(或远程访问)Unix更容易移植软件。“Windows现在支持ANSI代码,即使只是在新版本上。