tryOpen static method

TtyTerminal? tryOpen({
  1. String path = _defaultTtyPath,
  2. IOSink? output,
})

Attempts to open /dev/tty and returns a TtyTerminal, or null if not available on this platform.

Implementation

static TtyTerminal? tryOpen({
  String path = _defaultTtyPath,
  io.IOSink? output,
}) {
  try {
    if (io.Platform.isWindows) return null;
    final tty = io.File(path);
    if (!tty.existsSync()) return null;

    // If output is provided, we don't strictly need to be able to open tty
    // for write, but we usually want to verify it's a valid TTY we can
    // control. stty will fail if it's not a TTY.
    if (output == null) {
      final sink = tty.openWrite();
      sink.close();
    }

    return TtyTerminal._(path, tty, output: output);
  } catch (_) {
    return null;
  }
}