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.
This commit is contained in:
2026-11-11 00:05:16 +00:00
parent e9fa1b6ec9
commit 2628f6c425
3 changed files with 170 additions and 2 deletions
@@ -75,6 +75,97 @@ void main() {
variant: TargetPlatformVariant.only(TargetPlatform.macOS),
);
/// What the app wrote to the clipboard.
List<String> copied(WidgetTester tester) {
final texts = <String>[];
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<TerminalView>(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<void> 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<TerminalView>(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 {