在PowerShell控制台中使用ANSI/VT100代码输出彩色文本 [英] Colored text output in PowerShell console using ANSI / VT100 codes

查看:503
本文介绍了在PowerShell控制台中使用ANSI/VT100代码输出彩色文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个打印字符串的程序,其中包含

I wrote a program which prints a string, which contains ANSI escape sequences to make the text colored. But it doesn't work as expected in the default Windows 10 console, as you can see in the screenshot.

程序输出与转义序列一起显示为打印字符. 如果我通过变量或管道将该字符串提供给PowerShell,则输出将按预期显示(红色文本).

The program output appears with the escape sequences as printed characters. If I feed that string to PowerShell via a variable or piping, the output appears as intended (red text).

如何实现程序打印彩色文本而没有任何解决方法?

How can I achieve that the program prints colored text without any workarounds?

这是我的程序源(Haskell)-但是语言无关,只是为了您可以了解转义序列的编写方式.

This is my program source (Haskell) - but the language is not relevant, just so you can see how the escape sequences are written.

main = do
    let red = "\ESC[31m"
    let reset = "\ESC[39m"
    putStrLn $ red ++ "RED" ++ reset

推荐答案

虽然Windows 10中的控制台窗口确实支持VT(虚拟终端)/ANSI转义序列 原则上 ,默认情况下支持会关闭 .

While console windows in Windows 10 do support VT (Virtual Terminal) / ANSI escape sequences in principle, support is turned OFF by default.

您有三个选择:

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