mirror of
https://github.com/lollipopkit/flutter_server_box.git
synced 2025-12-17 23:34:24 +01:00
opt.: custom terminal emulator (#771)
This commit is contained in:
@@ -22,10 +22,7 @@ import 'package:server_box/view/page/systemd.dart';
|
||||
class ServerFuncBtnsTopRight extends StatelessWidget {
|
||||
final Spi spi;
|
||||
|
||||
const ServerFuncBtnsTopRight({
|
||||
super.key,
|
||||
required this.spi,
|
||||
});
|
||||
const ServerFuncBtnsTopRight({super.key, required this.spi});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
@@ -38,10 +35,7 @@ class ServerFuncBtnsTopRight extends StatelessWidget {
|
||||
}
|
||||
|
||||
class ServerFuncBtns extends StatelessWidget {
|
||||
const ServerFuncBtns({
|
||||
super.key,
|
||||
required this.spi,
|
||||
});
|
||||
const ServerFuncBtns({super.key, required this.spi});
|
||||
|
||||
final Spi spi;
|
||||
|
||||
@@ -86,7 +80,7 @@ class ServerFuncBtns extends StatelessWidget {
|
||||
padding: EdgeInsets.zero,
|
||||
icon: Icon(e.icon, size: 17),
|
||||
),
|
||||
Text(e.toStr, style: UIs.text11Grey)
|
||||
Text(e.toStr, style: UIs.text11Grey),
|
||||
],
|
||||
),
|
||||
);
|
||||
@@ -107,11 +101,7 @@ class ServerFuncBtns extends StatelessWidget {
|
||||
}
|
||||
}
|
||||
|
||||
void _onTapMoreBtns(
|
||||
ServerFuncBtn value,
|
||||
Spi spi,
|
||||
BuildContext context,
|
||||
) async {
|
||||
void _onTapMoreBtns(ServerFuncBtn value, Spi spi, BuildContext context) async {
|
||||
// final isMobile = ResponsiveBreakpoints.of(context).isMobile;
|
||||
switch (value) {
|
||||
// case ServerFuncBtn.pkg:
|
||||
@@ -153,16 +143,8 @@ void _onTapMoreBtns(
|
||||
final fmted = snippet.fmtWithSpi(spi);
|
||||
final sure = await context.showRoundDialog<bool>(
|
||||
title: libL10n.attention,
|
||||
child: SingleChildScrollView(
|
||||
child: SimpleMarkdown(data: '```shell\n$fmted\n```'),
|
||||
),
|
||||
actions: [
|
||||
CountDownBtn(
|
||||
onTap: () => context.pop(true),
|
||||
text: l10n.run,
|
||||
afterColor: Colors.red,
|
||||
),
|
||||
],
|
||||
child: SingleChildScrollView(child: SimpleMarkdown(data: '```shell\n$fmted\n```')),
|
||||
actions: [CountDownBtn(onTap: () => context.pop(true), text: l10n.run, afterColor: Colors.red)],
|
||||
);
|
||||
if (sure != true) return;
|
||||
if (!_checkClient(context, spi.id)) return;
|
||||
@@ -178,13 +160,13 @@ void _onTapMoreBtns(
|
||||
case ServerFuncBtn.container:
|
||||
if (!_checkClient(context, spi.id)) return;
|
||||
final args = SpiRequiredArgs(spi);
|
||||
if (isMobile) {
|
||||
ContainerPage.route.go(context, args);
|
||||
} else {
|
||||
SplitViewNavigator.of(context)?.replace(
|
||||
ContainerPage.route.toWidget(args: args),
|
||||
);
|
||||
}
|
||||
// if (isMobile) {
|
||||
ContainerPage.route.go(context, args);
|
||||
// } else {
|
||||
// SplitViewNavigator.of(
|
||||
// context,
|
||||
// )?.replace(ContainerPage.route.toWidget(args: args));
|
||||
// }
|
||||
break;
|
||||
case ServerFuncBtn.process:
|
||||
if (!_checkClient(context, spi.id)) return;
|
||||
@@ -260,7 +242,23 @@ void _gotoSSH(Spi spi, BuildContext context) async {
|
||||
await Process.start('cmd', ['/c', 'start'] + sshCommand);
|
||||
break;
|
||||
case Pfs.linux:
|
||||
await Process.start('x-terminal-emulator', ['-e'] + sshCommand);
|
||||
final scriptFile = File('${Directory.systemTemp.path}/srvbox_launch_term.sh');
|
||||
await scriptFile.writeAsString(_runEmulatorShell);
|
||||
|
||||
if (Platform.isLinux || Platform.isMacOS) {
|
||||
await Process.run('chmod', ['+x', scriptFile.path]);
|
||||
}
|
||||
|
||||
try {
|
||||
var terminal = Stores.setting.desktopTerminal.fetch();
|
||||
if (terminal.isEmpty) terminal = 'x-terminal-emulator';
|
||||
|
||||
await Process.start(scriptFile.path, [terminal, ...sshCommand]);
|
||||
} catch (e, s) {
|
||||
context.showErrDialog(e, s, l10n.emulator);
|
||||
} finally {
|
||||
await scriptFile.delete();
|
||||
}
|
||||
break;
|
||||
default:
|
||||
context.showSnackBar('Mismatch system: $system');
|
||||
@@ -280,3 +278,62 @@ bool _checkClient(BuildContext context, String id) {
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
const _runEmulatorShell = '''
|
||||
#!/bin/sh
|
||||
# launch_terminal.sh
|
||||
|
||||
TERMINAL="\$1"
|
||||
shift # Remove the first argument (terminal name)
|
||||
|
||||
# Auto detect terminal if not provided
|
||||
if [ -z "\$TERMINAL" ] || [ "\$TERMINAL" = "x-terminal-emulator" ]; then
|
||||
# Follow the order of preference
|
||||
for term in kitty alacritty gnome-terminal konsole xfce4-terminal terminator tilix wezterm foot; do
|
||||
if command -v "\$term" >/dev/null 2>&1; then
|
||||
TERMINAL="\$term"
|
||||
break
|
||||
fi
|
||||
done
|
||||
|
||||
[ -z "\$TERMINAL" ] && TERMINAL="x-terminal-emulator"
|
||||
fi
|
||||
|
||||
case "\$TERMINAL" in
|
||||
gnome-terminal)
|
||||
exec "\$TERMINAL" -- "\$@"
|
||||
;;
|
||||
konsole|terminator|tilix)
|
||||
exec "\$TERMINAL" -e "\$@"
|
||||
;;
|
||||
xfce4-terminal)
|
||||
exec "\$TERMINAL" -e "\$*"
|
||||
;;
|
||||
alacritty)
|
||||
# Check alacritty version
|
||||
if "\$TERMINAL" --version 2>&1 | grep -q "alacritty 0\\.1[3-9]"; then
|
||||
# 0.13.0+
|
||||
exec "\$TERMINAL" --command "\$@"
|
||||
else
|
||||
# Old versions
|
||||
exec "\$TERMINAL" -e "\$@"
|
||||
fi
|
||||
;;
|
||||
kitty)
|
||||
exec "\$TERMINAL" "\$@"
|
||||
;;
|
||||
wezterm)
|
||||
exec "\$TERMINAL" start -- "\$@"
|
||||
;;
|
||||
foot)
|
||||
exec "\$TERMINAL" "\$@"
|
||||
;;
|
||||
urxvt|rxvt-unicode)
|
||||
exec "\$TERMINAL" -e "\$@"
|
||||
;;
|
||||
x-terminal-emulator|*)
|
||||
# Default
|
||||
exec "\$TERMINAL" -e "\$@"
|
||||
;;
|
||||
esac
|
||||
''';
|
||||
|
||||
Reference in New Issue
Block a user