Terminal: find, zoom, scrollback, scroll keys, context menu, bell, link hover, OSC 52/7, cursor blink, stale selection
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,138 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:pipelines_app/features/chat/shell/chat_shell_terminal_search.dart';
|
||||
import 'package:xterm/xterm.dart';
|
||||
|
||||
import 'terminal_view_harness.dart';
|
||||
|
||||
const _field = ValueKey('terminal-find-field');
|
||||
|
||||
String _count(WidgetTester tester) =>
|
||||
tester.widget<Text>(find.byKey(const ValueKey('terminal-find-count'))).data!;
|
||||
|
||||
/// Opens the find bar with the platform's key.
|
||||
Future<void> _openFind(WidgetTester tester) => isApple()
|
||||
? chordKeys(tester, LogicalKeyboardKey.keyF, [LogicalKeyboardKey.metaLeft])
|
||||
: chordKeys(tester, LogicalKeyboardKey.keyF, [
|
||||
LogicalKeyboardKey.controlLeft,
|
||||
LogicalKeyboardKey.shiftLeft,
|
||||
]);
|
||||
|
||||
void main() {
|
||||
test('search joins wrapped rows, folds case unless asked and maps cells', () {
|
||||
final terminal = Terminal(maxLines: 100)..resize(10, 4);
|
||||
terminal.write('abc Needle\r\nxxxxxxxneedle NEEDLE\r\n中文needle');
|
||||
final all = chatShellTerminalSearch(terminal.buffer, 'needle');
|
||||
expect(all, hasLength(4));
|
||||
// "Needle" fills the first row's last six cells.
|
||||
expect(
|
||||
all[0],
|
||||
const ChatShellSearchMatch(CellOffset(4, 0), CellOffset(10, 0)),
|
||||
);
|
||||
// The second line wraps: its "needle" spans rows 1 and 2.
|
||||
expect(
|
||||
all[1],
|
||||
const ChatShellSearchMatch(CellOffset(7, 1), CellOffset(3, 2)),
|
||||
);
|
||||
expect(all[2].begin, const CellOffset(4, 2));
|
||||
// Wide characters take two cells each; the match starts after both.
|
||||
expect(all[3].begin, const CellOffset(4, 3));
|
||||
final exact = chatShellTerminalSearch(
|
||||
terminal.buffer,
|
||||
'needle',
|
||||
caseSensitive: true,
|
||||
);
|
||||
expect(exact, hasLength(2));
|
||||
expect(chatShellTerminalSearch(terminal.buffer, ''), isEmpty);
|
||||
expect(
|
||||
chatShellTerminalSearch(terminal.buffer, 'needle', limit: 2),
|
||||
hasLength(2),
|
||||
);
|
||||
});
|
||||
|
||||
testWidgets('the find key opens the bar; matches are counted, highlighted '
|
||||
'and scrolled to; Esc closes it and the terminal has the keys again', (
|
||||
tester,
|
||||
) async {
|
||||
final harness = TerminalViewHarness();
|
||||
await harness.pump(tester);
|
||||
await harness.write(
|
||||
tester,
|
||||
'first needle\r\n'
|
||||
'${List.filled(60, 'filler').join('\r\n')}\r\n'
|
||||
'Needle two\r\n'
|
||||
'last needle',
|
||||
);
|
||||
await harness.focus(tester);
|
||||
await _openFind(tester);
|
||||
expect(find.byKey(_field), findsOneWidget);
|
||||
// The keys went to the find bar, not the shell.
|
||||
expect(harness.typed, isEmpty);
|
||||
await tester.enterText(find.byKey(_field), 'needle');
|
||||
await tester.pump();
|
||||
// Searching upwards: the current match is the last one.
|
||||
expect(_count(tester), '3 of 3');
|
||||
expect(harness.controller(tester).highlights, hasLength(3));
|
||||
final bottom = harness.scroll(tester).maxScrollExtent;
|
||||
expect(harness.scroll(tester).pixels, bottom);
|
||||
// Next wraps to the first match, at the top of the scrollback.
|
||||
await tester.tap(find.byKey(const ValueKey('terminal-find-next')));
|
||||
await tester.pump();
|
||||
expect(_count(tester), '1 of 3');
|
||||
expect(harness.scroll(tester).pixels, lessThan(bottom));
|
||||
// Shift+Enter goes back.
|
||||
await chordKeys(tester, LogicalKeyboardKey.enter, [
|
||||
LogicalKeyboardKey.shiftLeft,
|
||||
]);
|
||||
expect(_count(tester), '3 of 3');
|
||||
await tester.sendKeyEvent(LogicalKeyboardKey.enter);
|
||||
await tester.pump();
|
||||
expect(_count(tester), '1 of 3');
|
||||
// Match case leaves out "Needle".
|
||||
await tester.tap(find.byKey(const ValueKey('terminal-find-case')));
|
||||
await tester.pump();
|
||||
expect(_count(tester), '2 of 2');
|
||||
await tester.enterText(find.byKey(_field), 'absent');
|
||||
await tester.pump();
|
||||
expect(_count(tester), 'No results');
|
||||
await tester.sendKeyEvent(LogicalKeyboardKey.escape);
|
||||
await tester.pump();
|
||||
expect(find.byKey(_field), findsNothing);
|
||||
expect(harness.controller(tester).highlights, isEmpty);
|
||||
expect(harness.view(tester).focusNode!.hasFocus, isTrue);
|
||||
await tester.sendKeyEvent(LogicalKeyboardKey.keyX);
|
||||
await tester.pump();
|
||||
expect(harness.typed, 'x');
|
||||
}, variant: desktops);
|
||||
|
||||
testWidgets('outside Apple platforms Ctrl+F stays the shell\'s', (
|
||||
tester,
|
||||
) async {
|
||||
final harness = TerminalViewHarness();
|
||||
await harness.pump(tester);
|
||||
await harness.focus(tester);
|
||||
await chordKeys(tester, LogicalKeyboardKey.keyF, [
|
||||
LogicalKeyboardKey.controlLeft,
|
||||
]);
|
||||
expect(find.byKey(_field), findsNothing);
|
||||
expect(harness.typed, '\x06');
|
||||
}, variant: TargetPlatformVariant.only(TargetPlatform.linux));
|
||||
|
||||
testWidgets('output after a search is found by the next step', (
|
||||
tester,
|
||||
) async {
|
||||
final harness = TerminalViewHarness();
|
||||
await harness.pump(tester);
|
||||
await harness.write(tester, 'one match\r\n');
|
||||
await harness.focus(tester);
|
||||
await _openFind(tester);
|
||||
await tester.enterText(find.byKey(_field), 'match');
|
||||
await tester.pump();
|
||||
expect(_count(tester), '1 of 1');
|
||||
await harness.write(tester, 'two match\r\n');
|
||||
await tester.tap(find.byKey(const ValueKey('terminal-find-next')));
|
||||
await tester.pump();
|
||||
expect(_count(tester), '2 of 2');
|
||||
}, variant: TargetPlatformVariant.only(TargetPlatform.linux));
|
||||
}
|
||||
@@ -0,0 +1,191 @@
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/gestures.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:pipelines_app/features/chat/shell/chat_shell_run.dart';
|
||||
import 'package:pipelines_app/features/chat/shell/chat_shell_terminal_session.dart';
|
||||
import 'package:pipelines_app/features/chat/shell/chat_shell_terminal_view.dart';
|
||||
import 'package:pipelines_app/features/terminal/terminal_layout.dart';
|
||||
import 'package:xterm/xterm.dart';
|
||||
|
||||
/// A terminal layout saved in memory.
|
||||
class MemoryStore implements TerminalLayoutStore {
|
||||
String? saved;
|
||||
@override
|
||||
Future<String?> load() async => saved;
|
||||
@override
|
||||
Future<bool> save(String value) async {
|
||||
saved = value;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
/// One live terminal view filling an 800x500 app, with what it sends to the
|
||||
/// process collected in [sent].
|
||||
class TerminalViewHarness {
|
||||
TerminalViewHarness({TerminalLayout? layout, bool live = true})
|
||||
: layout = layout ?? TerminalLayout(store: MemoryStore()) {
|
||||
session = ChatShellTerminalSession(
|
||||
ChatShellRun.fromJson({
|
||||
'id': 'run_1',
|
||||
'command': 'sh',
|
||||
'directory': '/w',
|
||||
'status': live ? 'running' : 'exited',
|
||||
'started_at': 1,
|
||||
'output': '',
|
||||
'output_bytes': 0,
|
||||
'terminal': true,
|
||||
'cols': 80,
|
||||
'rows': 24,
|
||||
})!,
|
||||
onInput: sent.add,
|
||||
onResize: (cols, rows) => sizes.add((cols, rows)),
|
||||
layout: this.layout,
|
||||
);
|
||||
}
|
||||
|
||||
final TerminalLayout layout;
|
||||
late final ChatShellTerminalSession session;
|
||||
final List<String> sent = [];
|
||||
final List<(int, int)> sizes = [];
|
||||
|
||||
Terminal get terminal => session.terminal;
|
||||
String get typed => sent.join();
|
||||
|
||||
Future<void> pump(WidgetTester tester, {bool reduceMotion = false}) async {
|
||||
tester.view.physicalSize = const Size(800, 500);
|
||||
tester.view.devicePixelRatio = 1;
|
||||
addTearDown(tester.view.reset);
|
||||
addTearDown(session.dispose);
|
||||
await tester.pumpWidget(
|
||||
MaterialApp(
|
||||
home: Builder(
|
||||
builder: (context) => MediaQuery(
|
||||
data: MediaQuery.of(
|
||||
context,
|
||||
).copyWith(disableAnimations: reduceMotion),
|
||||
child: Scaffold(
|
||||
body: ChatShellTerminalView(
|
||||
session: session,
|
||||
fill: true,
|
||||
autofocus: true,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
await tester.pump();
|
||||
}
|
||||
|
||||
TerminalView view(WidgetTester tester) =>
|
||||
tester.widget<TerminalView>(find.byType(TerminalView));
|
||||
|
||||
TerminalViewState viewState(WidgetTester tester) =>
|
||||
tester.state<TerminalViewState>(find.byType(TerminalView));
|
||||
|
||||
TerminalController controller(WidgetTester tester) =>
|
||||
view(tester).controller!;
|
||||
|
||||
ScrollPosition scroll(WidgetTester tester) =>
|
||||
view(tester).scrollController!.position;
|
||||
|
||||
/// The global centre of buffer cell ([x], [y]).
|
||||
Offset cellCentre(WidgetTester tester, int x, int y) {
|
||||
final render = viewState(tester).renderTerminal;
|
||||
final cell = render.cellSize;
|
||||
return render.localToGlobal(
|
||||
render.getOffset(CellOffset(x, y)) +
|
||||
Offset(cell.width / 2, cell.height / 2),
|
||||
);
|
||||
}
|
||||
|
||||
/// Writes [text] as live output and lets the view redraw.
|
||||
Future<void> write(WidgetTester tester, String text) async {
|
||||
terminal.write(text);
|
||||
await tester.pump();
|
||||
}
|
||||
|
||||
/// Focuses the emulator.
|
||||
Future<void> focus(WidgetTester tester) async {
|
||||
final node = view(tester).focusNode!;
|
||||
node.requestFocus();
|
||||
await tester.pump();
|
||||
}
|
||||
|
||||
/// Right-clicks buffer cell ([x], [y]) with a mouse, holding Shift when
|
||||
/// [shift].
|
||||
Future<void> rightClick(
|
||||
WidgetTester tester,
|
||||
int x,
|
||||
int y, {
|
||||
bool shift = false,
|
||||
}) async {
|
||||
if (shift) await tester.sendKeyDownEvent(LogicalKeyboardKey.shiftLeft);
|
||||
final gesture = await tester.startGesture(
|
||||
cellCentre(tester, x, y),
|
||||
kind: PointerDeviceKind.mouse,
|
||||
buttons: kSecondaryMouseButton,
|
||||
);
|
||||
await gesture.up();
|
||||
if (shift) await tester.sendKeyUpEvent(LogicalKeyboardKey.shiftLeft);
|
||||
await tester.pumpAndSettle();
|
||||
}
|
||||
}
|
||||
|
||||
/// Presses [key] with [modifiers] held.
|
||||
Future<void> chordKeys(
|
||||
WidgetTester tester,
|
||||
LogicalKeyboardKey key, [
|
||||
List<LogicalKeyboardKey> modifiers = const [],
|
||||
]) async {
|
||||
for (final modifier in modifiers) {
|
||||
await tester.sendKeyDownEvent(modifier);
|
||||
}
|
||||
await tester.sendKeyEvent(key);
|
||||
for (final modifier in modifiers.reversed) {
|
||||
await tester.sendKeyUpEvent(modifier);
|
||||
}
|
||||
await tester.pump();
|
||||
}
|
||||
|
||||
/// The platform's command key: Cmd on Apple platforms, else Ctrl.
|
||||
LogicalKeyboardKey commandKey() =>
|
||||
defaultTargetPlatform == TargetPlatform.macOS ||
|
||||
defaultTargetPlatform == TargetPlatform.iOS
|
||||
? LogicalKeyboardKey.metaLeft
|
||||
: LogicalKeyboardKey.controlLeft;
|
||||
|
||||
bool isApple() =>
|
||||
defaultTargetPlatform == TargetPlatform.macOS ||
|
||||
defaultTargetPlatform == TargetPlatform.iOS;
|
||||
|
||||
/// Collects what the app writes to the clipboard, answering reads with
|
||||
/// [paste].
|
||||
List<String> mockClipboard(WidgetTester tester, {String paste = ''}) {
|
||||
final texts = <String>[];
|
||||
tester.binding.defaultBinaryMessenger.setMockMethodCallHandler(
|
||||
SystemChannels.platform,
|
||||
(call) async {
|
||||
if (call.method == 'Clipboard.setData') {
|
||||
texts.add('${(call.arguments as Map)['text']}');
|
||||
}
|
||||
if (call.method == 'Clipboard.getData') return {'text': paste};
|
||||
return null;
|
||||
},
|
||||
);
|
||||
addTearDown(
|
||||
() => tester.binding.defaultBinaryMessenger.setMockMethodCallHandler(
|
||||
SystemChannels.platform,
|
||||
null,
|
||||
),
|
||||
);
|
||||
return texts;
|
||||
}
|
||||
|
||||
/// Desktop platforms: a Mac and one that is not.
|
||||
final desktops = TargetPlatformVariant({
|
||||
TargetPlatform.macOS,
|
||||
TargetPlatform.linux,
|
||||
});
|
||||
Reference in New Issue
Block a user