veilid/veilid-flutter/example/lib/log_terminal.dart
2022-12-10 12:11:46 -05:00

61 lines
1.6 KiB
Dart

import 'dart:convert';
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:xterm/xterm.dart';
import 'log.dart';
class LogTerminal extends StatefulWidget {
const LogTerminal({Key? key}) : super(key: key);
@override
// ignore: library_private_types_in_public_api
_LogTerminalState createState() => _LogTerminalState();
}
class _LogTerminalState extends State<LogTerminal> {
final terminal = Terminal(
maxLines: 10000,
);
final terminalController = TerminalController();
@override
void initState() {
super.initState();
terminal.setLineFeedMode(true);
globalTerminalPrinter
.setCallback((log) => {terminal.write("${log.pretty()}\n")});
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.transparent,
body: SafeArea(
child: TerminalView(
terminal,
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);
}
}
},
),
),
);
}
}