import '../../core/api_gateway.dart'; /// One machine terminal as the server records it (`terminal_list`, /// `terminal_event`): a login on that machine (its login prompt, then the /// account's shell), running or ended. It ends with the server process. class TerminalInfo { const TerminalInfo({ required this.id, required this.cwd, required this.status, this.title = '', this.shell = '', this.reason = '', this.exitCode, this.createdAt = 0, this.cols = 0, this.rows = 0, this.stream = '', this.outputEnd = 0, }); factory TerminalInfo.fromJson(Map json) => TerminalInfo( id: '${json['id'] ?? ''}', cwd: '${json['cwd'] ?? ''}', status: '${json['status'] ?? ''}', title: '${json['title'] ?? ''}', shell: '${json['shell'] ?? ''}', reason: '${json['reason'] ?? ''}', exitCode: (json['exit_code'] as num?)?.toInt(), createdAt: (json['created_at'] as num?)?.toInt() ?? 0, cols: (json['cols'] as num?)?.toInt() ?? 0, rows: (json['rows'] as num?)?.toInt() ?? 0, stream: '${json['stream'] ?? ''}', outputEnd: (json['output_end'] as num?)?.toInt() ?? 0, ); final String id; final String cwd; /// running, exited or closed. final String status; /// The last window title the shell set, or empty. final String title; final String shell; /// Why an ended terminal ended, when it was not its own exit. final String reason; final int? exitCode; final int createdAt; final int cols; final int rows; /// Names the server process's output stream; another stream's byte /// offsets begin again at zero. final String stream; final int outputEnd; /// This record naming output [stream] (lifecycle frames carry none). TerminalInfo withStream(String stream) => TerminalInfo( id: id, cwd: cwd, status: status, title: title, shell: shell, reason: reason, exitCode: exitCode, createdAt: createdAt, cols: cols, rows: rows, stream: stream, outputEnd: outputEnd, ); bool get running => status == 'running'; bool get closed => status == 'closed'; /// What an ended terminal shows instead of a prompt. String get endedText { if (reason.isNotEmpty) return reason; final code = exitCode; return code == null ? 'The shell ended.' : 'The shell exited with $code.'; } } /// The terminal records of a `terminal_list` reply. List terminalInfos(JsonMap reply) => [ for (final value in (reply['terminals'] as List?) ?? const []) if (value is Map) TerminalInfo.fromJson(value), ];