DoxigAlpha

detect

Detect suitable TTY configuration options for the given file (commonly stdout/stderr). This includes feature checks for ANSI escape codes and the Windows console API, as well as respecting the NO_COLOR and CLICOLOR_FORCE environment variables to override the default. Will attempt to enable ANSI escape code support if necessary/possible.

Function parameters

Parameters

#

Type definitions in this namespace

Types

#
Config
Provides simple functionality for manipulating the terminal in some way,

Deprecated in favor of `Config.detect`.

Functions

#
detectConfig
Deprecated in favor of `Config.detect`.

Source

Implementation

#
pub fn detect(file: File) Config {
    const force_color: ?bool = if (builtin.os.tag == .wasi)
        null // wasi does not support environment variables
    else if (process.hasNonEmptyEnvVarConstant("NO_COLOR"))
        false
    else if (process.hasNonEmptyEnvVarConstant("CLICOLOR_FORCE"))
        true
    else
        null;

    if (force_color == false) return .no_color;

    if (file.getOrEnableAnsiEscapeSupport()) return .escape_codes;

    if (native_os == .windows and file.isTty()) {
        var info: windows.CONSOLE_SCREEN_BUFFER_INFO = undefined;
        if (windows.kernel32.GetConsoleScreenBufferInfo(file.handle, &info) == windows.FALSE) {
            return if (force_color == true) .escape_codes else .no_color;
        }
        return .{ .windows_api = .{
            .handle = file.handle,
            .reset_attributes = info.wAttributes,
        } };
    }

    return if (force_color == true) .escape_codes else .no_color;
}