isolate.dart 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. import 'dart:async';
  2. import 'package:flutter/material.dart';
  3. import 'package:xterm/flutter.dart';
  4. import 'package:xterm/isolate.dart';
  5. import 'package:xterm/theme/terminal_theme.dart';
  6. import 'package:xterm/theme/terminal_themes.dart';
  7. import 'package:xterm/xterm.dart';
  8. void main() {
  9. runApp(MyApp());
  10. }
  11. class MyApp extends StatelessWidget {
  12. @override
  13. Widget build(BuildContext context) {
  14. return MaterialApp(
  15. title: 'xterm.dart demo',
  16. theme: ThemeData(
  17. primarySwatch: Colors.blue,
  18. visualDensity: VisualDensity.adaptivePlatformDensity,
  19. ),
  20. home: MyHomePage(
  21. theme: TerminalThemes.defaultTheme,
  22. terminalOpacity: 0.8,
  23. ),
  24. );
  25. }
  26. }
  27. class MyHomePage extends StatefulWidget {
  28. MyHomePage({
  29. Key? key,
  30. required this.theme,
  31. required this.terminalOpacity,
  32. }) : super(key: key);
  33. final TerminalTheme theme;
  34. final double terminalOpacity;
  35. @override
  36. _MyHomePageState createState() => _MyHomePageState();
  37. }
  38. class FakeTerminalBackend extends TerminalBackend {
  39. // we do a late initialization of those backend members as the backend gets
  40. // transferred into the Isolate.
  41. // It is not allowed to e.g. transfer closures which we can not guarantee
  42. // to not exist in our member types.
  43. // The Isolate will call init() once it starts (from its context) and that is
  44. // the place where we initialize those members
  45. late final _exitCodeCompleter;
  46. // ignore: close_sinks
  47. late final _outStream;
  48. @override
  49. Future<int> get exitCode => _exitCodeCompleter.future;
  50. @override
  51. void init() {
  52. _exitCodeCompleter = Completer<int>();
  53. _outStream = StreamController<String>();
  54. _outStream.sink.add('xterm.dart demo');
  55. _outStream.sink.add('\r\n');
  56. _outStream.sink.add('\$ ');
  57. }
  58. @override
  59. Stream<String> get out => _outStream.stream;
  60. @override
  61. void resize(int width, int height, int pixelWidth, int pixelHeight) {
  62. // NOOP
  63. }
  64. @override
  65. void write(String input) {
  66. if (input.length <= 0) {
  67. return;
  68. }
  69. // in a "real" terminal emulation you would connect onInput to the backend
  70. // (like a pty or ssh connection) that then handles the changes in the
  71. // terminal.
  72. // As we don't have a connected backend here we simulate the changes by
  73. // directly writing to the terminal.
  74. if (input == '\r') {
  75. _outStream.sink.add('\r\n');
  76. _outStream.sink.add('\$ ');
  77. } else if (input.codeUnitAt(0) == 127) {
  78. // Backspace handling
  79. _outStream.sink.add('\b \b');
  80. } else {
  81. _outStream.sink.add(input);
  82. }
  83. }
  84. @override
  85. void terminate() {
  86. //NOOP
  87. }
  88. @override
  89. void ackProcessed() {
  90. //NOOP
  91. }
  92. }
  93. class _MyHomePageState extends State<MyHomePage> {
  94. TerminalIsolate? terminal;
  95. Future<TerminalIsolate> _ensureTerminalStarted() async {
  96. if (terminal == null) {
  97. terminal = TerminalIsolate(
  98. backend: FakeTerminalBackend(),
  99. maxLines: 10000,
  100. theme: widget.theme,
  101. );
  102. }
  103. if (!terminal!.isReady) {
  104. await terminal!.start();
  105. }
  106. return terminal!;
  107. }
  108. void onInput(String input) {}
  109. @override
  110. Widget build(BuildContext context) {
  111. return Scaffold(
  112. body: FutureBuilder(
  113. future: _ensureTerminalStarted(),
  114. builder: (context, snapshot) {
  115. return SafeArea(
  116. child: snapshot.hasData
  117. ? TerminalView(terminal: snapshot.data as TerminalIsolate)
  118. : Container(
  119. constraints: const BoxConstraints.expand(),
  120. color: Color(widget.theme.background)
  121. .withOpacity(widget.terminalOpacity),
  122. ),
  123. );
  124. },
  125. ),
  126. );
  127. }
  128. }