From fe9bd29c7d50b44525099931ac2453c53c1e300b Mon Sep 17 00:00:00 2001 From: kira1928 Date: Mon, 4 Dec 2023 00:17:21 +0900 Subject: [PATCH] feat: disable console quick edit mode for Cli (#556) --- BililiveRecorder.Cli/Program.cs | 73 +++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) diff --git a/BililiveRecorder.Cli/Program.cs b/BililiveRecorder.Cli/Program.cs index 728b6f7..41d5852 100644 --- a/BililiveRecorder.Cli/Program.cs +++ b/BililiveRecorder.Cli/Program.cs @@ -5,6 +5,7 @@ using System.CommandLine.NamingConventionBinder; using System.IO; using System.Linq; using System.Net; +using System.Runtime.InteropServices; using System.Security.Authentication; using System.Security.Cryptography; using System.Security.Cryptography.X509Certificates; @@ -38,6 +39,11 @@ namespace BililiveRecorder.Cli { private static int Main(string[] args) { + if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) + && string.IsNullOrEmpty(Environment.GetEnvironmentVariable("BREC_SKIP_DISABLE_QUICK_EDIT"))) + { + ConsoleModeHelper.SetQuickEditMode(false); + } RootCommand root; using (var entrypointLogger = BuildLogger(LogEventLevel.Fatal, LogEventLevel.Verbose)) @@ -564,5 +570,72 @@ namespace BililiveRecorder.Cli All = Danmaku | SuperChat | Guard | Gift | RawData } } + + private static class ConsoleModeHelper + { + internal static bool SetQuickEditMode(bool enable = true) + { + return SetMode(ConsoleModes.ENABLE_QUICK_EDIT_MODE, enable); + } + + private static bool SetMode(ConsoleModes mode, bool enable = true) + { + IntPtr consoleHandle = GetStdHandle(STD_INPUT_HANDLE); + if (consoleHandle == INVALID_HANDLE_VALUE) + { + return false; + } + + uint consoleMode; + if (!GetConsoleMode(consoleHandle, out consoleMode)) + { + return false; + } + + if (enable) + { + consoleMode |= (uint)mode; + } + else + { + consoleMode &= ~(uint)mode; + } + + return SetConsoleMode(consoleHandle, consoleMode); + } + + const int STD_INPUT_HANDLE = -10; + static readonly IntPtr INVALID_HANDLE_VALUE = new IntPtr(-1); + + [Flags] + private enum ConsoleModes : uint + { + ENABLE_PROCESSED_INPUT = 0x0001, + ENABLE_LINE_INPUT = 0x0002, + ENABLE_ECHO_INPUT = 0x0004, + ENABLE_WINDOW_INPUT = 0x0008, + ENABLE_MOUSE_INPUT = 0x0010, + ENABLE_INSERT_MODE = 0x0020, + ENABLE_QUICK_EDIT_MODE = 0x0040, + ENABLE_EXTENDED_FLAGS = 0x0080, + ENABLE_AUTO_POSITION = 0x0100, + ENABLE_VIRTUAL_TERMINAL_INPUT = 0x0200, + + ENABLE_PROCESSED_OUTPUT = 0x0001, + ENABLE_WRAP_AT_EOL_OUTPUT = 0x0002, + ENABLE_VIRTUAL_TERMINAL_PROCESSING = 0x0004, + DISABLE_NEWLINE_AUTO_RETURN = 0x0008, + ENABLE_LVB_GRID_WORLDWIDE = 0x0010 + } + + [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); + } } }