98 lines
2.7 KiB
Dart
98 lines
2.7 KiB
Dart
import '../../core/api_gateway.dart';
|
|
|
|
/// One machine terminal as the server records it (`terminal_list`,
|
|
/// `terminal_event`): a login shell on that machine, running or ended.
|
|
class TerminalInfo {
|
|
const TerminalInfo({
|
|
required this.id,
|
|
required this.cwd,
|
|
required this.status,
|
|
this.title = '',
|
|
this.shell = '',
|
|
this.tmux = '',
|
|
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'] ?? ''}',
|
|
tmux: '${json['tmux'] ?? ''}',
|
|
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;
|
|
|
|
/// The tmux session keeping the shell alive across server restarts;
|
|
/// empty when the shell ends with the server.
|
|
final String tmux;
|
|
|
|
/// 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: a restarted server starts
|
|
/// a new one, whose 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,
|
|
tmux: tmux,
|
|
reason: reason,
|
|
exitCode: exitCode,
|
|
createdAt: createdAt,
|
|
cols: cols,
|
|
rows: rows,
|
|
stream: stream,
|
|
outputEnd: outputEnd,
|
|
);
|
|
|
|
bool get running => status == 'running';
|
|
bool get closed => status == 'closed';
|
|
bool get persistent => tmux.isNotEmpty;
|
|
|
|
/// 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<TerminalInfo> terminalInfos(JsonMap reply) => [
|
|
for (final value in (reply['terminals'] as List?) ?? const [])
|
|
if (value is Map) TerminalInfo.fromJson(value),
|
|
];
|