watchSize method

  1. @override
Stream<Point<int>> watchSize()
override

Watches terminal size changes.

Implementation

@override
Stream<Point<int>> watchSize() {
  late StreamController<Point<int>> controller;
  Timer? timer;
  StreamSubscription? sigwinchSubscription;

  controller = StreamController<Point<int>>(
    onListen: () {
      var last = rawTerminal.getScreenBufferSize();

      if (Platform.isWindows) {
        timer = Timer.periodic(const Duration(milliseconds: 500), (_) {
          final peek = rawTerminal.getScreenBufferSize();
          if (peek != last) {
            last = peek;
            controller.add(peek);
          }
        });
      } else {
        sigwinchSubscription = ProcessSignal.sigwinch.watch().listen((_) {
          final peek = rawTerminal.getScreenBufferSize();
          if (peek != last) {
            last = peek;
            controller.add(peek);
          }
        });
      }
    },
    onCancel: () {
      timer?.cancel();
      sigwinchSubscription?.cancel();
    },
  );

  return controller.stream;
}