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
This commit is contained in:
albertony 2023-02-10 21:13:33 +01:00
parent 3641993fab
commit f9d50f677d
2 changed files with 18 additions and 1 deletions

View File

@ -401,9 +401,15 @@ func initConfig() {
// Start accounting // Start accounting
accounting.Start(ctx) accounting.Start(ctx)
// Hide console window // Configure console
if ci.NoConsole { if ci.NoConsole {
// Hide the console window
terminal.HideConsole() 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 // Load filters

View File

@ -111,3 +111,14 @@ func Write(out []byte) {
Start() Start()
_, _ = Out.Write(out) _, _ = 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)
}