1
This commit is contained in:
1 parent
87e03ce0d8
commit
e932c60dbd
47 files changed
+4417
-923
No files matched your search
+13
-185
@@ -1,190 +1,18 @@
|
||||
/// Linux 发行版安装状态
|
||||
enum DistroStatus {
|
||||
/// 未安装
|
||||
notInstalled,
|
||||
import 'dart:ffi';
|
||||
|
||||
/// 正在安装/解压中
|
||||
installing,
|
||||
|
||||
/// 已安装就绪
|
||||
installed,
|
||||
|
||||
/// 安装或运行异常
|
||||
error,
|
||||
}
|
||||
|
||||
/// Linux 发行版元数据模型,具备高扩展性
|
||||
/// 内置 Linux 发行版资源信息常量
|
||||
class DistroInfo {
|
||||
/// 唯一标识符,例如 'alpine', 'debian', 'ubuntu', 'host'
|
||||
final String id;
|
||||
|
||||
/// 显示名称,例如 'Alpine Linux'
|
||||
final String name;
|
||||
|
||||
/// 版本号,例如 '3.20.3'
|
||||
final String version;
|
||||
|
||||
/// 描述信息
|
||||
final String description;
|
||||
|
||||
/// 默认登录 Shell,例如 '/bin/sh', '/bin/bash'
|
||||
final String defaultShell;
|
||||
|
||||
/// 包管理器名称,例如 'apk', 'apt'
|
||||
final String packageManager;
|
||||
|
||||
/// 支持的硬件架构,例如 ['aarch64', 'arm64-v8a']
|
||||
final List<String> supportedArchs;
|
||||
|
||||
/// 内置应用资源路径(如有,可离线解压)
|
||||
final String? assetPath;
|
||||
|
||||
/// 远程下载地址(支持网络按需下载扩展)
|
||||
final String? downloadUrl;
|
||||
|
||||
/// 是否为官方内置预设
|
||||
final bool isBuiltin;
|
||||
|
||||
/// 图标名称标识
|
||||
final String iconName;
|
||||
|
||||
const DistroInfo({
|
||||
required this.id,
|
||||
required this.name,
|
||||
required this.version,
|
||||
required this.description,
|
||||
this.defaultShell = '/bin/sh',
|
||||
this.packageManager = 'apk',
|
||||
this.supportedArchs = const ['aarch64', 'arm64-v8a', 'all'],
|
||||
this.assetPath,
|
||||
this.downloadUrl,
|
||||
this.isBuiltin = true,
|
||||
this.iconName = 'terminal',
|
||||
});
|
||||
|
||||
/// 预设:Alpine Linux (轻量极速,内置 asset)
|
||||
static const DistroInfo alpine = DistroInfo(
|
||||
id: 'alpine',
|
||||
name: 'Alpine Linux',
|
||||
version: '3.20.3',
|
||||
description: '轻量快速的 Linux 发行版,体积仅约 4MB,内置 apk 包管理器',
|
||||
defaultShell: '/bin/sh',
|
||||
packageManager: 'apk',
|
||||
supportedArchs: ['aarch64', 'arm64-v8a'],
|
||||
assetPath: 'assets/distros/alpine-minirootfs-3.20.3-aarch64.tar.gz',
|
||||
isBuiltin: true,
|
||||
iconName: 'alpine',
|
||||
);
|
||||
|
||||
/// 预设扩展:Debian GNU/Linux
|
||||
static const DistroInfo debian = DistroInfo(
|
||||
id: 'debian',
|
||||
name: 'Debian GNU/Linux',
|
||||
version: '12 (Bookworm)',
|
||||
description: '全能稳定的 Linux 发行版,标准 glibc 兼容,内置 apt 包管理器',
|
||||
defaultShell: '/bin/bash',
|
||||
packageManager: 'apt',
|
||||
supportedArchs: ['aarch64', 'x86_64'],
|
||||
downloadUrl: 'https://mirrors.tuna.tsinghua.edu.cn/lxc-images/images/debian/bookworm/arm64/default/',
|
||||
isBuiltin: false,
|
||||
iconName: 'debian',
|
||||
);
|
||||
|
||||
/// 预设扩展:Ubuntu
|
||||
static const DistroInfo ubuntu = DistroInfo(
|
||||
id: 'ubuntu',
|
||||
name: 'Ubuntu',
|
||||
version: '24.04 LTS',
|
||||
description: '应用与开发生态丰富的流行 Linux 发行版,内置 apt 包管理器',
|
||||
defaultShell: '/bin/bash',
|
||||
packageManager: 'apt',
|
||||
supportedArchs: ['aarch64', 'x86_64'],
|
||||
downloadUrl: 'https://mirrors.tuna.tsinghua.edu.cn/ubuntu-cdimage/ubuntu-base/releases/24.04/release/',
|
||||
isBuiltin: false,
|
||||
iconName: 'ubuntu',
|
||||
);
|
||||
|
||||
/// 预设:Android 本地 Shell
|
||||
static const DistroInfo host = DistroInfo(
|
||||
id: 'host',
|
||||
name: '本地 Shell',
|
||||
version: 'Android',
|
||||
description: '直接调用安卓系统内置的基础 Shell,无需虚拟环境',
|
||||
defaultShell: '/system/bin/sh',
|
||||
packageManager: 'none',
|
||||
supportedArchs: ['all'],
|
||||
isBuiltin: true,
|
||||
iconName: 'host',
|
||||
);
|
||||
|
||||
/// 默认官方预设列表
|
||||
static const List<DistroInfo> defaults = [
|
||||
alpine,
|
||||
debian,
|
||||
ubuntu,
|
||||
host,
|
||||
];
|
||||
|
||||
DistroInfo copyWith({
|
||||
String? id,
|
||||
String? name,
|
||||
String? version,
|
||||
String? description,
|
||||
String? defaultShell,
|
||||
String? packageManager,
|
||||
List<String>? supportedArchs,
|
||||
String? assetPath,
|
||||
String? downloadUrl,
|
||||
bool? isBuiltin,
|
||||
String? iconName,
|
||||
}) {
|
||||
return DistroInfo(
|
||||
id: id ?? this.id,
|
||||
name: name ?? this.name,
|
||||
version: version ?? this.version,
|
||||
description: description ?? this.description,
|
||||
defaultShell: defaultShell ?? this.defaultShell,
|
||||
packageManager: packageManager ?? this.packageManager,
|
||||
supportedArchs: supportedArchs ?? this.supportedArchs,
|
||||
assetPath: assetPath ?? this.assetPath,
|
||||
downloadUrl: downloadUrl ?? this.downloadUrl,
|
||||
isBuiltin: isBuiltin ?? this.isBuiltin,
|
||||
iconName: iconName ?? this.iconName,
|
||||
);
|
||||
/// 根据当前运行环境的 CPU 架构自动选取匹配的内置 Alpine Minirootfs
|
||||
static String get builtinAlpineAssetPath {
|
||||
final currentAbi = Abi.current();
|
||||
if (currentAbi == Abi.androidX64 ||
|
||||
currentAbi == Abi.linuxX64 ||
|
||||
currentAbi == Abi.windowsX64) {
|
||||
return 'assets/distros/alpine-minirootfs-3.20.3-x86_64.tar.gz';
|
||||
}
|
||||
return 'assets/distros/alpine-minirootfs-3.20.3-aarch64.tar.gz';
|
||||
}
|
||||
|
||||
Map<String, dynamic> toJson() {
|
||||
return {
|
||||
'id': id,
|
||||
'name': name,
|
||||
'version': version,
|
||||
'description': description,
|
||||
'defaultShell': defaultShell,
|
||||
'packageManager': packageManager,
|
||||
'supportedArchs': supportedArchs,
|
||||
'assetPath': assetPath,
|
||||
'downloadUrl': downloadUrl,
|
||||
'isBuiltin': isBuiltin,
|
||||
'iconName': iconName,
|
||||
};
|
||||
}
|
||||
|
||||
factory DistroInfo.fromJson(Map<String, dynamic> json) {
|
||||
return DistroInfo(
|
||||
id: json['id'] as String,
|
||||
name: json['name'] as String,
|
||||
version: json['version'] as String,
|
||||
description: json['description'] as String,
|
||||
defaultShell: (json['defaultShell'] as String?) ?? '/bin/sh',
|
||||
packageManager: (json['packageManager'] as String?) ?? 'apk',
|
||||
supportedArchs: (json['supportedArchs'] as List<dynamic>?)
|
||||
?.map((e) => e.toString())
|
||||
.toList() ??
|
||||
const ['aarch64'],
|
||||
assetPath: json['assetPath'] as String?,
|
||||
downloadUrl: json['downloadUrl'] as String?,
|
||||
isBuiltin: (json['isBuiltin'] as bool?) ?? false,
|
||||
iconName: (json['iconName'] as String?) ?? 'terminal',
|
||||
);
|
||||
}
|
||||
/// 默认系统实例名称
|
||||
static const String defaultSystemName = 'alpine';
|
||||
}
|
||||
@@ -1,56 +1,169 @@
|
||||
import 'dart:async';
|
||||
import 'dart:convert';
|
||||
import 'package:code_editor/services/distro_manager.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_pty/flutter_pty.dart';
|
||||
import 'package:xterm/xterm.dart';
|
||||
|
||||
/// 单个终端会话的数据与状态模型
|
||||
/// 单个真实交互式终端会话的数据与状态模型(基于 原生 Unix PTY 伪终端)
|
||||
class TerminalSession {
|
||||
final String id;
|
||||
String name;
|
||||
final String distroId;
|
||||
final DateTime createdAt;
|
||||
final List<String> outputLines;
|
||||
final TextEditingController inputController;
|
||||
final ScrollController scrollController;
|
||||
final Terminal terminal;
|
||||
final FocusNode focusNode;
|
||||
|
||||
Pty? _pty;
|
||||
StreamSubscription? _ptyOutputSub;
|
||||
bool _isProcessRunning = false;
|
||||
String _inputBuffer = '';
|
||||
|
||||
TerminalSession({
|
||||
required this.id,
|
||||
this.name = '会话',
|
||||
this.distroId = 'alpine',
|
||||
DateTime? createdAt,
|
||||
List<String>? initialOutput,
|
||||
int maxLines = 10000,
|
||||
bool autoStartProcess = false,
|
||||
}) : createdAt = createdAt ?? DateTime.now(),
|
||||
outputLines = initialOutput ?? <String>[],
|
||||
inputController = TextEditingController(),
|
||||
scrollController = ScrollController(),
|
||||
focusNode = FocusNode();
|
||||
|
||||
/// 向终端输出追加内容
|
||||
void appendOutput(String line) {
|
||||
outputLines.add(line);
|
||||
_autoScrollToBottom();
|
||||
terminal = Terminal(maxLines: maxLines),
|
||||
focusNode = FocusNode() {
|
||||
_initTerminalEvents();
|
||||
if (autoStartProcess) {
|
||||
unawaited(startProcess());
|
||||
}
|
||||
}
|
||||
|
||||
/// 自动滚动到终端最底部
|
||||
void _autoScrollToBottom() {
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||
if (scrollController.hasClients) {
|
||||
scrollController.animateTo(
|
||||
scrollController.position.maxScrollExtent,
|
||||
duration: const Duration(milliseconds: 100),
|
||||
curve: Curves.easeOut,
|
||||
);
|
||||
bool get isProcessRunning => _isProcessRunning;
|
||||
|
||||
/// 获取终端缓冲区当前全部可见文本(常用于测试与日志断言)
|
||||
String get bufferText => terminal.buffer.getText();
|
||||
|
||||
void _initTerminalEvents() {
|
||||
// 监听输入直接送往 PTY
|
||||
terminal.onOutput = (data) {
|
||||
if (_pty != null && _isProcessRunning) {
|
||||
try {
|
||||
_pty!.write(utf8.encode(data));
|
||||
} catch (_) {}
|
||||
} else {
|
||||
_handleLocalEcho(data);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
// 监听终端视口尺寸变化并同步给底层 PTY
|
||||
terminal.onResize = (width, height, pixelWidth, pixelHeight) {
|
||||
if (_pty != null && _isProcessRunning) {
|
||||
try {
|
||||
_pty!.resize(height, width);
|
||||
} catch (_) {}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/// 清屏
|
||||
/// 当无底层进程挂接时的本地简易交互回显支持(保证脱机/本地开发时的基础交互与光标跳动)
|
||||
void _handleLocalEcho(String data) {
|
||||
for (int i = 0; i < data.length; i++) {
|
||||
final char = data[i];
|
||||
if (char == '\r' || char == '\n') {
|
||||
terminal.write('\r\n');
|
||||
_executeLocalCommand(_inputBuffer.trim());
|
||||
_inputBuffer = '';
|
||||
} else if (char == '\x7f' || char == '\b') {
|
||||
// 退格键
|
||||
if (_inputBuffer.isNotEmpty) {
|
||||
_inputBuffer = _inputBuffer.substring(0, _inputBuffer.length - 1);
|
||||
terminal.write('\b \b');
|
||||
}
|
||||
} else if (char.codeUnitAt(0) >= 32) {
|
||||
_inputBuffer += char;
|
||||
terminal.write(char);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void _executeLocalCommand(String cmd) {
|
||||
if (cmd.isEmpty) {
|
||||
terminal.write('\$ ');
|
||||
return;
|
||||
}
|
||||
if (cmd == 'clear') {
|
||||
clear();
|
||||
terminal.write('\$ ');
|
||||
return;
|
||||
}
|
||||
terminal.write('\x1b[33m[Host fallback]: executed "$cmd"\x1b[0m\r\n');
|
||||
terminal.write('\$ ');
|
||||
}
|
||||
|
||||
/// 启动底层真实 PTY 进程(连接 PRoot 容器或 Host Shell)
|
||||
Future<void> startProcess({DistroManager? distroManager, String? workspacePath}) async {
|
||||
final manager = distroManager ?? DistroManager();
|
||||
try {
|
||||
final config = await manager.buildLaunchConfig(
|
||||
systemName: distroId,
|
||||
workspacePath: workspacePath,
|
||||
);
|
||||
|
||||
final initialRows = terminal.viewHeight > 0 ? terminal.viewHeight : 24;
|
||||
final initialCols = terminal.viewWidth > 0 ? terminal.viewWidth : 80;
|
||||
|
||||
// 使用真实 Unix 伪终端 (PTY) 启动子进程
|
||||
_pty = Pty.start(
|
||||
config.executable,
|
||||
arguments: config.arguments,
|
||||
environment: config.environment,
|
||||
workingDirectory: config.workingDirectory,
|
||||
rows: initialRows,
|
||||
columns: initialCols,
|
||||
);
|
||||
_isProcessRunning = true;
|
||||
|
||||
// PTY 输出数据流实时解码并写入终端
|
||||
_ptyOutputSub = _pty!.output.listen(
|
||||
(bytes) {
|
||||
terminal.write(utf8.decode(bytes, allowMalformed: true));
|
||||
},
|
||||
onError: (err) {
|
||||
terminal.write('\r\n\x1b[31m[PTY Output error: $err]\x1b[0m\r\n');
|
||||
},
|
||||
onDone: () {
|
||||
_isProcessRunning = false;
|
||||
},
|
||||
);
|
||||
|
||||
_pty!.exitCode.then((code) {
|
||||
_isProcessRunning = false;
|
||||
terminal.write('\r\n\x1b[33m[Process exited with code $code]\x1b[0m\r\n');
|
||||
});
|
||||
} catch (e) {
|
||||
_isProcessRunning = false;
|
||||
terminal.write('\x1b[1;36mWelcome to Terminal [$distroId]\x1b[0m\r\n');
|
||||
terminal.write('\x1b[90m(PTY launch failed: $e. Running in local fallback mode.)\x1b[0m\r\n\r\n');
|
||||
terminal.write('\$ ');
|
||||
}
|
||||
}
|
||||
|
||||
/// 向终端写入文本(支持 VT100 / ANSI 转义序列)
|
||||
void write(String text) {
|
||||
terminal.write(text);
|
||||
}
|
||||
|
||||
/// 发送标准 ANSI 清屏并复位光标
|
||||
void clear() {
|
||||
outputLines.clear();
|
||||
terminal.write('\x1b[2J\x1b[H');
|
||||
}
|
||||
|
||||
/// 释放资源
|
||||
/// 释放 PTY 进程与焦点资源
|
||||
void dispose() {
|
||||
inputController.dispose();
|
||||
scrollController.dispose();
|
||||
_ptyOutputSub?.cancel();
|
||||
if (_pty != null) {
|
||||
try {
|
||||
_pty!.kill();
|
||||
} catch (_) {}
|
||||
_pty = null;
|
||||
}
|
||||
focusNode.dispose();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user