import 'package:xterm/core.dart'; final RegExp _link = RegExp(r'''https?://[^\s<>"'`]+'''); /// 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) { 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(); 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)); } } 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; return uri; } return null; }