|
| 1 | +using BenchmarkDotNet.Detectors; |
| 2 | +using BenchmarkDotNet.Loggers; |
| 3 | +using System; |
| 4 | +using System.Diagnostics; |
| 5 | +using System.Runtime.InteropServices; |
| 6 | +using System.Runtime.Versioning; |
| 7 | +using System.Text.RegularExpressions; |
| 8 | + |
| 9 | +namespace BenchmarkDotNet.Helpers; |
| 10 | + |
| 11 | +#nullable enable |
| 12 | + |
| 13 | +internal static class ConsoleHelper |
| 14 | +{ |
| 15 | + private const string ESC = "\e"; // Escape sequence. |
| 16 | + private const string OSC8 = $"{ESC}]8;;"; // Operating System Command 8 |
| 17 | + private const string ST = ESC + @"\"; // String Terminator |
| 18 | + |
| 19 | + /// <summary> |
| 20 | + /// Write clickable link to console. |
| 21 | + /// If console doesn't support OSC 8 hyperlinks. It writes plain link with markdown syntax. |
| 22 | + /// </summary> |
| 23 | + public static void WriteLineAsClickableLink(ILogger consoleLogger, string link, string? linkCaption = null, LogKind logKind = LogKind.Info, string prefixText = "", string suffixText = "") |
| 24 | + { |
| 25 | + if (prefixText != "") |
| 26 | + consoleLogger.Write(logKind, prefixText); |
| 27 | + |
| 28 | + WriteAsClickableLink(consoleLogger, link, linkCaption, logKind); |
| 29 | + |
| 30 | + // On Windows Terminal environment. |
| 31 | + // It need to write extra space to avoid link style corrupted issue that occurred when window resized. |
| 32 | + if (IsWindowsTerminal.Value && IsClickableLinkSupported.Value && suffixText == "") |
| 33 | + suffixText = " "; |
| 34 | + |
| 35 | + if (suffixText != "") |
| 36 | + consoleLogger.Write(logKind, suffixText); |
| 37 | + |
| 38 | + consoleLogger.WriteLine(); |
| 39 | + } |
| 40 | + |
| 41 | + /// <summary> |
| 42 | + /// Write clickable link to console. |
| 43 | + /// If console doesn't support OSC 8 hyperlinks. It writes plain link with markdown syntax. |
| 44 | + /// </summary> |
| 45 | + public static void WriteAsClickableLink(ILogger consoleLogger, string link, string? linkCaption = null, LogKind logKind = LogKind.Info) |
| 46 | + { |
| 47 | + if (consoleLogger.Id != nameof(ConsoleLogger)) |
| 48 | + throw new NotSupportedException("This method is expected logger that has ConsoleLogger id."); |
| 49 | + |
| 50 | + // If clickable link supported. Write clickable link with OSC8. |
| 51 | + if (IsClickableLinkSupported.Value) |
| 52 | + { |
| 53 | + consoleLogger.Write(logKind, @$"{OSC8}{link}{ST}{linkCaption ?? link}{OSC8}{ST}"); |
| 54 | + return; |
| 55 | + } |
| 56 | + |
| 57 | + // If link caption is specified. Write link as plain text with markdown link syntax. |
| 58 | + if (!string.IsNullOrEmpty(linkCaption)) |
| 59 | + { |
| 60 | + consoleLogger.Write(logKind, $"[{linkCaption}]({link})"); |
| 61 | + return; |
| 62 | + } |
| 63 | + |
| 64 | + // Write link as plain text. |
| 65 | + consoleLogger.Write(logKind, link); |
| 66 | + } |
| 67 | + |
| 68 | + private static readonly Lazy<bool> IsWindowsTerminal = new(() |
| 69 | + => Environment.GetEnvironmentVariable("WT_SESSION") != null); |
| 70 | + |
| 71 | + private static readonly Lazy<bool> IsClickableLinkSupported = new(() => |
| 72 | + { |
| 73 | + if (Console.IsOutputRedirected) |
| 74 | + return false; |
| 75 | + |
| 76 | + // The current console doesn't have a valid buffer size, which means it is not a real console. |
| 77 | + if (Console.BufferHeight == 0 || Console.BufferWidth == 0) |
| 78 | + return false; |
| 79 | + |
| 80 | + // Disable clickable link on CI environment. |
| 81 | + if (!string.IsNullOrEmpty(Environment.GetEnvironmentVariable("CI"))) |
| 82 | + return false; |
| 83 | + |
| 84 | + // dumb terminal don't support ANSI escape sequence. |
| 85 | + var term = Environment.GetEnvironmentVariable("TERM") ?? ""; |
| 86 | + if (term == "dumb") |
| 87 | + return false; |
| 88 | + |
| 89 | + if (OsDetector.IsWindows()) |
| 90 | + { |
| 91 | + try |
| 92 | + { |
| 93 | + // conhost.exe don't support clickable link with OSC8. |
| 94 | + if (IsRunningOnConhost()) |
| 95 | + return false; |
| 96 | + |
| 97 | + // ConEmu and don't support OSC8. |
| 98 | + var conEmu = Environment.GetEnvironmentVariable("ConEmuANSI"); |
| 99 | + if (conEmu != null) |
| 100 | + return false; |
| 101 | + |
| 102 | + // Return true if Virtual Terminal Processing mode is enabled. |
| 103 | + return IsVirtualTerminalProcessingEnabled(); |
| 104 | + } |
| 105 | + catch |
| 106 | + { |
| 107 | + return false; // Ignore unexpected exception. |
| 108 | + } |
| 109 | + } |
| 110 | + else |
| 111 | + { |
| 112 | + // screen don't support OSC8 clickable link. |
| 113 | + if (Regex.IsMatch(term, "^screen")) |
| 114 | + return false; |
| 115 | + |
| 116 | + // Other major terminal supports OSC8 by default. https://github.com/Alhadis/OSC8-Adoption |
| 117 | + return true; |
| 118 | + } |
| 119 | + }); |
| 120 | + |
| 121 | + [SupportedOSPlatform("windows")] |
| 122 | + private static bool IsVirtualTerminalProcessingEnabled() |
| 123 | + { |
| 124 | + // Try to get Virtual Terminal Processing enebled or not. |
| 125 | + const uint STD_OUTPUT_HANDLE = unchecked((uint)-11); |
| 126 | + IntPtr handle = NativeMethods.GetStdHandle(STD_OUTPUT_HANDLE); |
| 127 | + if (handle == IntPtr.Zero) |
| 128 | + return false; |
| 129 | + |
| 130 | + if (NativeMethods.GetConsoleMode(handle, out uint consoleMode)) |
| 131 | + { |
| 132 | + const uint ENABLE_VIRTUAL_TERMINAL_PROCESSING = 0x0004; |
| 133 | + if ((consoleMode & ENABLE_VIRTUAL_TERMINAL_PROCESSING) > 0) |
| 134 | + { |
| 135 | + return true; |
| 136 | + } |
| 137 | + } |
| 138 | + return false; |
| 139 | + } |
| 140 | + |
| 141 | + [SupportedOSPlatform("windows")] |
| 142 | + private static bool IsRunningOnConhost() |
| 143 | + { |
| 144 | + IntPtr hwnd = NativeMethods.GetConsoleWindow(); |
| 145 | + if (hwnd == IntPtr.Zero) |
| 146 | + return false; |
| 147 | + |
| 148 | + NativeMethods.GetWindowThreadProcessId(hwnd, out uint pid); |
| 149 | + using var process = Process.GetProcessById((int)pid); |
| 150 | + return process.ProcessName == "conhost"; |
| 151 | + } |
| 152 | + |
| 153 | + [SupportedOSPlatform("windows")] |
| 154 | + private static class NativeMethods |
| 155 | + { |
| 156 | + [DllImport("kernel32.dll", SetLastError = true)] |
| 157 | + public static extern IntPtr GetStdHandle(uint nStdHandle); |
| 158 | + |
| 159 | + [DllImport("kernel32.dll", SetLastError = true)] |
| 160 | + public static extern bool GetConsoleMode(IntPtr hConsoleHandle, out uint lpMode); |
| 161 | + |
| 162 | + [DllImport("kernel32.dll", SetLastError = true)] |
| 163 | + public static extern IntPtr GetConsoleWindow(); |
| 164 | + |
| 165 | + [DllImport("user32.dll", SetLastError = true)] |
| 166 | + public static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId); |
| 167 | + } |
| 168 | +} |
0 commit comments