89882d90f3
The card's output area is now an xterm.dart emulator kept per run by the controller: it is fed the raw stream in order, takes typing and paste while the command runs (with a key toolbar and a soft-keyboard field on touch platforms), resizes the process to its layout, opens printed links, shrinks finished runs to their output and opens full screen. A reconnect resumes from the byte the card holds; Copy output copies the server's plain text.
230 lines
6.0 KiB
Dart
230 lines
6.0 KiB
Dart
import 'package:flutter/cupertino.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter/services.dart';
|
|
import 'package:xterm/core.dart';
|
|
|
|
import 'chat_shell_terminal_session.dart';
|
|
|
|
/// One key of the terminal toolbar: its label, icons and what it sends.
|
|
typedef _Key = ({
|
|
String id,
|
|
String label,
|
|
IconData? material,
|
|
IconData? cupertino,
|
|
void Function(Terminal terminal) send,
|
|
});
|
|
|
|
final List<_Key> _keys = [
|
|
(
|
|
id: 'enter',
|
|
label: 'Enter',
|
|
material: Icons.keyboard_return,
|
|
cupertino: CupertinoIcons.return_icon,
|
|
send: (t) => t.keyInput(TerminalKey.enter),
|
|
),
|
|
(
|
|
id: 'ctrl-c',
|
|
label: 'Ctrl-C',
|
|
material: null,
|
|
cupertino: null,
|
|
send: (t) => t.textInput('\x03'),
|
|
),
|
|
(
|
|
id: 'tab',
|
|
label: 'Tab',
|
|
material: Icons.keyboard_tab,
|
|
cupertino: CupertinoIcons.arrow_right_to_line,
|
|
send: (t) => t.keyInput(TerminalKey.tab),
|
|
),
|
|
(
|
|
id: 'esc',
|
|
label: 'Esc',
|
|
material: null,
|
|
cupertino: null,
|
|
send: (t) => t.keyInput(TerminalKey.escape),
|
|
),
|
|
(
|
|
id: 'left',
|
|
label: 'Left',
|
|
material: Icons.keyboard_arrow_left,
|
|
cupertino: CupertinoIcons.arrow_left,
|
|
send: (t) => t.keyInput(TerminalKey.arrowLeft),
|
|
),
|
|
(
|
|
id: 'up',
|
|
label: 'Up',
|
|
material: Icons.keyboard_arrow_up,
|
|
cupertino: CupertinoIcons.arrow_up,
|
|
send: (t) => t.keyInput(TerminalKey.arrowUp),
|
|
),
|
|
(
|
|
id: 'down',
|
|
label: 'Down',
|
|
material: Icons.keyboard_arrow_down,
|
|
cupertino: CupertinoIcons.arrow_down,
|
|
send: (t) => t.keyInput(TerminalKey.arrowDown),
|
|
),
|
|
(
|
|
id: 'right',
|
|
label: 'Right',
|
|
material: Icons.keyboard_arrow_right,
|
|
cupertino: CupertinoIcons.arrow_right,
|
|
send: (t) => t.keyInput(TerminalKey.arrowRight),
|
|
),
|
|
];
|
|
|
|
/// The keys a touch keyboard lacks, and Paste, for a running terminal; with
|
|
/// [field], also a text line whose text is typed into the process on Enter
|
|
/// (the soft keyboard's own way in). The field is emptied as soon as its
|
|
/// text is sent and can hide what is typed, for a token or password.
|
|
class ChatShellTerminalKeys extends StatefulWidget {
|
|
const ChatShellTerminalKeys({
|
|
super.key,
|
|
required this.session,
|
|
required this.cupertino,
|
|
required this.field,
|
|
});
|
|
|
|
final ChatShellTerminalSession session;
|
|
final bool cupertino;
|
|
final bool field;
|
|
|
|
@override
|
|
State<ChatShellTerminalKeys> createState() => _ChatShellTerminalKeysState();
|
|
}
|
|
|
|
class _ChatShellTerminalKeysState extends State<ChatShellTerminalKeys> {
|
|
final TextEditingController _text = TextEditingController();
|
|
bool _hidden = false;
|
|
|
|
@override
|
|
void dispose() {
|
|
_text.dispose();
|
|
super.dispose();
|
|
}
|
|
|
|
Terminal get _terminal => widget.session.terminal;
|
|
|
|
Future<void> _paste() async {
|
|
final data = await Clipboard.getData(Clipboard.kTextPlain);
|
|
final text = data?.text;
|
|
if (text != null && text.isNotEmpty && widget.session.live) {
|
|
_terminal.paste(text);
|
|
}
|
|
}
|
|
|
|
/// Types the field's text and Enter, then forgets it.
|
|
void _submit() {
|
|
final text = _text.text;
|
|
_text.clear();
|
|
if (text.isNotEmpty) _terminal.textInput(text);
|
|
_terminal.keyInput(TerminalKey.enter);
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final keys = Wrap(
|
|
spacing: 4,
|
|
runSpacing: 4,
|
|
children: [
|
|
_button(
|
|
'paste',
|
|
'Paste',
|
|
Icons.content_paste,
|
|
CupertinoIcons.doc_on_clipboard,
|
|
_paste,
|
|
),
|
|
for (final key in _keys)
|
|
_button(
|
|
key.id,
|
|
key.label,
|
|
key.material,
|
|
key.cupertino,
|
|
() => key.send(_terminal),
|
|
),
|
|
],
|
|
);
|
|
if (!widget.field) return keys;
|
|
return Column(
|
|
crossAxisAlignment: CrossAxisAlignment.stretch,
|
|
mainAxisSize: MainAxisSize.min,
|
|
children: [
|
|
Padding(padding: const EdgeInsets.only(bottom: 8), child: _field()),
|
|
keys,
|
|
],
|
|
);
|
|
}
|
|
|
|
Widget _field() {
|
|
final toggle = _hidden ? 'Show typing' : 'Hide typing';
|
|
if (widget.cupertino) {
|
|
return CupertinoTextField(
|
|
key: const Key('chat-shell-terminal-field'),
|
|
controller: _text,
|
|
placeholder: 'Type input, then Enter',
|
|
obscureText: _hidden,
|
|
autocorrect: false,
|
|
enableSuggestions: false,
|
|
textInputAction: TextInputAction.send,
|
|
onSubmitted: (_) => _submit(),
|
|
suffix: CupertinoButton(
|
|
key: const Key('chat-shell-terminal-hide'),
|
|
padding: const EdgeInsets.symmetric(horizontal: 8),
|
|
onPressed: () => setState(() => _hidden = !_hidden),
|
|
child: Icon(
|
|
_hidden ? CupertinoIcons.eye : CupertinoIcons.eye_slash,
|
|
semanticLabel: toggle,
|
|
),
|
|
),
|
|
);
|
|
}
|
|
return TextField(
|
|
key: const Key('chat-shell-terminal-field'),
|
|
controller: _text,
|
|
obscureText: _hidden,
|
|
autocorrect: false,
|
|
enableSuggestions: false,
|
|
textInputAction: TextInputAction.send,
|
|
onSubmitted: (_) => _submit(),
|
|
decoration: InputDecoration(
|
|
labelText: 'Type input, then Enter',
|
|
suffixIcon: IconButton(
|
|
key: const Key('chat-shell-terminal-hide'),
|
|
tooltip: toggle,
|
|
onPressed: () => setState(() => _hidden = !_hidden),
|
|
icon: Icon(_hidden ? Icons.visibility : Icons.visibility_off),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
Widget _button(
|
|
String id,
|
|
String label,
|
|
IconData? material,
|
|
IconData? cupertino,
|
|
VoidCallback onPressed,
|
|
) {
|
|
final key = Key('chat-shell-key-$id');
|
|
if (widget.cupertino) {
|
|
return CupertinoButton(
|
|
key: key,
|
|
padding: const EdgeInsets.symmetric(horizontal: 8),
|
|
onPressed: onPressed,
|
|
child: cupertino == null
|
|
? Text(label)
|
|
: Icon(cupertino, semanticLabel: label),
|
|
);
|
|
}
|
|
if (material == null) {
|
|
return OutlinedButton(key: key, onPressed: onPressed, child: Text(label));
|
|
}
|
|
return IconButton.outlined(
|
|
key: key,
|
|
tooltip: label,
|
|
onPressed: onPressed,
|
|
icon: Icon(material),
|
|
);
|
|
}
|
|
}
|