From 1e03b7bfdba9fbd2dce9d9479a5de7e18c8e5629 Mon Sep 17 00:00:00 2001 From: Georg Wechslberger Date: Tue, 13 Sep 2022 13:33:06 +0200 Subject: [PATCH 1/2] feat: expose readOnly flag of CustomTextEdit in TerminalView --- lib/src/terminal_view.dart | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/lib/src/terminal_view.dart b/lib/src/terminal_view.dart index 14558ce6..2211e5ea 100644 --- a/lib/src/terminal_view.dart +++ b/lib/src/terminal_view.dart @@ -42,6 +42,7 @@ class TerminalView extends StatefulWidget { this.alwaysShowCursor = false, this.deleteDetection = false, this.shortcuts, + this.readOnly = false, }) : super(key: key); /// The underlying terminal that this widget renders. @@ -115,6 +116,9 @@ class TerminalView extends StatefulWidget { /// of the terminal If not provided, [defaultTerminalShortcuts] will be used. final Map? shortcuts; + /// True if no input should send to the terminal. + final bool readOnly; + @override State createState() => TerminalViewState(); } @@ -219,6 +223,7 @@ class TerminalViewState extends State { } }, onKey: _onKeyEvent, + readOnly: widget.readOnly, child: child, ); From b89b7f53a052d5c73c1b46f4adaecec3164c1b93 Mon Sep 17 00:00:00 2001 From: xuty Date: Wed, 14 Sep 2022 07:26:34 +0800 Subject: [PATCH 2/2] Add test --- test/src/terminal_view_test.dart | 44 ++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/test/src/terminal_view_test.dart b/test/src/terminal_view_test.dart index c2486e85..a59b5927 100644 --- a/test/src/terminal_view_test.dart +++ b/test/src/terminal_view_test.dart @@ -7,6 +7,8 @@ import 'package:xterm/xterm.dart'; import '../_fixture/_fixture.dart'; void main() { + final binding = TestWidgetsFlutterBinding.ensureInitialized(); + testWidgets( 'Golden test', (WidgetTester tester) async { @@ -27,4 +29,46 @@ void main() { }, skip: !Platform.isMacOS, ); + + group('TerminalView.readOnly', () { + testWidgets('works', (WidgetTester tester) async { + final terminalOutput = []; + final terminal = Terminal(onOutput: terminalOutput.add); + + await tester.pumpWidget(MaterialApp( + home: Scaffold( + body: TerminalView(terminal, readOnly: true, autofocus: true), + ), + )); + + // https://github.com/flutter/flutter/issues/11181#issuecomment-314936646 + await tester.tap(find.byType(TerminalView)); + await tester.pump(Duration(seconds: 1)); + + binding.testTextInput.enterText('ls -al'); + await binding.idle(); + + expect(terminalOutput.join(), isEmpty); + }); + + testWidgets('does not block input when false', (WidgetTester tester) async { + final terminalOutput = []; + final terminal = Terminal(onOutput: terminalOutput.add); + + await tester.pumpWidget(MaterialApp( + home: Scaffold( + body: TerminalView(terminal, readOnly: false, autofocus: true), + ), + )); + + // https://github.com/flutter/flutter/issues/11181#issuecomment-314936646 + await tester.tap(find.byType(TerminalView)); + await tester.pump(Duration(seconds: 1)); + + binding.testTextInput.enterText('ls -al'); + await binding.idle(); + + expect(terminalOutput.join(), 'ls -al'); + }); + }); }