ANSI-着色控制台输出与.NET [英] ANSI-Coloring Console Output with .NET

查看:286
本文介绍了ANSI-着色控制台输出与.NET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用 ANSI转义码具有以下最低的C#程序来生成彩色控制台输出:

I try to generate colored console output using ANSI escape codes with the following minimal C# program:

using System;

// test.cs
class foo {
    static void Main(string[] args) {
        Console.WriteLine("\x1b[36mTEST\x1b[0m");
    }
}



我正在运行的 Ansicon V1.66与CSC.EXE(微软(R)的Visual C#编译器版本4.6.0081.0 的。)

I am running Ansicon v1.66 on Windows 7 x64 with csc.exe (Microsoft (R) Visual C# Compiler version 4.6.0081.0).

彩色输出工作在这个配置的罚款; Ansicon本身工作完美无缺。

Colored output works fine in this configuration; Ansicon itself is working flawlessly.

要交叉检查我使用Node.js的单行即相当于100%的C#程序:

To cross-check I use a node.js one-liner that is 100% equivalent to the C# program:

// test.js
console.log("\x1b[36mTEST\x1b[0m");

和,更基本的,手工制作的文本文件:

And, even more basic, a hand-crafted text file:

这两者哪个做正确意料之中的事情:打印水鸭色的字符串TEST

Both of which which correctly do the expected thing: Print a teal-colored string "TEST":

只有TEST.EXE我建有CSC打印别的东西。为什么

Only the test.exe I built with csc prints something else. Why?

推荐答案

您的程序需要被编译为 /平台:64 如果您使用的x64 AnsiCon环境以及 /平台:如果您使用AnsiCon的x86 / 32位版本的x86 。确切的原因是一个谜......

Your program needs to be compiled for /platform:x64 if you use the AnsiCon x64 environment and with /platform:x86 if you use the AnsiCon x86/32 bits version. The exact reason is a mystery...

原本我以为你需要这一切的:

您需要获取StandardOutput,让Console.WriteLine命令相信你写一个文件,而不是到控制台,使用ASCII编码。

You need to grab the StandardOutput and let the Console.WriteLine believe you write to a File instead of to a Console and use an ASCII encoding.

这是它是如何工作的:

 var stdout = Console.OpenStandardOutput();
 var con = new StreamWriter(stdout, Encoding.ASCII);
 con.AutoFlush = true;
 Console.SetOut(con);

 Console.WriteLine("\x1b[36mTEST\x1b[0m");



在.NET Console.WriteLine使用内部 __ ConsoleStream 来检查,如果在 Console.Out 是文件句柄或控制台句柄。默认情况下,它使用一个控制台手柄并为此致电 WriteConsoleW 写入控制台。在备注你发现:

The .Net Console.WriteLine uses an internal __ConsoleStream that checks if the Console.Out is as file handle or a console handle. By default it uses a console handle and therefor writes to the console by calling WriteConsoleW. In the remarks you find:

虽然应用程序可以使用WriteConsole在ANSI模式下写的ANSI字符,控制台不支持ANSI转义序列。但是,某些功能提供等同的功能。欲了解更多信息,请参阅SetCursorPos,SetConsoleTextAttribute和GetConsoleCursorInfo。

Although an application can use WriteConsole in ANSI mode to write ANSI characters, consoles do not support ANSI escape sequences. However, some functions provide equivalent functionality. For more information, see SetCursorPos, SetConsoleTextAttribute, and GetConsoleCursorInfo.

要直接写字节到控制台而不 WriteConsoleW 干扰一个简单的文件句柄/流将不被通过调用实现 OpenStandardOutput 。通过包装在的StreamWriter 该流,所以我们可以用 Console.SetOut 我们完成了再次设定。该字节序列发送到的OutputStream和AnsiCon拿起

To write the bytes directly to the console without WriteConsoleW interfering a simple filehandle/stream will do which is achieved by calling OpenStandardOutput. By wrapping that stream in a StreamWriter so we can set it again with Console.SetOut we are done. The byte sequences are send to the OutputStream and picked up by AnsiCon.

请注意,这仅与适用的终端仿真器,如AnsiCon可用,如下所示:

Do notice that this is only useable with an applicable terminal emulator, like AnsiCon, as shown here:

这篇关于ANSI-着色控制台输出与.NET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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