Cygwin中的24位控制台颜色ANSI代码

问题描述 投票:2回答:1

我编写了这个简单的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();
        }
    }
}

命令提示符:Command Prompt

Windows 10中Cygwin的Cygwin命令提示符或Mintty 3.1.6:Cygwin and Mintty

Windows 10中MSYS2中Mingw64中的Mintty 3.1.6:Mintty from MSYS

c# cygwin ansi
1个回答
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;
© www.soinside.com 2019 - 2024. All rights reserved.