ANSI逃生代码无法正常工作[C+]

发布于 2025-02-08 18:31:43 字数 1229 浏览 3 评论 0原文

我正在使用ANSI逃生代码打印彩色输出。

我在集成终端中获得了适当的彩色输出。

但是当我在外部命令prompt/powershell中运行该程序时,我没有得到预期的彩色输出。

我的程序看起来像这样:


#define RESET   "\033[0m"
#define RED  "\x1B[31m"
#define GREEN  "\x1B[32m"

int main(int argc, char** argv){
    if(argc != 1){
        std::cout << RED ">> Invalid Arguments!" RESET;
    }else{
        if(verify_password()){
            ...
            ...
            ...
        }else{
            std::cout <<  "\n>> " RED "Invalid Password!" RESET;
        }
    }
    return 0;
}

完整的代码

注意:我观察到的一件很奇怪的事情是,如果我输入正确的密码,那么两个终端中的一切正常(获得正确的颜色)。但是问题是当我输入不正确的密码或无效的参数时,

这可能是什么原因?

编辑:我想出了一种使它起作用的方法。我发现,要使这些逃生代码工作,我需要至少一个呼叫system()函数。但是,我不确定这些事情是如何联系的。

I am using the ANSI escape code to print colored output.

I am getting proper colored output in vs code integrated terminal.
enter image description here

But when I am running the program in external command-prompt/Powershell, I am not getting the expected colored output.

enter image description here

My program looks something like this:


#define RESET   "\033[0m"
#define RED  "\x1B[31m"
#define GREEN  "\x1B[32m"

int main(int argc, char** argv){
    if(argc != 1){
        std::cout << RED ">> Invalid Arguments!" RESET;
    }else{
        if(verify_password()){
            ...
            ...
            ...
        }else{
            std::cout <<  "\n>> " RED "Invalid Password!" RESET;
        }
    }
    return 0;
}

Complete Code

NOTE: One weird thing I observed is that if I am entering the correct password then everything is working fine in both terminals(getting proper colors). But the problem is when either I am entering an incorrect password or an invalid amount of arguments

What might be the reason for this?

EDIT: I figured out a way to make it work. I found that in order to make these escape codes work I need to have at least one call to system() function. But still, I am not sure how these things are connected.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凉墨 2025-02-15 18:31:43

从历史上看,窗户上的控制台需要使用 console api> console api Windows SDK为了做诸如颜色之类的效果。但是Windows的最新版本现在支持Escape代码(甚至UTF-8),但是程序必须通过调用带有enable_virtual_terminal_processing的setConsolemode 。 (选择不使用逃生代码或UTF-8的较旧程序的向后兼容性。)

如果您在某些地方而不是其他地方获得颜色,那么我猜想与状态有一个问题,终端/发送逃生代码时,控制台就在。例如,如果终端认为它已经在处理逃生代码,而新的代码也可能无法识别。我想如果程序的一个使用逃生代码,但另一部分使用控制台API,这也可能是一个问题。

Historically, consoles on Windows required use of the console API in the Windows SDK in order to do effects like color. But recent versions of Windows now support escape codes (and even UTF-8), but programs have to opt-in by calling SetConsoleMode with ENABLE_VIRTUAL_TERMINAL_PROCESSING. (Opting in preserves backward compatibility for older programs that aren't using escape codes or UTF-8.)

If you're getting color in some places and not others, then I'd guess there's a problem related to the state the terminal/console is in when sending the escape codes. For example, if the terminal thinks it's already processing an escape code and a new one begins, it might not recognize either. I suppose this might also be a problem if one part of the program uses escape codes but another part uses the Console API.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文