veilid/veilid-flutter/example/lib/log_terminal.dart

60 lines
1.6 KiB
Dart
Raw Normal View History

2022-12-09 23:59:31 +00:00
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:xterm/xterm.dart';
2022-12-10 17:11:46 +00:00
import 'log.dart';
2022-12-10 22:07:52 +00:00
import 'veilid_theme.dart';
const kDefaultTerminalStyle = TerminalStyle(
fontSize: kDefaultMonoTerminalFontSize,
height: kDefaultMonoTerminalFontHeight,
fontFamily: kDefaultMonoTerminalFontFamily);
2022-12-09 23:59:31 +00:00
2022-12-10 17:11:46 +00:00
class LogTerminal extends StatefulWidget {
const LogTerminal({Key? key}) : super(key: key);
2022-12-09 23:59:31 +00:00
@override
// ignore: library_private_types_in_public_api
2022-12-10 17:11:46 +00:00
_LogTerminalState createState() => _LogTerminalState();
2022-12-09 23:59:31 +00:00
}
2022-12-10 17:11:46 +00:00
class _LogTerminalState extends State<LogTerminal> {
2022-12-09 23:59:31 +00:00
final terminal = Terminal(
maxLines: 10000,
);
final terminalController = TerminalController();
@override
void initState() {
super.initState();
2022-12-10 17:11:46 +00:00
terminal.setLineFeedMode(true);
globalTerminalPrinter
.setCallback((log) => {terminal.write("${log.pretty()}\n")});
2022-12-09 23:59:31 +00:00
}
@override
Widget build(BuildContext context) {
2022-12-10 22:07:52 +00:00
return TerminalView(
terminal,
textStyle: kDefaultTerminalStyle,
controller: terminalController,
autofocus: true,
2022-12-11 00:11:58 +00:00
backgroundOpacity: 0.9,
2022-12-10 22:07:52 +00:00
onSecondaryTapDown: (details, offset) async {
final selection = terminalController.selection;
if (selection != null) {
final text = terminal.buffer.getText(selection);
terminalController.clearSelection();
await Clipboard.setData(ClipboardData(text: text));
} else {
final data = await Clipboard.getData('text/plain');
final text = data?.text;
if (text != null) {
terminal.paste(text);
}
}
},
2022-12-09 23:59:31 +00:00
);
}
}