isSupport256Colors static method

bool isSupport256Colors()

Determines if the current terminal likely supports 256-color ANSI escape sequences.

This method uses heuristic checks based on environment variables:

  • Requires io.stdout.supportsAnsiEscapes for basic ANSI support.
  • On Windows: checks WT_SESSION (for Windows Terminal) or TERM (for WSL/Git Bash).
  • On non-Windows: relies on TERM (e.g., xterm-256color).

Note: This is a heuristic and may not be 100% accurate.

Implementation

static bool isSupport256Colors() {
  // If basic ANSI escape sequences are not supported, then 256 colors definitely aren't.
  if (!stdout.supportsAnsiEscapes) return false;

  final term = Platform.environment['TERM']?.toLowerCase();
  final has256color = term != null &&
      (term.contains('256color') || term.contains('truecolor'));

  if (Platform.isWindows) {
    // Windows Terminal sets WT_SESSION. It supports 256+ colors.
    if (Platform.environment.containsKey('WT_SESSION')) return true;

    // For other Windows environments (e.g., WSL, Git Bash), check TERM.
    return has256color;
  } else {
    // On non-Windows (Linux, macOS), TERM is the primary indicator.
    return has256color;
  }
}