Files
pipelines/app/lib/features/chat/shell/chat_shell_terminal_find.dart
T

146 lines
4.1 KiB
Dart

import 'package:flutter/material.dart';
import 'package:xterm/xterm.dart';
import 'chat_shell_terminal_search.dart';
/// Find in one terminal's scrollback: the query, whether case matters, the
/// matches (highlighted on the emulator through its [controller]) and the
/// current one. Output arriving meanwhile marks the matches stale; the next
/// step or query searches again.
class ChatShellTerminalFind extends ChangeNotifier {
ChatShellTerminalFind({
required this.terminal,
required this.controller,
required this.hit,
required this.current,
}) {
terminal.addListener(_terminalChanged);
}
final Terminal terminal;
final TerminalController controller;
/// The highlight colours of a match and of the current one.
Color hit;
Color current;
String _query = '';
bool _caseSensitive = false;
List<ChatShellSearchMatch> _matches = const [];
final List<TerminalHighlight> _highlights = [];
int _index = -1;
bool _stale = false;
String get query => _query;
bool get caseSensitive => _caseSensitive;
int get count => _matches.length;
/// Whether there are more matches than [count] reports.
bool get capped => _matches.length >= chatShellSearchLimit;
/// The current match's position among [count] (0-based), -1 with none.
int get index => _index;
/// The current match, as the buffer is now.
ChatShellSearchMatch? get selected {
if (_index < 0 || _index >= _highlights.length) return null;
final range = _highlights[_index].range;
return range == null ? null : ChatShellSearchMatch(range.begin, range.end);
}
set query(String value) {
if (value == _query) return;
_query = value;
_search(keep: false);
}
void toggleCase() {
_caseSensitive = !_caseSensitive;
_search(keep: false);
}
/// Moves to the next match ([step] 1) or the previous one (-1), wrapping;
/// a stale search runs again first.
void step(int step) {
if (_stale) _search(keep: true);
if (_matches.isEmpty) return;
_setIndex((_index + step) % _matches.length);
}
/// Searches the buffer as it is now. With [keep] the current match stays
/// the one at (or just after) where it was; otherwise it is the last match
/// at or above the bottom of the scrollback, as terminals search upwards.
void _search({required bool keep}) {
final before = keep ? selected?.begin : null;
_stale = false;
_matches = chatShellTerminalSearch(
terminal.buffer,
_query,
caseSensitive: _caseSensitive,
);
_clearHighlights();
final buffer = terminal.buffer;
for (final match in _matches) {
_highlights.add(
controller.highlight(
p1: buffer.createAnchorFromOffset(match.begin),
p2: buffer.createAnchorFromOffset(match.end),
color: hit,
),
);
}
var index = _matches.isEmpty ? -1 : _matches.length - 1;
if (before != null) {
final after = _matches.indexWhere((m) => m.begin.isAfterOrSame(before));
if (after >= 0) index = after;
}
_index = -1;
_setIndex(index);
}
void _setIndex(int index) {
final buffer = terminal.buffer;
void paint(int at, Color color) {
if (at < 0 || at >= _highlights.length) return;
final old = _highlights[at];
final range = old.range;
_dispose(old);
if (range == null) return;
_highlights[at] = controller.highlight(
p1: buffer.createAnchorFromOffset(range.begin),
p2: buffer.createAnchorFromOffset(range.end),
color: color,
);
}
if (_index != index) paint(_index, hit);
_index = index;
paint(_index, current);
notifyListeners();
}
void _terminalChanged() {
if (_query.isEmpty || _stale) return;
_stale = true;
}
void _clearHighlights() {
_highlights.forEach(_dispose);
_highlights.clear();
}
static void _dispose(TerminalHighlight highlight) {
if (highlight.disposed) return;
highlight.p1.dispose();
highlight.p2.dispose();
highlight.dispose();
}
@override
void dispose() {
terminal.removeListener(_terminalChanged);
_clearHighlights();
super.dispose();
}
}