using System; using System.Collections.Generic; using System.Linq; using System.Runtime.InteropServices; using System.Text; using System.Threading.Tasks; namespace BlockIPAddr.ClassObj; internal class ANSI { /* ANSI Console Color Codes 8 Colors Foreground The most basic terminals have a set of 8 different colors: Black: \u001b[30m Red: \u001b[31m Green: \u001b[32m Yellow: \u001b[33m Blue: \u001b[34m Magenta: \u001b[35m Cyan: \u001b[36m White: \u001b[37m Reset: \u001b[0m 16 Colors Foreground Most terminals, apart from the basic set of 8 colors, also support the "bright" or "bold" colors. These have their own set of codes, mirroring the normal colors, but with an additional ;1 in their codes: Bright Black: \u001b[30;1m Bright Red: \u001b[31;1m Bright Green: \u001b[32;1m Bright Yellow: \u001b[33;1m Bright Blue: \u001b[34;1m Bright Magenta: \u001b[35;1m Bright Cyan: \u001b[36;1m Bright White: \u001b[37;1m Reset: \u001b[0m Background Colors The Ansi escape codes let you set the color of the text-background the same way it lets you set the color of the foregrond. For example, the 8 background colors correspond to the codes: Background Black: \u001b[40m Background Red: \u001b[41m Background Green: \u001b[42m Background Yellow: \u001b[43m Background Blue: \u001b[44m Background Magenta: \u001b[45m Background Cyan: \u001b[46m Background White: \u001b[47m With the bright versions being: Background Bright Black: \u001b[40;1m Background Bright Red: \u001b[41;1m Background Bright Green: \u001b[42;1m Background Bright Yellow: \u001b[43;1m Background Bright Blue: \u001b[44;1m Background Bright Magenta: \u001b[45;1m Background Bright Cyan: \u001b[46;1m Background Bright White: \u001b[47;1m Decorations Apart from colors, and background-colors, Ansi escape codes also allow decorations on the text: Bold: \u001b[1m Underline: \u001b[4m Reversed: \u001b[7m Source: https://www.lihaoyi.com/post/BuildyourownCommandLinewithANSIescapecodes.html */ internal const string fgReset = "\u001b[0m"; internal const string fgBrightBlack = "\u001b[30;1m"; internal const string fgBrightRed = "\u001b[31;1m"; internal const string fgBrightGreen = "\u001b[32;1m"; internal const string fgBrightYellow = "\u001b[33;1m"; internal const string fgBrightBlue = "\u001b[34;1m"; internal const string fgBrightMagenta = "\u001b[35;1m"; internal const string fgBrightCyan = "\u001b[36;1m"; internal const string fgBrightWhite = "\u001b[37;1m"; internal const string fgNormalBlack = "\u001b[30m"; internal const string fgNormalRed = "\u001b[31m"; internal const string fgNormalGreen = "\u001b[32m"; internal const string fgNormalYellow = "\u001b[33m"; internal const string fgNormalBlue = "\u001b[34m"; internal const string fgNormalMagenta = "\u001b[35m"; internal const string fgNormalCyan = "\u001b[36m"; internal const string fgNormalWhite = "\u001b[37m"; public static class ConsoleColorization { private const int STD_OUTPUT_HANDLE = -11; private const uint ENABLE_VIRTUAL_TERMINAL_PROCESSING = 0x0004; [DllImport("kernel32.dll", SetLastError = true)] private static extern IntPtr GetStdHandle(int nStdHandle); [DllImport("kernel32.dll", SetLastError = true)] private static extern bool GetConsoleMode(IntPtr hConsoleHandle, out uint lpMode); [DllImport("kernel32.dll", SetLastError = true)] private static extern bool SetConsoleMode(IntPtr hConsoleHandle, uint dwMode); public static void Initialize() { if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { EnableAnsiEscapeSequencesOnWindows(); Console.OutputEncoding = System.Text.Encoding.UTF8; } } //Initialize private static void EnableAnsiEscapeSequencesOnWindows() { IntPtr handle = GetStdHandle(STD_OUTPUT_HANDLE); if (handle == IntPtr.Zero) { throw new Exception("Cannot get standard output handle"); } if (!GetConsoleMode(handle, out uint mode)) { throw new Exception("Cannot get console mode"); } mode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING; if (!SetConsoleMode(handle, mode)) { throw new Exception("Cannot set console mode"); } } //EnableAnsiEscapeSequencesOnWindows } //ConsoleColor }