From 2628f6c4253fd8d4ca7bebfc9b6f7be4e5c93844 Mon Sep 17 00:00:00 2001 From: Alan Silva Date: Wed, 11 Nov 2026 00:05:16 +0000 Subject: [PATCH] Copy a terminal selection with Ctrl+C outside Apple platforms A browser keeps Ctrl+Shift+C for its developer tools, so on Linux and Windows web nothing copied. With text selected Ctrl+C now copies it (and clears the selection) instead of interrupting the process; Ctrl+Insert copies too, and a finished run's read-only output answers the copy keys. The terminal's options menu names the key. --- .../chat/shell/chat_shell_terminal_view.dart | 72 ++++++++++++++- .../features/terminal/terminal_shortcuts.dart | 9 ++ .../chat/shell/chat_shell_terminal_test.dart | 91 +++++++++++++++++++ 3 files changed, 170 insertions(+), 2 deletions(-) diff --git a/app/lib/features/chat/shell/chat_shell_terminal_view.dart b/app/lib/features/chat/shell/chat_shell_terminal_view.dart index d1dde00e..3dc7c30b 100644 --- a/app/lib/features/chat/shell/chat_shell_terminal_view.dart +++ b/app/lib/features/chat/shell/chat_shell_terminal_view.dart @@ -19,8 +19,10 @@ bool chatShellTouchFirst() => /// The terminal's own key shortcuts. Cmd-C, Cmd-V and Cmd-A on Apple /// platforms; elsewhere Ctrl-V (and the terminal convention Ctrl-Shift-V) -/// paste and Ctrl-Shift-C copies, so every other Ctrl key, Ctrl-A and Ctrl-C -/// included, reaches the process. +/// paste and Ctrl-Shift-C or Ctrl-Insert copies, so every other Ctrl key, +/// Ctrl-A included, reaches the process. Ctrl-C copies too while text is +/// selected (see [chatShellCopyKey]); a browser keeps Ctrl-Shift-C for its +/// developer tools, so there it is the key that works. Map chatShellTerminalShortcuts() { const paste = PasteTextIntent(SelectionChangedCause.keyboard); final apple = @@ -39,11 +41,30 @@ Map chatShellTerminalShortcuts() { return const { SingleActivator(LogicalKeyboardKey.keyC, control: true, shift: true): CopySelectionTextIntent.copy, + SingleActivator(LogicalKeyboardKey.insert, control: true): + CopySelectionTextIntent.copy, SingleActivator(LogicalKeyboardKey.keyV, control: true): paste, SingleActivator(LogicalKeyboardKey.keyV, control: true, shift: true): paste, }; } +/// Whether [event] is Ctrl-C outside Apple platforms: with text selected in +/// the terminal it copies the selection instead of interrupting the process, +/// as terminals in browsers and on Windows do. Without a selection Ctrl-C +/// stays the process's. +bool chatShellCopyKey(KeyEvent event, HardwareKeyboard keyboard) { + final apple = + defaultTargetPlatform == TargetPlatform.macOS || + defaultTargetPlatform == TargetPlatform.iOS; + return !apple && + event is KeyDownEvent && + event.logicalKey == LogicalKeyboardKey.keyC && + keyboard.isControlPressed && + !keyboard.isShiftPressed && + !keyboard.isAltPressed && + !keyboard.isMetaPressed; +} + /// One run's terminal emulator view. In a card it is bounded to /// [kChatShellOutputCollapsedLines] rows (a finished run shrinks to its /// output); with [fill] it takes the space its parent gives (full screen). @@ -74,6 +95,7 @@ class ChatShellTerminalView extends StatefulWidget { class _ChatShellTerminalViewState extends State { final TerminalController _controller = TerminalController(); final FocusNode _focus = FocusNode(debugLabel: 'chat-shell-terminal'); + final FocusNode _outputFocus = FocusNode(debugLabel: 'chat-shell-output'); @override void initState() { @@ -88,9 +110,42 @@ class _ChatShellTerminalViewState extends State { ChatComposerPasteFocus.unregisterInput(_focus); _controller.dispose(); _focus.dispose(); + _outputFocus.dispose(); super.dispose(); } + /// Copies the selected text and clears the selection; false with none. + bool _copySelection() { + final selection = _controller.selection; + if (selection == null) return false; + final text = widget.session.terminal.buffer.getText(selection); + if (text.isEmpty) return false; + Clipboard.setData(ClipboardData(text: text)); + _controller.clearSelection(); + return true; + } + + /// Ctrl-C with a selection copies; anything else is the terminal's. + KeyEventResult _key(FocusNode node, KeyEvent event) => + chatShellCopyKey(event, HardwareKeyboard.instance) && _copySelection() + ? KeyEventResult.handled + : KeyEventResult.ignored; + + /// A finished run's output takes no keys of its own, so the copy keys are + /// answered here while it has the keyboard. + KeyEventResult _readOnlyKey(FocusNode node, KeyEvent event) { + if (event is! KeyDownEvent || + event.logicalKey != LogicalKeyboardKey.keyC && + event.logicalKey != LogicalKeyboardKey.insert) { + return KeyEventResult.ignored; + } + final keyboard = HardwareKeyboard.instance; + if (!keyboard.isControlPressed && !keyboard.isMetaPressed) { + return KeyEventResult.ignored; + } + return _copySelection() ? KeyEventResult.handled : KeyEventResult.ignored; + } + void _tapped(TapUpDetails details, CellOffset offset) { final uri = chatShellLinkAt(widget.session.terminal.buffer, offset); if (uri != null) openChatLink(context, uri); @@ -127,9 +182,22 @@ class _ChatShellTerminalViewState extends State { shortcuts: chatShellTerminalShortcuts(), cursorType: TerminalCursorType.block, onTapUp: _tapped, + onKeyEvent: _key, ), ), ); + if (!live) { + // The read-only emulator attaches no keyboard: a press selects text + // and gives this node the keyboard, so the copy keys work. + view = Listener( + onPointerDown: (_) => _outputFocus.requestFocus(), + child: Focus( + focusNode: _outputFocus, + onKeyEvent: _readOnlyKey, + child: view, + ), + ); + } // The emulator would otherwise inset itself by the window's safe area. view = MediaQuery.removePadding( context: context, diff --git a/app/lib/features/terminal/terminal_shortcuts.dart b/app/lib/features/terminal/terminal_shortcuts.dart index abaae335..fc72200c 100644 --- a/app/lib/features/terminal/terminal_shortcuts.dart +++ b/app/lib/features/terminal/terminal_shortcuts.dart @@ -77,5 +77,14 @@ class TerminalShortcutLabels { String get summary => [ 'Show or hide: ${toggle.join(', ')}', if (tabs.isNotEmpty) 'Next or previous tab: ${tabs.join(' / ')}', + 'Copy selected text: $copy', ].join('\n'); + + /// The keys that copy the terminal's selected text. A browser keeps + /// Ctrl+Shift+C for its developer tools. + String get copy => _apple + ? 'Cmd+C' + : web + ? 'Ctrl+C' + : 'Ctrl+C, Ctrl+Shift+C'; } diff --git a/app/test/features/chat/shell/chat_shell_terminal_test.dart b/app/test/features/chat/shell/chat_shell_terminal_test.dart index 684cd22b..629e65b4 100644 --- a/app/test/features/chat/shell/chat_shell_terminal_test.dart +++ b/app/test/features/chat/shell/chat_shell_terminal_test.dart @@ -75,6 +75,97 @@ void main() { variant: TargetPlatformVariant.only(TargetPlatform.macOS), ); + /// What the app wrote to the clipboard. + List copied(WidgetTester tester) { + final texts = []; + tester.binding.defaultBinaryMessenger.setMockMethodCallHandler( + SystemChannels.platform, + (call) async { + if (call.method == 'Clipboard.setData') { + texts.add('${(call.arguments as Map)['text']}'); + } + return null; + }, + ); + addTearDown( + () => tester.binding.defaultBinaryMessenger.setMockMethodCallHandler( + SystemChannels.platform, + null, + ), + ); + return texts; + } + + /// Selects the first [length] cells of the terminal's first line. + void select(WidgetTester tester, int length) { + final view = tester.widget(find.byType(TerminalView)); + final buffer = view.terminal.buffer; + view.controller!.setSelection( + buffer.createAnchor(0, 0), + buffer.createAnchor(length, 0), + ); + } + + testWidgets('on Linux, Ctrl-C copies a selection and interrupts without one; ' + 'Ctrl-Insert copies too', (tester) async { + await openRunning(tester, output: 'Paste your token: '); + final texts = copied(tester); + await tester.tap(find.byType(TerminalView)); + await tester.pump(); + Future ctrl(LogicalKeyboardKey key) async { + await tester.sendKeyDownEvent(LogicalKeyboardKey.controlLeft); + await tester.sendKeyEvent(key); + await tester.sendKeyUpEvent(LogicalKeyboardKey.controlLeft); + await tester.pump(); + } + + select(tester, 5); + await ctrl(LogicalKeyboardKey.keyC); + expect(texts, ['Paste']); + // The process was not interrupted, and the selection is gone. + expect(typed(), isEmpty); + final view = tester.widget(find.byType(TerminalView)); + expect(view.controller!.selection, isNull); + // With nothing selected Ctrl-C is the process's again. + await ctrl(LogicalKeyboardKey.keyC); + expect(typed(), '\x03'); + expect(texts, ['Paste']); + + select(tester, 10); + await ctrl(LogicalKeyboardKey.insert); + expect(texts, ['Paste', 'Paste your']); + await tester.pump(const Duration(milliseconds: 400)); + }, variant: TargetPlatformVariant.only(TargetPlatform.linux)); + + testWidgets( + "a finished run's output copies its selection by key", + (tester) async { + gateway.runs = [ + shellRecord( + 'run_done', + command: 'echo done', + terminal: true, + output: 'all done here\r\n', + status: 'exited', + exitCode: 0, + ), + ]; + await tester.pumpWidget(shellThread(gateway)); + await tester.pump(); + final texts = copied(tester); + await tester.tap(find.byType(TerminalView)); + await tester.pump(); + select(tester, 8); + await tester.sendKeyDownEvent(LogicalKeyboardKey.controlLeft); + await tester.sendKeyEvent(LogicalKeyboardKey.keyC); + await tester.sendKeyUpEvent(LogicalKeyboardKey.controlLeft); + await tester.pump(); + expect(texts, ['all done']); + await tester.pump(const Duration(milliseconds: 400)); + }, + variant: TargetPlatformVariant.only(TargetPlatform.linux), + ); + testWidgets('Paste types the clipboard text into the process', ( tester, ) async {