ANSI-Coloring Console输出与.NET [英] ANSI-Coloring Console Output with .NET

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

问题描述

我尝试使用



这两种方法都能正确执行预期的操作:打印 teal - 颜色字符串TEST:





只有使用csc构建的test.exe才会打印其他内容。为什么?

解决方案

您的程序需要编译为 / platform:x64 如果您使用AnsiCon x86 / 32位版本,则使用AnsiCon x64环境和 / platform:x86 确切的原因是一个谜... ...



$ b

原来我认为你需要所有这些: b

您需要抓取StandardOutput并让Console.WriteLine相信您写入文件而不是控制台并使用ASCII编码。



这是如何工作:

  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 是作为文件句柄还是控制台句柄。默认情况下,它使用控制台句柄,并通过调用


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");
    }
}

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

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

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:

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

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

解决方案

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...

Originally I thought you need all this:

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.

This is how it will work:

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

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

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:

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.

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.

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

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

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