using System; using System.Runtime.InteropServices; namespace PureCrack.Util; internal static class Log { private static readonly object Lock = new object(); private static readonly bool UseColor = !Console.IsOutputRedirected && TryEnableVirtualTerminal(); private const string Reset = "\u001b[0m"; private const string Bold = "\u001b[1m"; private const string Red = "\u001b[91m"; private const string Green = "\u001b[92m"; private const string Yellow = "\u001b[93m"; private const string Blue = "\u001b[94m"; private const string Gray = "\u001b[90m"; private const int STD_OUTPUT_HANDLE = -11; private const uint ENABLE_VIRTUAL_TERMINAL_PROCESSING = 4u; private static readonly IntPtr InvalidHandleValue = new IntPtr(-1); public static void Banner(string text) { string text2 = new string('=', text.Length + 4); lock (Lock) { Write("\u001b[1m" + text2 + "\u001b[0m\n"); Write("\u001b[1m " + text + " \u001b[0m\n"); Write("\u001b[1m" + text2 + "\u001b[0m\n"); } } public static void Section(string text) { lock (Lock) { Write("\n\u001b[1m\u001b[94m:: " + text + "\u001b[0m\n"); } } public static void Info(string text) { Tagged("\u001b[94m", "[*]", text); } public static void Ok(string text) { Tagged("\u001b[92m", "[+]", text); } public static void Warn(string text) { Tagged("\u001b[93m", "[!]", text); } public static void Err(string text) { Tagged("\u001b[91m", "[X]", text); } public static void Debug(string text) { Tagged("\u001b[90m", "[.]", text); } public static void Bullet(string text) { lock (Lock) { Write(" " + text + "\n"); } } public static void Kv(string key, string value) { lock (Lock) { Write(" \u001b[90m" + key.PadRight(14) + "\u001b[0m" + value + "\n"); } } private static void Tagged(string color, string tag, string text) { lock (Lock) { Write(color + tag + "\u001b[0m " + text + "\n"); } } private static void Write(string s) { if (!UseColor) { int num = 0; while (num < s.Length) { if (s[num] == '\u001b' && num + 1 < s.Length && s[num + 1] == '[') { int num2 = s.IndexOf('m', num); if (num2 < 0) { break; } num = num2 + 1; } else { Console.Write(s[num]); num++; } } } else { Console.Write(s); } } [DllImport("kernel32.dll", SetLastError = true)] private static extern IntPtr GetStdHandle(int nStdHandle); [DllImport("kernel32.dll", SetLastError = true)] [return: MarshalAs(UnmanagedType.Bool)] private static extern bool GetConsoleMode(IntPtr hConsoleHandle, out uint lpMode); [DllImport("kernel32.dll", SetLastError = true)] [return: MarshalAs(UnmanagedType.Bool)] private static extern bool SetConsoleMode(IntPtr hConsoleHandle, uint dwMode); private static bool TryEnableVirtualTerminal() { try { IntPtr stdHandle = GetStdHandle(-11); if (stdHandle == IntPtr.Zero || stdHandle == InvalidHandleValue) { return false; } if (!GetConsoleMode(stdHandle, out var lpMode)) { return false; } if ((lpMode & 4) != 0) { return true; } return SetConsoleMode(stdHandle, lpMode | 4); } catch { return false; } } }