width property

  1. @override
int get width
override

Returns the width of the current console window in characters.

Implementation

@override
int get width {
  if (_width == null) {
    // try using ioctl() to give us the screen size
    final width = _terminal.get_window_width();
    if (width != -1) {
      return _width = width;
    } else {
      // otherwise, fall back to the approach of setting the cursor to beyond
      // the edge of the screen and then reading back its actual position
      final originalCursor = cursorPosition.get();
      stdout.write(control_sequence_identifier +
          '999C' +
          control_sequence_identifier +
          '999B');
      final newCursor = cursorPosition.get();
      cursorPosition.update(originalCursor);
      if (newCursor != null) {
        return _width = newCursor.col;
      } else {
        // we've run out of options; terminal is unsupported
        throw Exception(
          "Couldn't retrieve window width.",
        );
      }
    }
  } else {
    return _width!;
  }
}