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

63 lines
1.7 KiB
Dart
Raw Normal View History

2022-12-09 23:59:31 +00:00
import 'dart:convert';
import 'dart:io';
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,
backgroundOpacity: 0.7,
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
);
}
}