TerminalContext class

Global context for accessing the current Terminal instance.

Provides a centralized access point for terminal I/O operations, allowing external users to replace the default implementation with their own for testing or alternative environments.

Usage:

// Get the current terminal (creates default DartTerminal on first access)
final terminal = TerminalContext.current;

// Write output
TerminalContext.current.output.writeln('Hello');

// Read input
final byte = TerminalContext.current.input.readByteSync();

// Set a custom terminal (for testing)
TerminalContext.current = MyCustomTerminal();

// Reset to default dart:io terminal
TerminalContext.reset();

Testing example:

class TestTerminal implements Terminal {
  final outputBuffer = StringBuffer();
  final inputQueue = <int>[];

  @override
  TerminalInput get input => TestInput(inputQueue);

  @override
  TerminalOutput get output => TestOutput(outputBuffer);
}

void main() {
  final testTerminal = TestTerminal();
  TerminalContext.current = testTerminal;

  // Run prompts - all output goes to testTerminal.outputBuffer
  // All input reads from testTerminal.inputQueue

  TerminalContext.reset(); // Restore default
}

Design notes:

  • Lazy initialization: Default DartTerminal created on first access
  • Thread-safe: Single static instance (Dart is single-threaded)
  • No breaking changes: Existing code continues to work unchanged

Properties

hashCode int
The hash code for this object.
no setterinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited

Static Properties

current Terminal
The current terminal instance.
getter/setter pair
hasCustomTerminal bool
Whether a custom terminal has been set.
no setter
input TerminalInput
Shortcut to the current terminal's input.
no setter
output TerminalOutput
Shortcut to the current terminal's output.
no setter

Static Methods

reset() → void
Resets to the default dart:io terminal.