getTerminalSize function

({int height, int width}) getTerminalSize()

Tries to get the size of the terminal.

Mimics the logic of Python's shutil.get_terminal_size function:

  • Returns the values of the COLUMNS and LINES environment variables if available. (However, these variables usually are unexported shell variables and therefore typically are not available.)
  • Otherwise queries the connected TTY.
  • Otherwise falls back to default values (80 columns by 24 lines).

Implementation

({int width, int height}) getTerminalSize() => (
      width: tryParseInt(io.Platform.environment['COLUMNS']) ??
          (io.stdout.hasTerminal ? io.stdout.terminalColumns : 80),
      height: tryParseInt(io.Platform.environment['LINES']) ??
          (io.stdout.hasTerminal ? io.stdout.terminalLines : 24),
    );