From e4dbc3ba12b1acff2d1570a58443af1f5b3434cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?lollipopkit=F0=9F=8F=B3=EF=B8=8F=E2=80=8D=E2=9A=A7?= =?UTF-8?q?=EF=B8=8F?= <10864310+lollipopkit@users.noreply.github.com> Date: Tue, 23 Jul 2024 21:34:34 +0800 Subject: [PATCH] feat: set envs in term (#485) --- lib/core/route.dart | 5 +- .../model/server/server_private_info.dart | 18 +++ .../model/server/server_private_info.g.dart | 7 +- lib/l10n/app_de.arb | 1 + lib/l10n/app_en.arb | 1 + lib/l10n/app_es.arb | 1 + lib/l10n/app_fr.arb | 1 + lib/l10n/app_id.arb | 1 + lib/l10n/app_ja.arb | 1 + lib/l10n/app_nl.arb | 1 + lib/l10n/app_pt.arb | 1 + lib/l10n/app_ru.arb | 1 + lib/l10n/app_zh.arb | 1 + lib/l10n/app_zh_tw.arb | 1 + lib/view/page/server/edit.dart | 133 +++++++++++------- lib/view/page/ssh/page.dart | 3 +- pubspec.lock | 4 +- pubspec.yaml | 2 +- 18 files changed, 124 insertions(+), 59 deletions(-) diff --git a/lib/core/route.dart b/lib/core/route.dart index 4e262dd7..7821d242 100644 --- a/lib/core/route.dart +++ b/lib/core/route.dart @@ -250,6 +250,9 @@ class AppRoutes { } static AppRoutes kvEditor({Key? key, required Map data}) { - return AppRoutes(KvEditor(key: key, data: data), 'kv_editor'); + return AppRoutes( + KvEditor(key: key, args: KvEditorArgs(data: data)), + 'kv_editor', + ); } } diff --git a/lib/data/model/server/server_private_info.dart b/lib/data/model/server/server_private_info.dart index 3fdc3307..c3d38267 100644 --- a/lib/data/model/server/server_private_info.dart +++ b/lib/data/model/server/server_private_info.dart @@ -42,6 +42,10 @@ class ServerPrivateInfo { @HiveField(11) final WakeOnLanCfg? wolCfg; + /// It only applies to SSH terminal. + @HiveField(12) + final Map? envs; + final String id; const ServerPrivateInfo({ @@ -57,6 +61,7 @@ class ServerPrivateInfo { this.jumpId, this.custom, this.wolCfg, + this.envs, }) : id = '$user@$ip:$port'; static ServerPrivateInfo fromJson(Map json) { @@ -76,6 +81,15 @@ class ServerPrivateInfo { final wolCfg = json["wolCfg"] == null ? null : WakeOnLanCfg.fromJson(json["wolCfg"].cast()); + final envs_ = json["envs"] as Map?; + final envs = {}; + if (envs_ != null) { + envs_.forEach((key, value) { + if (value is String) { + envs[key] = value; + } + }); + } return ServerPrivateInfo( name: name, @@ -90,6 +104,7 @@ class ServerPrivateInfo { jumpId: jumpId, custom: custom, wolCfg: wolCfg, + envs: envs.isEmpty ? null : envs, ); } @@ -123,6 +138,9 @@ class ServerPrivateInfo { if (wolCfg != null) { data["wolCfg"] = wolCfg?.toJson(); } + if (envs != null) { + data["envs"] = envs; + } return data; } diff --git a/lib/data/model/server/server_private_info.g.dart b/lib/data/model/server/server_private_info.g.dart index c91bdd5a..c79d1529 100644 --- a/lib/data/model/server/server_private_info.g.dart +++ b/lib/data/model/server/server_private_info.g.dart @@ -29,13 +29,14 @@ class ServerPrivateInfoAdapter extends TypeAdapter { jumpId: fields[9] as String?, custom: fields[10] as ServerCustom?, wolCfg: fields[11] as WakeOnLanCfg?, + envs: (fields[12] as Map?)?.cast(), ); } @override void write(BinaryWriter writer, ServerPrivateInfo obj) { writer - ..writeByte(12) + ..writeByte(13) ..writeByte(0) ..write(obj.name) ..writeByte(1) @@ -59,7 +60,9 @@ class ServerPrivateInfoAdapter extends TypeAdapter { ..writeByte(10) ..write(obj.custom) ..writeByte(11) - ..write(obj.wolCfg); + ..write(obj.wolCfg) + ..writeByte(12) + ..write(obj.envs); } @override diff --git a/lib/l10n/app_de.arb b/lib/l10n/app_de.arb index fcb34ee9..03312a7f 100644 --- a/lib/l10n/app_de.arb +++ b/lib/l10n/app_de.arb @@ -92,6 +92,7 @@ "editor": "Editor", "editorHighlightTip": "Die Leistung der aktuellen Codehervorhebung ist schlechter und kann zur Verbesserung optional ausgeschaltet werden.", "encode": "Encode", + "envVars": "Umgebungsvariable", "error": "Fehler", "exampleName": "Servername", "experimentalFeature": "Experimentelles Feature", diff --git a/lib/l10n/app_en.arb b/lib/l10n/app_en.arb index 9fc0b443..aea619b1 100644 --- a/lib/l10n/app_en.arb +++ b/lib/l10n/app_en.arb @@ -92,6 +92,7 @@ "editor": "Editor", "editorHighlightTip": "The current code highlighting performance is not ideal and can be optionally turned off to improve.", "encode": "Encode", + "envVars": "Environment variable", "error": "Error", "exampleName": "Example name", "experimentalFeature": "Experimental feature", diff --git a/lib/l10n/app_es.arb b/lib/l10n/app_es.arb index 35177c23..c2dcf2fa 100644 --- a/lib/l10n/app_es.arb +++ b/lib/l10n/app_es.arb @@ -92,6 +92,7 @@ "editor": "Editor", "editorHighlightTip": "El rendimiento del resaltado de código es bastante pobre actualmente, puedes elegir desactivarlo para mejorar.", "encode": "Codificar", + "envVars": "Variable de entorno", "error": "Error", "exampleName": "Ejemplo de nombre", "experimentalFeature": "Función experimental", diff --git a/lib/l10n/app_fr.arb b/lib/l10n/app_fr.arb index a4890ed8..c47d7491 100644 --- a/lib/l10n/app_fr.arb +++ b/lib/l10n/app_fr.arb @@ -92,6 +92,7 @@ "editor": "Éditeur", "editorHighlightTip": "La performance actuelle de mise en surbrillance du code est pire et peut être désactivée en option pour s'améliorer.", "encode": "Encoder", + "envVars": "Variable d’environnement", "error": "Erreur", "exampleName": "Nom de l'exemple", "experimentalFeature": "Fonctionnalité expérimentale", diff --git a/lib/l10n/app_id.arb b/lib/l10n/app_id.arb index ff5a30aa..81e59a77 100644 --- a/lib/l10n/app_id.arb +++ b/lib/l10n/app_id.arb @@ -92,6 +92,7 @@ "editor": "Editor", "editorHighlightTip": "Performa penyorotan kode saat ini lebih buruk, dan dapat dimatikan secara opsional untuk perbaikan.", "encode": "Menyandi", + "envVars": "Variabel lingkungan", "error": "Kesalahan", "exampleName": "Nama contoh", "experimentalFeature": "Fitur eksperimental", diff --git a/lib/l10n/app_ja.arb b/lib/l10n/app_ja.arb index bf9764ee..d7d85aaa 100644 --- a/lib/l10n/app_ja.arb +++ b/lib/l10n/app_ja.arb @@ -92,6 +92,7 @@ "editor": "エディター", "editorHighlightTip": "現在のコードハイライトのパフォーマンスはかなり悪いため、改善するために無効にすることを選択できます。", "encode": "エンコード", + "envVars": "環境変数", "error": "エラー", "exampleName": "名前例", "experimentalFeature": "実験的な機能", diff --git a/lib/l10n/app_nl.arb b/lib/l10n/app_nl.arb index 2e06ce0e..a96c9530 100644 --- a/lib/l10n/app_nl.arb +++ b/lib/l10n/app_nl.arb @@ -92,6 +92,7 @@ "editor": "Editor", "editorHighlightTip": "De huidige codehighlighting-prestaties zijn slechter en kunnen optioneel worden uitgeschakeld om te verbeteren.", "encode": "Coderen", + "envVars": "Omgevingsvariabele", "error": "Fout", "exampleName": "Voorbeeldnaam", "experimentalFeature": "Experimentele functie", diff --git a/lib/l10n/app_pt.arb b/lib/l10n/app_pt.arb index 3c8ae7d6..bd629918 100644 --- a/lib/l10n/app_pt.arb +++ b/lib/l10n/app_pt.arb @@ -92,6 +92,7 @@ "editor": "Editor", "editorHighlightTip": "O desempenho do destaque de código atualmente é ruim, pode optar por desativá-lo para melhorar.", "encode": "Codificar", + "envVars": "Variável de ambiente", "error": "Erro", "exampleName": "Exemplo de nome", "experimentalFeature": "Recurso experimental", diff --git a/lib/l10n/app_ru.arb b/lib/l10n/app_ru.arb index 83b9b1b6..7dbf79fd 100644 --- a/lib/l10n/app_ru.arb +++ b/lib/l10n/app_ru.arb @@ -92,6 +92,7 @@ "editor": "редактор", "editorHighlightTip": "Текущая производительность подсветки кода неудовлетворительна, можно отключить для улучшения.", "encode": "кодировать", + "envVars": "Переменная окружения", "error": "ошибка", "exampleName": "пример имени", "experimentalFeature": "экспериментальная функция", diff --git a/lib/l10n/app_zh.arb b/lib/l10n/app_zh.arb index 42581477..2c0ce88c 100644 --- a/lib/l10n/app_zh.arb +++ b/lib/l10n/app_zh.arb @@ -92,6 +92,7 @@ "editor": "编辑器", "editorHighlightTip": "目前的代码高亮性能较为糟糕,可以选择关闭以改善。", "encode": "编码", + "envVars": "环境变量", "error": "错误", "exampleName": "名称示例", "experimentalFeature": "实验性功能", diff --git a/lib/l10n/app_zh_tw.arb b/lib/l10n/app_zh_tw.arb index 5d6f871f..76372760 100644 --- a/lib/l10n/app_zh_tw.arb +++ b/lib/l10n/app_zh_tw.arb @@ -92,6 +92,7 @@ "editor": "編輯器", "editorHighlightTip": "目前的代碼高亮性能較為糟糕,可以選擇關閉以改善。", "encode": "編碼", + "envVars": "環境變量", "error": "錯誤", "exampleName": "名稱範例", "experimentalFeature": "實驗性功能", diff --git a/lib/view/page/server/edit.dart b/lib/view/page/server/edit.dart index 914bf188..4c3fb0bf 100644 --- a/lib/view/page/server/edit.dart +++ b/lib/view/page/server/edit.dart @@ -22,7 +22,7 @@ class ServerEditPage extends StatefulWidget { State createState() => _ServerEditPageState(); } -class _ServerEditPageState extends State { +class _ServerEditPageState extends State with AfterLayoutMixin { final _nameController = TextEditingController(); final _ipController = TextEditingController(); final _altUrlController = TextEditingController(); @@ -49,58 +49,10 @@ class _ServerEditPageState extends State { final _autoConnect = ValueNotifier(true); final _jumpServer = ValueNotifier(null); final _pveIgnoreCert = ValueNotifier(false); + final _env = {}.vn; var _tags = []; - @override - void initState() { - super.initState(); - - final spi = widget.spi; - if (spi != null) { - _nameController.text = spi.name; - _ipController.text = spi.ip; - _portController.text = spi.port.toString(); - _usernameController.text = spi.user; - if (spi.keyId == null) { - _passwordController.text = spi.pwd ?? ''; - } else { - _keyIdx.value = Pros.key.pkis.indexWhere( - (e) => e.id == widget.spi!.keyId, - ); - } - - /// List in dart is passed by pointer, so you need to copy it here - _tags.addAll(spi.tags ?? []); - - _altUrlController.text = spi.alterUrl ?? ''; - _autoConnect.value = spi.autoConnect ?? true; - _jumpServer.value = spi.jumpId; - - final custom = spi.custom; - if (custom != null) { - _pveAddrCtrl.text = custom.pveAddr ?? ''; - _pveIgnoreCert.value = custom.pveIgnoreCert; - try { - // Add a null check here to prevent setting `null` to the controller - final encoded = json.encode(custom.cmds!); - if (encoded.isNotEmpty) { - _customCmdCtrl.text = encoded; - } - } catch (_) {} - _preferTempDevCtrl.text = custom.preferTempDev ?? ''; - _logoUrlCtrl.text = custom.logoUrl ?? ''; - } - - final wol = spi.wolCfg; - if (wol != null) { - _wolMacCtrl.text = wol.mac; - _wolIpCtrl.text = wol.ip; - _wolPwdCtrl.text = wol.pwd ?? ''; - } - } - } - @override void dispose() { super.dispose(); @@ -246,6 +198,7 @@ class _ServerEditPageState extends State { ), ), _buildAuth(), + _buildEnvs(), _buildJumpServer(), _buildMore(), ]; @@ -302,12 +255,13 @@ class _ServerEditPageState extends State { } Widget _buildKeyAuth() { + const padding = EdgeInsets.only(left: 23, right: 13); return Consumer( builder: (_, key, __) { final tiles = List.generate(key.pkis.length, (index) { final e = key.pkis[index]; return ListTile( - contentPadding: const EdgeInsets.symmetric(horizontal: 17), + contentPadding: padding, leading: Text( '#${index + 1}', style: const TextStyle(fontWeight: FontWeight.bold, fontSize: 15), @@ -329,7 +283,7 @@ class _ServerEditPageState extends State { tiles.add( ListTile( title: Text(l10n.addPrivateKey), - contentPadding: const EdgeInsets.symmetric(horizontal: 17), + contentPadding: padding, trailing: const Padding( padding: EdgeInsets.only(right: 13), child: Icon(Icons.add), @@ -347,6 +301,31 @@ class _ServerEditPageState extends State { ); } + Widget _buildEnvs() { + return _env.listenVal((val) { + final subtitle = val.isEmpty + ? null + : Text(val.keys.join(','), style: UIs.textGrey); + return ListTile( + leading: const Padding( + padding: EdgeInsets.only(left: 10), + child: Icon(HeroIcons.variable), + ), + subtitle: subtitle, + title: Text(l10n.envVars), + trailing: const Icon(Icons.keyboard_arrow_right), + onTap: () async { + final res = await KvEditor.route.go( + context, + args: KvEditorArgs(data: widget.spi?.envs ?? {}), + ); + if (res == null) return; + _env.value = res; + }, + ).cardx; + }); + } + Widget _buildMore() { return ExpandTile( title: Text(l10n.more), @@ -630,6 +609,7 @@ class _ServerEditPageState extends State { jumpId: _jumpServer.value, custom: custom, wolCfg: wol, + envs: _env.value.isEmpty ? null : _env.value, ); if (widget.spi == null) { @@ -655,4 +635,53 @@ class _ServerEditPageState extends State { trailing: const Icon(Icons.keyboard_arrow_right), ).cardx; } + + @override + void afterFirstLayout(BuildContext context) { + final spi = widget.spi; + if (spi != null) { + _nameController.text = spi.name; + _ipController.text = spi.ip; + _portController.text = spi.port.toString(); + _usernameController.text = spi.user; + if (spi.keyId == null) { + _passwordController.text = spi.pwd ?? ''; + } else { + _keyIdx.value = Pros.key.pkis.indexWhere( + (e) => e.id == widget.spi!.keyId, + ); + } + + /// List in dart is passed by pointer, so you need to copy it here + _tags.addAll(spi.tags ?? []); + + _altUrlController.text = spi.alterUrl ?? ''; + _autoConnect.value = spi.autoConnect ?? true; + _jumpServer.value = spi.jumpId; + + final custom = spi.custom; + if (custom != null) { + _pveAddrCtrl.text = custom.pveAddr ?? ''; + _pveIgnoreCert.value = custom.pveIgnoreCert; + try { + // Add a null check here to prevent setting `null` to the controller + final encoded = json.encode(custom.cmds!); + if (encoded.isNotEmpty) { + _customCmdCtrl.text = encoded; + } + } catch (_) {} + _preferTempDevCtrl.text = custom.preferTempDev ?? ''; + _logoUrlCtrl.text = custom.logoUrl ?? ''; + } + + final wol = spi.wolCfg; + if (wol != null) { + _wolMacCtrl.text = wol.mac; + _wolIpCtrl.text = wol.ip; + _wolPwdCtrl.text = wol.pwd ?? ''; + } + + _env.value = spi.envs ?? {}; + } + } } diff --git a/lib/view/page/ssh/page.dart b/lib/view/page/ssh/page.dart index ade4098a..d21ba857 100644 --- a/lib/view/page/ssh/page.dart +++ b/lib/view/page/ssh/page.dart @@ -393,12 +393,13 @@ class SSHPageState extends State width: _terminal.viewWidth, height: _terminal.viewHeight, ), + environment: widget.spi.envs, ); //_setupDiscontinuityTimer(); if (session == null) { - _writeLn('Null session'); + _writeLn('Null session, please back and retry\r\n'); return; } diff --git a/pubspec.lock b/pubspec.lock index d395406a..4f565dba 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -385,8 +385,8 @@ packages: dependency: "direct main" description: path: "." - ref: "v1.0.82" - resolved-ref: "424b976ff383222f49cd6053c79340597a5a8429" + ref: "v1.0.84" + resolved-ref: "4069be5204fb1866ca72b702df47fa334b189d1a" url: "https://github.com/lppcg/fl_lib" source: git version: "0.0.1" diff --git a/pubspec.yaml b/pubspec.yaml index 9bf8605f..053ea486 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -61,7 +61,7 @@ dependencies: fl_lib: git: url: https://github.com/lppcg/fl_lib - ref: v1.0.82 + ref: v1.0.84 dependency_overrides: # dartssh2: