6832ec789e
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
98 lines
3.1 KiB
Dart
98 lines
3.1 KiB
Dart
import 'package:xterm/core.dart';
|
|
|
|
/// The most matches a search reports; more show as "1000+".
|
|
const int chatShellSearchLimit = 1000;
|
|
|
|
/// One match of a scrollback search: from [begin] to [end] (exclusive), in
|
|
/// buffer cells (row 0 is the oldest scrollback line).
|
|
class ChatShellSearchMatch {
|
|
const ChatShellSearchMatch(this.begin, this.end);
|
|
final CellOffset begin;
|
|
final CellOffset end;
|
|
|
|
@override
|
|
bool operator ==(Object other) =>
|
|
other is ChatShellSearchMatch && other.begin == begin && other.end == end;
|
|
|
|
@override
|
|
int get hashCode => Object.hash(begin, end);
|
|
|
|
@override
|
|
String toString() => 'Match($begin, $end)';
|
|
}
|
|
|
|
/// Every place [query] occurs in [buffer], oldest first, at most [limit]. A
|
|
/// line the terminal wrapped across rows is searched as one, so a match may
|
|
/// span rows. Without [caseSensitive] letters match either case.
|
|
List<ChatShellSearchMatch> chatShellTerminalSearch(
|
|
Buffer buffer,
|
|
String query, {
|
|
bool caseSensitive = false,
|
|
int limit = chatShellSearchLimit,
|
|
}) {
|
|
final matches = <ChatShellSearchMatch>[];
|
|
if (query.isEmpty) return matches;
|
|
final needle = caseSensitive ? query : _fold(query);
|
|
final lines = buffer.lines;
|
|
var y = 0;
|
|
while (y < buffer.height && matches.length < limit) {
|
|
final text = StringBuffer();
|
|
final cells = <int>[];
|
|
var last = y;
|
|
while (last + 1 < buffer.height && lines[last + 1].isWrapped) {
|
|
last++;
|
|
}
|
|
for (var row = y; row <= last; row++) {
|
|
_appendRow(lines[row], row, text, cells, caseSensitive);
|
|
}
|
|
final haystack = text.toString();
|
|
var from = 0;
|
|
while (matches.length < limit) {
|
|
final at = haystack.indexOf(needle, from);
|
|
if (at < 0) break;
|
|
final endAt = at + needle.length - 1;
|
|
final first = _cell(cells[at]);
|
|
final lastCell = _cell(cells[endAt]);
|
|
final width = lines[lastCell.y].getWidth(lastCell.x) == 2 ? 2 : 1;
|
|
matches.add(
|
|
ChatShellSearchMatch(first, CellOffset(lastCell.x + width, lastCell.y)),
|
|
);
|
|
from = at + needle.length;
|
|
}
|
|
y = last + 1;
|
|
}
|
|
return matches;
|
|
}
|
|
|
|
/// A cell packed as `y * 0x10000 + x` (rows are at most 500 cells wide;
|
|
/// arithmetic, not shifts, so the web's 32-bit bit operations never wrap).
|
|
CellOffset _cell(int packed) => CellOffset(packed % 0x10000, packed ~/ 0x10000);
|
|
|
|
/// Appends row [y]'s characters to [text], recording each UTF-16 unit's
|
|
/// packed cell in [cells]. A wide character's second cell adds nothing; an
|
|
/// empty cell is a space.
|
|
void _appendRow(
|
|
BufferLine line,
|
|
int y,
|
|
StringBuffer text,
|
|
List<int> cells,
|
|
bool caseSensitive,
|
|
) {
|
|
for (var x = 0; x < line.length; x++) {
|
|
final code = line.getCodePoint(x);
|
|
if (code == 0 && x > 0 && line.getWidth(x - 1) == 2) continue;
|
|
var char = code == 0 ? ' ' : String.fromCharCode(code);
|
|
if (!caseSensitive) {
|
|
final folded = _fold(char);
|
|
// Folding that changes the length would misalign cells.
|
|
if (folded.length == char.length) char = folded;
|
|
}
|
|
text.write(char);
|
|
for (var i = 0; i < char.length; i++) {
|
|
cells.add(y * 0x10000 + x);
|
|
}
|
|
}
|
|
}
|
|
|
|
String _fold(String text) => text.toLowerCase();
|