mock.dart 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. import 'package:flutter/foundation.dart';
  2. import 'package:flutter/material.dart';
  3. import 'package:xterm/xterm.dart';
  4. void main() {
  5. runApp(MyApp());
  6. }
  7. bool get isDesktop {
  8. if (kIsWeb) return false;
  9. return [
  10. TargetPlatform.windows,
  11. TargetPlatform.linux,
  12. TargetPlatform.macOS,
  13. ].contains(defaultTargetPlatform);
  14. }
  15. class MyApp extends StatelessWidget {
  16. @override
  17. Widget build(BuildContext context) {
  18. return MaterialApp(
  19. title: 'xterm.dart demo',
  20. debugShowCheckedModeBanner: false,
  21. home: MyHomePage(),
  22. );
  23. }
  24. }
  25. class MyHomePage extends StatefulWidget {
  26. MyHomePage({super.key});
  27. @override
  28. // ignore: library_private_types_in_public_api
  29. _MyHomePageState createState() => _MyHomePageState();
  30. }
  31. class _MyHomePageState extends State<MyHomePage> {
  32. final terminal = Terminal(
  33. maxLines: 1000,
  34. );
  35. late final MockRepl pty;
  36. @override
  37. void initState() {
  38. super.initState();
  39. pty = MockRepl(terminal.write);
  40. terminal.onOutput = pty.write;
  41. }
  42. @override
  43. Widget build(BuildContext context) {
  44. return Scaffold(
  45. backgroundColor: Colors.transparent,
  46. body: SafeArea(
  47. child: TerminalView(
  48. terminal,
  49. backgroundOpacity: 0.7,
  50. ),
  51. ),
  52. );
  53. }
  54. }
  55. class MockRepl {
  56. MockRepl(this.onOutput) {
  57. onOutput('Welcome to xterm.dart!\r\n');
  58. onOutput('Type "help" for more information.\r\n');
  59. onOutput('\n');
  60. onOutput('\$ ');
  61. }
  62. final void Function(String data) onOutput;
  63. void write(String input) {
  64. for (var char in input.codeUnits) {
  65. switch (char) {
  66. case 13: // carriage return
  67. onOutput.call('\r\n');
  68. onOutput.call('\$ ');
  69. break;
  70. case 127: // backspace
  71. onOutput.call('\b \b');
  72. break;
  73. default:
  74. onOutput.call(String.fromCharCode(char));
  75. }
  76. }
  77. }
  78. }