main.dart 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. Completer<int> _exitCodeCompleter = Completer<int>();
  28. // ignore: close_sinks
  29. StreamController<String> _outStream = StreamController<String>();
  30. @override
  31. Future<int> get exitCode => _exitCodeCompleter.future;
  32. @override
  33. void init() {
  34. _outStream.sink.add('xterm.dart demo');
  35. _outStream.sink.add('\r\n');
  36. _outStream.sink.add('\$ ');
  37. }
  38. @override
  39. Stream<String> get out => _outStream.stream;
  40. @override
  41. void resize(int width, int height, int pixelWidth, int pixelHeight) {
  42. // NOOP
  43. }
  44. @override
  45. void write(String input) {
  46. if (input.length <= 0) {
  47. return;
  48. }
  49. // in a "real" terminal emulation you would connect onInput to the backend
  50. // (like a pty or ssh connection) that then handles the changes in the
  51. // terminal.
  52. // As we don't have a connected backend here we simulate the changes by
  53. // directly writing to the terminal.
  54. if (input == '\r') {
  55. _outStream.sink.add('\r\n');
  56. _outStream.sink.add('\$ ');
  57. } else if (input.codeUnitAt(0) == 127) {
  58. // Backspace handling
  59. _outStream.sink.add('\b \b');
  60. } else {
  61. _outStream.sink.add(input);
  62. }
  63. }
  64. @override
  65. void terminate() {
  66. //NOOP
  67. }
  68. @override
  69. void ackProcessed() {
  70. //NOOP
  71. }
  72. }
  73. class _MyHomePageState extends State<MyHomePage> {
  74. Terminal terminal = Terminal(
  75. backend: FakeTerminalBackend(),
  76. maxLines: 10000,
  77. );
  78. @override
  79. void initState() {
  80. super.initState();
  81. }
  82. void onInput(String input) {}
  83. @override
  84. Widget build(BuildContext context) {
  85. return Scaffold(
  86. body: SafeArea(
  87. child: TerminalView(
  88. terminal: terminal,
  89. style: TerminalStyle(fontFamily: ['Cascadia Mono']),
  90. ),
  91. ),
  92. );
  93. }
  94. }