如何使用ESC序列设置前景色? [英] How to use ESC sequences to set foreground colors?

查看:99
本文介绍了如何使用ESC序列设置前景色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为控制台终端编写动态调色板。问题是要使ANSI ESC序列在默认的Wincon终端中工作就足以将这些标志设置到控制台的句柄:

I'm writing a dynamic color palette for console terminal. The thing is to get ANSI ESC sequences to work within a default wincon terminal is enough to set those flags to the handles of the console:

    DWORD dwRequestedOutModes = ENABLE_VIRTUAL_TERMINAL_PROCESSING | DISABLE_NEWLINE_AUTO_RETURN;
    DWORD dwRequestedInModes = ENABLE_VIRTUAL_TERMINAL_INPUT;

,但是使用它们是另一回事。现在,我的控制台引擎输出由 CHAR_INFO WriteConsoleOutputW()制成的屏幕缓冲区,但 CHAR_INFO 本身仅支持16种默认方案颜色。

but using them is another thing. Right now my console engine outputs screen buffer made with CHAR_INFO with WriteConsoleOutputW(), but CHAR_INFO itself supports only 16 default scheme colors.

再次使用 CONSOLE_SCREEN_BUFFER_INFOEX 限制16种颜色,但这一次是自定义的。

Using CONSOLE_SCREEN_BUFFER_INFOEX is again limited by 16 colors, but this time custom.

使用控制台ESC序列是一个很好的方法,但是问题是我发现仅使用 printf()。由于我的项目printf太慢且不可靠,是否还有其他方法可以将ESC序列分配给缓冲区中的每个符号。

Using console ESC sequences is a good point, but the problem is I've found only examples of using printf(). For my project printf is too slow and unreliable, is there any other way to assign ESC sequence to each symbol in buffer.

这里是我的代码示例:

WriteConsoleOutputW(this->m_hConsole, this->localFrame, (COORD){ (short)this->m_nScreenWidth, (short)this->m_nScreenHeight }, (COORD){ 0,0 }, &this->m_rectWindow);

绘制例程:localFrame是CHAR_INFO指针

Drawing routine: localFrame is CHAR_INFO pointer

void PrintFrameW(void* self, int x, int y, wchar_t character, short color)
{
    struct c_class* this = self;
    if (x >= 0 && x < this->nFrameLength&&y >= 0 && y < this->nFrameHeight)
    {
        this->localFrame[y*this->nFrameLength + x].Char.UnicodeChar = character;
        this->localFrame[y*this->nFrameLength + x].Attributes = color;
    }
}

它看起来像C ++代码,但这是我的

It looks like C++ code, but this one is my self made C with classes, so it is ANSI-C wrap-up made for educational purposes.

问题是:如何为课程着色。带有Escape序列的控制台的输出,以及将其放置在何处,并能够打印如下内容:

The question is: How to colorize the output of the console with Escape sequences, and where to put them, with it being able to print something like this:

使用''char(space)并将背景色设置为ESC [48; 2; r; G ; b或ESC [48; 5; s

using ' ' char(space) and setting background color to ESC [48 ; 2 ; r ; g ; b or ESC [48 ; 5 ; s

推荐答案

事实证明,可以在不进行任何检查的情况下在控制台中编写低级功能。为此,只需要两件事:char缓冲区和WriteFile()。

Turns out the low-level function can be done to write something in console without any checks. For this it's required only two things: char buffer and WriteFile().

char lfbuf[5120U] = { '\x1b','[','3','8',';','2',';','1','0','0',';','0',';','0','m','w','\0' };



        DWORD const lfbuf_length = (DWORD)(17);

        DWORD written;
        WriteFile(hOut, lfbuf, lfbuf_length, &written, NULL);

其中hOut是控制台句柄。事实证明,这是由终端机处理的,并且速度很快,因为它仅持续几千次滴答。我不得不反转整个printf()才能找到这一点。也许有些东西更快,但是WriteFile是不可逆的,当我伸手去组装时它会停止执行。

where hOut is a console handle. Turns out this one is being processed by the terminal and is fast, as it lasts only for couple thousands of ticks. I had to reverse the entire printf() to find this one out. Maybe there is something faster, but WriteFile is irreversible, it stops the execution when I reach out for assembly.

这篇关于如何使用ESC序列设置前景色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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