mock.dart 2.2 KB

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