mock.dart 2.2 KB

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