From f9d50f677d61b336f95ca9984753bfc6fa8b9981 Mon Sep 17 00:00:00 2001 From: albertony <12441419+albertony@users.noreply.github.com> Date: Fri, 10 Feb 2023 21:13:33 +0100 Subject: [PATCH] lib/terminal: enable windows console virtual terminal sequences processing (ANSI/VT100 colors) This ensures the virtual terminal processing mode is enabled on the rclone process for Windows 10 consoles (by using Windows Console API functions GetConsoleMode/SetConsoleMode and flag ENABLE_VIRTUAL_TERMINAL_PROCESSING), which adds native support for ANSI/VT100 escape sequences. This mode is default in many cases, e.g. when using the Windows Terminal application, but in other cases it is not, and the default can also be controlled with registry setting (see below), and therefore configuring it on the process seem to be the only reliable way of ensuring it is enabled when supported. [HKEY_CURRENT_USER\Console] "VirtualTerminalLevel"=dword:00000001 --- cmd/cmd.go | 8 +++++++- lib/terminal/terminal.go | 11 +++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/cmd/cmd.go b/cmd/cmd.go index f6365cee8..73fe98a2d 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -401,9 +401,15 @@ func initConfig() { // Start accounting accounting.Start(ctx) - // Hide console window + // Configure console if ci.NoConsole { + // Hide the console window terminal.HideConsole() + } else { + // Enable color support on stdout if possible. + // This enables virtual terminal processing on Windows 10, + // adding native support for ANSI/VT100 escape sequences. + terminal.EnableColorsStdout() } // Load filters diff --git a/lib/terminal/terminal.go b/lib/terminal/terminal.go index 532cf1fcc..f933de719 100644 --- a/lib/terminal/terminal.go +++ b/lib/terminal/terminal.go @@ -111,3 +111,14 @@ func Write(out []byte) { Start() _, _ = Out.Write(out) } + +// EnableColorsStdout enable colors if possible. +// This enables virtual terminal processing on Windows 10 console, +// adding native support for VT100 escape codes. When this terminal +// package is used for output, the result is that the colorable library +// don't have to decode the escapes and explicitely write text with color +// formatting to the console using Windows API functions, but can simply +// relay everything to stdout. +func EnableColorsStdout() { + _ = colorable.EnableColorsStdout(nil) +}