main.dart 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import 'dart:async';
  2. import 'package:flutter/material.dart';
  3. import 'package:xterm/flutter.dart';
  4. import 'package:xterm/xterm.dart';
  5. void main() {
  6. runApp(MyApp());
  7. }
  8. class MyApp extends StatelessWidget {
  9. @override
  10. Widget build(BuildContext context) {
  11. return MaterialApp(
  12. title: 'xterm.dart demo',
  13. theme: ThemeData(
  14. primarySwatch: Colors.blue,
  15. visualDensity: VisualDensity.adaptivePlatformDensity,
  16. ),
  17. home: MyHomePage(),
  18. );
  19. }
  20. }
  21. class MyHomePage extends StatefulWidget {
  22. MyHomePage({Key? key}) : super(key: key);
  23. @override
  24. _MyHomePageState createState() => _MyHomePageState();
  25. }
  26. class FakeTerminalBackend extends TerminalBackend {
  27. final _exitCodeCompleter = Completer<int>();
  28. // ignore: close_sinks
  29. final _outStream = StreamController<String>();
  30. @override
  31. bool get isReady => true;
  32. @override
  33. Future<int> get exitCode => _exitCodeCompleter.future;
  34. @override
  35. void init() {
  36. _outStream.sink.add('xterm.dart demo');
  37. _outStream.sink.add('\r\n');
  38. _outStream.sink.add('\$ ');
  39. }
  40. @override
  41. Stream<String> get out => _outStream.stream;
  42. @override
  43. void resize(int width, int height, int pixelWidth, int pixelHeight) {
  44. // NOOP
  45. }
  46. @override
  47. void write(String input) {
  48. if (input.length <= 0) {
  49. return;
  50. }
  51. // in a "real" terminal emulation you would connect onInput to the backend
  52. // (like a pty or ssh connection) that then handles the changes in the
  53. // terminal.
  54. // As we don't have a connected backend here we simulate the changes by
  55. // directly writing to the terminal.
  56. if (input == '\r') {
  57. _outStream.sink.add('\r\n');
  58. _outStream.sink.add('\$ ');
  59. } else if (input.codeUnitAt(0) == 127) {
  60. // Backspace handling
  61. _outStream.sink.add('\b \b');
  62. } else {
  63. _outStream.sink.add(input);
  64. }
  65. }
  66. @override
  67. void terminate() {
  68. //NOOP
  69. }
  70. @override
  71. void ackProcessed() {
  72. //NOOP
  73. }
  74. }
  75. class _MyHomePageState extends State<MyHomePage> {
  76. final terminal = Terminal(
  77. backend: FakeTerminalBackend(),
  78. maxLines: 10000,
  79. );
  80. void onInput(String input) {}
  81. @override
  82. Widget build(BuildContext context) {
  83. return Scaffold(
  84. body: SafeArea(
  85. child: TerminalView(
  86. terminal: terminal,
  87. style: TerminalStyle(fontFamily: ['Cascadia Mono']),
  88. ),
  89. ),
  90. );
  91. }
  92. }