Skip to content

Commit 337f09e

Browse files
committed
Add File.getOrEnableAnsiEscapeSupport and use it
On Windows, the console mode flag `ENABLE_VIRTUAL_TERMINAL_PROCESSING` determines whether or not ANSI escape codes are parsed/acted on. On the newer Windows Terminal, this flag is set by default, but on the older Windows Console, it is not set by default, but *can* be enabled (since Windows 10 RS1 from June 2016). The new `File.getOrEnableAnsiEscapeSupport` function will get the current status of ANSI escape code support, but will also attempt to enable `ENABLE_VIRTUAL_TERMINAL_PROCESSING` on Windows if necessary which will provide better/more consistent results for things like `std.Progress` and `std.io.tty`. This type of change was not done previously due to a mistaken assumption (on my part) that the console mode would persist after the run of a program. However, it turns out that the console mode is always reset to the default for each program run in a console session.
1 parent 17dc939 commit 337f09e

File tree

3 files changed

+32
-6
lines changed

3 files changed

+32
-6
lines changed

lib/std/Progress.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ pub fn start(options: Options) Node {
377377
}
378378
const stderr = std.io.getStdErr();
379379
global_progress.terminal = stderr;
380-
if (stderr.supportsAnsiEscapeCodes()) {
380+
if (stderr.getOrEnableAnsiEscapeSupport()) {
381381
global_progress.terminal_mode = .ansi_escape_codes;
382382
} else if (is_windows and stderr.isTty()) {
383383
global_progress.terminal_mode = TerminalMode{ .windows_api = .{

lib/std/fs/File.zig

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ pub fn sync(self: File) SyncError!void {
188188
}
189189

190190
/// Test whether the file refers to a terminal.
191-
/// See also `supportsAnsiEscapeCodes`.
191+
/// See also `getOrEnableAnsiEscapeSupport` and `supportsAnsiEscapeCodes`.
192192
pub fn isTty(self: File) bool {
193193
return posix.isatty(self.handle);
194194
}
@@ -245,8 +245,16 @@ pub fn isCygwinPty(file: File) bool {
245245
std.mem.indexOf(u16, name_wide, &[_]u16{ '-', 'p', 't', 'y' }) != null;
246246
}
247247

248-
/// Test whether ANSI escape codes will be treated as such.
249-
pub fn supportsAnsiEscapeCodes(self: File) bool {
248+
/// Returns whether or not ANSI escape codes will be treated as such,
249+
/// and attempts to enable support for ANSI escape codes if necessary
250+
/// (on Windows).
251+
///
252+
/// Returns `true` if ANSI escape codes are supported or support was
253+
/// successfully enabled. Returns false if ANSI escape codes are not
254+
/// supported or support was unable to be enabled.
255+
///
256+
/// See also `supportsAnsiEscapeCodes`.
257+
pub fn getOrEnableAnsiEscapeSupport(self: File) bool {
250258
if (builtin.os.tag == .windows) {
251259
var original_console_mode: windows.DWORD = 0;
252260

@@ -262,14 +270,31 @@ pub fn supportsAnsiEscapeCodes(self: File) bool {
262270
var console_mode = original_console_mode | requested_console_modes;
263271
if (windows.kernel32.SetConsoleMode(self.handle, console_mode) != 0) return true;
264272

265-
// An application receiving ERROR_INVALID_PARAMETER with one of the newer console mode flags in the bit field should gracefully degrade behavior and try again.
273+
// An application receiving ERROR_INVALID_PARAMETER with one of the newer console mode
274+
// flags in the bit field should gracefully degrade behavior and try again.
266275
requested_console_modes = windows.ENABLE_VIRTUAL_TERMINAL_PROCESSING;
267276
console_mode = original_console_mode | requested_console_modes;
268277
if (windows.kernel32.SetConsoleMode(self.handle, console_mode) != 0) return true;
269278
}
270279

271280
return self.isCygwinPty();
272281
}
282+
return self.supportsAnsiEscapeCodes();
283+
}
284+
285+
/// Test whether ANSI escape codes will be treated as such without
286+
/// attempting to enable support for ANSI escape codes.
287+
///
288+
/// See also `getOrEnableAnsiEscapeSupport`.
289+
pub fn supportsAnsiEscapeCodes(self: File) bool {
290+
if (builtin.os.tag == .windows) {
291+
var console_mode: windows.DWORD = 0;
292+
if (windows.kernel32.GetConsoleMode(self.handle, &console_mode) != 0) {
293+
if (console_mode & windows.ENABLE_VIRTUAL_TERMINAL_PROCESSING != 0) return true;
294+
}
295+
296+
return self.isCygwinPty();
297+
}
273298
if (builtin.os.tag == .wasi) {
274299
// WASI sanitizes stdout when fd is a tty so ANSI escape codes
275300
// will not be interpreted as actual cursor commands, and

lib/std/io/tty.zig

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const native_os = builtin.os.tag;
88
/// Detect suitable TTY configuration options for the given file (commonly stdout/stderr).
99
/// This includes feature checks for ANSI escape codes and the Windows console API, as well as
1010
/// respecting the `NO_COLOR` and `CLICOLOR_FORCE` environment variables to override the default.
11+
/// Will attempt to enable ANSI escape code support if necessary/possible.
1112
pub fn detectConfig(file: File) Config {
1213
const force_color: ?bool = if (builtin.os.tag == .wasi)
1314
null // wasi does not support environment variables
@@ -20,7 +21,7 @@ pub fn detectConfig(file: File) Config {
2021

2122
if (force_color == false) return .no_color;
2223

23-
if (file.supportsAnsiEscapeCodes()) return .escape_codes;
24+
if (file.getOrEnableAnsiEscapeSupport()) return .escape_codes;
2425

2526
if (native_os == .windows and file.isTty()) {
2627
var info: windows.CONSOLE_SCREEN_BUFFER_INFO = undefined;

0 commit comments

Comments
 (0)