6832ec789e
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
131 lines
4.2 KiB
Dart
131 lines
4.2 KiB
Dart
import 'package:xterm/core.dart';
|
|
|
|
final RegExp _link = RegExp(r'''https?://[^\s<>"'`]+''');
|
|
|
|
/// A web link drawn in a terminal: where it goes and the cells it covers,
|
|
/// from [begin] to [end] (exclusive), which may span wrapped rows.
|
|
class ChatShellTerminalLink {
|
|
const ChatShellTerminalLink(this.uri, this.begin, this.end);
|
|
final Uri uri;
|
|
final CellOffset begin;
|
|
final CellOffset end;
|
|
|
|
@override
|
|
bool operator ==(Object other) =>
|
|
other is ChatShellTerminalLink &&
|
|
other.uri == uri &&
|
|
other.begin == begin &&
|
|
other.end == end;
|
|
|
|
@override
|
|
int get hashCode => Object.hash(uri, begin, end);
|
|
}
|
|
|
|
/// The web link drawn at [offset] in [buffer], if any: the operator taps the
|
|
/// login URL an auth command printed. A link the terminal wrapped across
|
|
/// rows is joined first. Only http and https links are offered.
|
|
Uri? chatShellLinkAt(Buffer buffer, CellOffset offset) =>
|
|
chatShellTextLinkAt(buffer, offset)?.uri;
|
|
|
|
/// The link at [offset] in [terminal]'s buffer: an OSC 8 hyperlink on the
|
|
/// cell first (the program's own link, whatever its text), else a web
|
|
/// address printed there. Only http and https links are offered.
|
|
ChatShellTerminalLink? chatShellTerminalLinkAt(
|
|
Terminal terminal,
|
|
CellOffset offset,
|
|
) {
|
|
final buffer = terminal.buffer;
|
|
if (offset.y < 0 || offset.y >= buffer.height) return null;
|
|
final target = terminal.hyperlinkAt(buffer.lines[offset.y], offset.x);
|
|
if (target != null) {
|
|
final uri = _web(target);
|
|
if (uri == null) return null;
|
|
return _hyperlinkExtent(terminal, offset, target, uri);
|
|
}
|
|
return chatShellTextLinkAt(buffer, offset);
|
|
}
|
|
|
|
Uri? _web(String text) {
|
|
final uri = Uri.tryParse(text);
|
|
if (uri == null || uri.host.isEmpty) return null;
|
|
return uri.scheme == 'http' || uri.scheme == 'https' ? uri : null;
|
|
}
|
|
|
|
/// The run of cells around [offset] carrying the same OSC 8 [target],
|
|
/// following wrapped rows.
|
|
ChatShellTerminalLink _hyperlinkExtent(
|
|
Terminal terminal,
|
|
CellOffset offset,
|
|
String target,
|
|
Uri uri,
|
|
) {
|
|
final lines = terminal.buffer.lines;
|
|
bool same(int x, int y) => terminal.hyperlinkAt(lines[y], x) == target;
|
|
var (bx, by) = (offset.x, offset.y);
|
|
while (true) {
|
|
if (bx > 0 && same(bx - 1, by)) {
|
|
bx--;
|
|
} else if (bx == 0 && by > 0 && lines[by].isWrapped) {
|
|
final width = lines[by - 1].length;
|
|
if (width == 0 || !same(width - 1, by - 1)) break;
|
|
(bx, by) = (width - 1, by - 1);
|
|
} else {
|
|
break;
|
|
}
|
|
}
|
|
var (ex, ey) = (offset.x, offset.y);
|
|
while (true) {
|
|
if (ex + 1 < lines[ey].length && same(ex + 1, ey)) {
|
|
ex++;
|
|
} else if (ex + 1 >= lines[ey].length &&
|
|
ey + 1 < lines.length &&
|
|
lines[ey + 1].isWrapped &&
|
|
same(0, ey + 1)) {
|
|
(ex, ey) = (0, ey + 1);
|
|
} else {
|
|
break;
|
|
}
|
|
}
|
|
return ChatShellTerminalLink(uri, CellOffset(bx, by), CellOffset(ex + 1, ey));
|
|
}
|
|
|
|
/// The web address printed at [offset] in [buffer], with its cells.
|
|
ChatShellTerminalLink? chatShellTextLinkAt(Buffer buffer, CellOffset offset) {
|
|
if (offset.y < 0 || offset.y >= buffer.height) return null;
|
|
var first = offset.y;
|
|
while (first > 0 && buffer.lines[first].isWrapped) {
|
|
first--;
|
|
}
|
|
var last = offset.y;
|
|
while (last + 1 < buffer.height && buffer.lines[last + 1].isWrapped) {
|
|
last++;
|
|
}
|
|
final text = StringBuffer();
|
|
final cells = <CellOffset>[];
|
|
var tapped = 0;
|
|
for (var y = first; y <= last; y++) {
|
|
final line = buffer.lines[y];
|
|
if (y == offset.y) tapped = text.length + offset.x;
|
|
for (var x = 0; x < line.length; x++) {
|
|
final code = line.getCodePoint(x);
|
|
// One character per cell keeps cell and string positions aligned.
|
|
text.write(code == 0 || code > 0xFFFF ? ' ' : String.fromCharCode(code));
|
|
cells.add(CellOffset(x, y));
|
|
}
|
|
}
|
|
for (final match in _link.allMatches(text.toString())) {
|
|
if (tapped < match.start || tapped >= match.end) continue;
|
|
final trimmed = match[0]!.replaceFirst(RegExp(r'[.,;:!?)\]}]+$'), '');
|
|
if (tapped >= match.start + trimmed.length) return null;
|
|
final uri = Uri.tryParse(trimmed);
|
|
if (uri == null || uri.host.isEmpty) return null;
|
|
final end = cells[match.start + trimmed.length - 1];
|
|
return ChatShellTerminalLink(
|
|
uri,
|
|
cells[match.start],
|
|
CellOffset(end.x + 1, end.y),
|
|
);
|
|
}
|
|
return null;
|
|
}
|