urlAt method

TerminalSearchMatch? urlAt(
  1. CellOffset position
)

Returns the URL at position, if any, along with the cell range it occupies. Returns null if position is not inside a detected URL.

Implementation

TerminalSearchMatch? urlAt(CellOffset position) {
  final buffer = this.buffer;
  if (position.y < 0 || position.y >= buffer.lines.length) return null;

  final firstLineIndex = logicalLineStart(buffer, position.y);
  final textBuffer = StringBuffer();
  final cells = LogicalLineCells();
  final logicalLine = buildLogicalLine(
    buffer,
    firstLineIndex,
    textBuffer,
    cells,
  );
  if (logicalLine.text.isEmpty) return null;

  final textOffset = cells.textOffsetFor(position.x, position.y);
  if (textOffset == null) return null;

  for (final match in _urlPattern.allMatches(logicalLine.text)) {
    if (textOffset < match.start || textOffset >= match.end) continue;

    final trimmedEnd = _trimTrailingPunctuation(
      logicalLine.text,
      match.start,
      match.end,
    );
    if (textOffset >= trimmedEnd) return null;

    final startCell = cells.cellAt(buffer, match.start);
    final endCell = cells.cellAt(buffer, trimmedEnd - 1);
    if (startCell == null || endCell == null) return null;

    return TerminalSearchMatch(
      range: BufferRangeLine(
        CellOffset(startCell.x, startCell.y),
        CellOffset(endCell.x + endCell.width, endCell.y),
      ),
      text: logicalLine.text.substring(match.start, trimmedEnd),
    );
  }

  return null;
}