TermRemoteCtl/apps/mobile_app/lib/features/terminal/terminal_interaction_controller.dart

107 lines
2.6 KiB
Dart

import 'package:flutter/foundation.dart';
import 'history_window.dart';
enum TerminalConnectionState {
connecting,
connected,
reconnecting,
disconnected,
}
class TerminalInteractionController extends ChangeNotifier {
static const int maxTrackedLiveLines = 200;
TerminalInteractionController({
HistoryWindow historyWindow = const HistoryWindow(
lines: <String>[],
hasMoreAbove: false,
),
}) : _historyWindow = historyWindow;
TerminalConnectionState _connectionState = TerminalConnectionState.connecting;
bool _isFollowingLiveOutput = true;
bool _hasPendingLiveOutput = false;
HistoryWindow _historyWindow;
final List<String> _liveLines = <String>[];
int _liveOutputCount = 0;
TerminalConnectionState get connectionState => _connectionState;
bool get isFollowingLiveOutput => _isFollowingLiveOutput;
bool get hasPendingLiveOutput => _hasPendingLiveOutput;
bool get canSendInput =>
_connectionState == TerminalConnectionState.connected;
HistoryWindow get historyWindow => _historyWindow;
List<String> get liveLines => List.unmodifiable(_liveLines);
int get liveOutputCount => _liveOutputCount;
void markConnecting() {
_connectionState = TerminalConnectionState.connecting;
notifyListeners();
}
void markConnected() {
_connectionState = TerminalConnectionState.connected;
notifyListeners();
}
void markReconnecting() {
_connectionState = TerminalConnectionState.reconnecting;
notifyListeners();
}
void markDisconnected() {
_connectionState = TerminalConnectionState.disconnected;
notifyListeners();
}
void enterScrollback() {
_isFollowingLiveOutput = false;
notifyListeners();
}
void jumpToLive() {
_isFollowingLiveOutput = true;
_hasPendingLiveOutput = false;
notifyListeners();
}
void registerIncomingFrame() {
if (!_isFollowingLiveOutput) {
_hasPendingLiveOutput = true;
notifyListeners();
}
}
void applyFrame(String chunk) {
_liveLines.add(chunk);
_liveOutputCount += 1;
while (_liveLines.length > maxTrackedLiveLines) {
_liveLines.removeAt(0);
}
notifyListeners();
}
void loadHistory(HistoryWindow historyWindow) {
_historyWindow = historyWindow;
_liveLines
..clear()
..addAll(
historyWindow.lines.length <= maxTrackedLiveLines
? historyWindow.lines
: historyWindow.lines.sublist(
historyWindow.lines.length - maxTrackedLiveLines,
),
);
_liveOutputCount = historyWindow.lines.length;
_hasPendingLiveOutput = false;
notifyListeners();
}
}