我编写了这个简单的C#/。NET Core控制台应用程序代码,该代码输出一组7x7x7的颜色立方体,测试24位颜色而不是256色模式,以及我使用的自定义TTF字体,该字体源自“ Ultimate Old School PC Font Pack”,其中包括一些额外的Unicode块字符。
如所见,它在Windows 10终端中运行良好,但在Cygwin中却是坦克,即使Github(https://gist.github.com/XVilka/8346728)应该支持它也是如此。
如果在代码中使用[38m或[48m]代码的某些内容可能与使用Cygwin.bat或Cygwin的mintty的Cygwin不那么兼容,那么关于什么地方出问题的任何想法?
但是,从第三张图中可以看出,在Mingw64 / MSYS2的Mintty中看起来还不错,但是我更喜欢使用Cygwin。即使您将薄荷糖中的字符集设置为UTF-8,它也会以某种方式扭曲第三张图片中的三角形字符。
using System;
using System.Text;
namespace ConsoleColor
{
public class App
{
//int[] colorMeta1 = { 0, 85, 170, 255 };
int[] colorMeta2 = { 0, 43, 85, 127, 170, 213, 255 };
public App()
{
int length = colorMeta2.Length;
int index = 0;
Encoding defaultEncoder = Console.OutputEncoding;
Console.OutputEncoding = Encoding.UTF8;
for (int r=0;r<length;r++)
{
for(int g=0;g<length;g++)
{
for(int b=0;b<length;b++)
{
int r2 = colorMeta2[r];
int g2 = colorMeta2[g];
int b2 = colorMeta2[b];
int foregroundColor = (r2 == 255 || g2 == 255 || b2 == 255) ? 0 : 255;
Console.Write($"\u001b[38;2;{r2};{g2};{b2}m█");
Console.Write($"\u001b[38;2;{foregroundColor};{foregroundColor};{foregroundColor}m\u001b[48;2;{r2};{g2};{b2}m({r2.ToString().PadLeft(3, ' ')},{g2.ToString().PadLeft(3, ' ')},{b2.ToString().PadLeft(3, ' ')})");
Console.Write($"\u001b[38;2;{r2};{g2};{b2}m█");
Console.Write($"\u001b[38;2;{170};{170};{170}m");
Console.Write($"\u001b[48;2;{0};{0};{0}m");
index++;
}
Console.WriteLine();
}
}
Console.WriteLine($"{index} total colors.");
for (int a = 0x2580; a <= 0x259F; a++)
Console.Write($"{(char)a}");
for (int a = 0x25E2; a <= 0x25E5; a++)
Console.Write($"{(char)a}");
Console.WriteLine();
Console.OutputEncoding = defaultEncoder;
}
}
class Program
{
static void Main(string[] args)
{
App app = new App();
}
}
}
答案 0 :(得分:1)
以下博客文章显示了需要做的事情。显然,仍然需要在Windows 10的.NET Core控制台应用程序中进行一些win32调用,才能使Cygwin / Mintty与它们正常工作。
https://www.jerriepelser.com/blog/using-ansi-color-codes-in-net-console-apps/
代码:
private const int STD_OUTPUT_HANDLE = -11;
private const uint ENABLE_VIRTUAL_TERMINAL_PROCESSING = 0x0004;
private const uint DISABLE_NEWLINE_AUTO_RETURN = 0x0008;
[DllImport("kernel32.dll")]
private static extern bool GetConsoleMode(IntPtr hConsoleHandle, out uint lpMode);
[DllImport("kernel32.dll")]
private static extern bool SetConsoleMode(IntPtr hConsoleHandle, uint dwMode);
[DllImport("kernel32.dll", SetLastError = true)]
private static extern IntPtr GetStdHandle(int nStdHandle);
[DllImport("kernel32.dll")]
public static extern uint GetLastError();
...
public void ConsoleInit()
{
var iStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
if (!GetConsoleMode(iStdOut, out uint outConsoleMode))
{
Console.WriteLine("failed to get output console mode");
Console.ReadKey();
return;
}
outConsoleMode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING | DISABLE_NEWLINE_AUTO_RETURN;
if (!SetConsoleMode(iStdOut, outConsoleMode))
{
Console.WriteLine($"failed to set output console mode, error code: {GetLastError()}");
Console.ReadKey();
return;
}
}
这一行特别重要:
outConsoleMode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING | DISABLE_NEWLINE_AUTO_RETURN;