Open-source Grok Bot alternative for named AI teammates. Local-first desktop, web, and mobile clients with per-bot tools, instructions, and memory. Independent fork of T3 Code.
import { collectWrappedTerminalLinkLine, extractTerminalLinks } from "../../terminal-links";
import { type GhosttyScrollbar, type GhosttySnapshot } from "./coreTypes";
import { type GhosttyCellRange, type GhosttyCellMetrics } from "./renderer";
export const CONTENT_PADDING = 4;
const MIN_SCROLLBAR_THUMB_HEIGHT = 18;
/**
* Vertical origin of the grid inside the mount. While content is shorter than
* the viewport the grid sits at the top like a fresh terminal. Once scrollback
* exists the prompt lives on the bottom row, so the grid anchors to the bottom
* edge instead: the sub-row remainder moves above row 0 and resizing within a
* row boundary keeps the prompt pinned instead of snapping up and down.
*/
export function terminalContentOriginY(
mountHeight: number,
padding: number,
rows: number,
cellHeight: number,
anchorBottom: boolean,
): number {
if (!anchorBottom) return padding;
const slack = mountHeight - padding * 2 - rows * cellHeight;
return padding + Math.max(0, slack);
}
export interface TerminalScrollbarGeometry {
readonly thumbHeight: number;
readonly thumbTop: number;
readonly maxOffset: number;
}
export function terminalScrollbarGeometry(
state: GhosttyScrollbar,
trackHeight: number,
): TerminalScrollbarGeometry | null {
const total = Math.max(0, state.total);
const len = Math.max(0, Math.min(state.len, total));
const maxOffset = Math.max(0, total - len);
if (trackHeight <= 0 || len <= 0 || maxOffset === 0) return null;
const thumbHeight = Math.min(
trackHeight,
Math.max(MIN_SCROLLBAR_THUMB_HEIGHT, (trackHeight * len) / total),
);
const travel = Math.max(0, trackHeight - thumbHeight);
const offset = Math.max(0, Math.min(state.offset, maxOffset));
return {
thumbHeight,
thumbTop: travel * (offset / maxOffset),
maxOffset,
};
}
export function terminalScrollbarOffsetAtPointer(
state: GhosttyScrollbar,
trackHeight: number,
pointerY: number,
pointerOffset: number,
): number {
const geometry = terminalScrollbarGeometry(state, trackHeight);
if (geometry === null) return 0;
const travel = Math.max(0, trackHeight - geometry.thumbHeight);
if (travel === 0) return 0;
const thumbTop = Math.max(0, Math.min(pointerY - pointerOffset, travel));
return Math.round((thumbTop / travel) * geometry.maxOffset);
}
export function terminalGridCellAt(options: {
bounds: { left: number; top: number };
clientX: number;
clientY: number;
cols: number;
rows: number;
metrics: Pick<GhosttyCellMetrics, "width" | "height">;
padding: number;
originY: number;
}): { x: number; y: number } | null {
const { bounds, clientX, clientY, cols, rows, metrics, padding, originY } = options;
const gridX = clientX - bounds.left - padding;
const gridY = clientY - bounds.top - originY;
if (gridX < 0 || gridY < 0 || gridX >= cols * metrics.width || gridY >= rows * metrics.height) {
return null;
}
return {
x: Math.floor(gridX / metrics.width),
y: Math.floor(gridY / metrics.height),
};
}
function terminalRowText(row: GhosttySnapshot["rowData"][number], trimRight: boolean): string {
const text = row.cells.map((cell) => cell.text || " ").join("");
return trimRight ? text.trimEnd() : text;
}
function terminalColumnOffset(row: GhosttySnapshot["rowData"][number], column: number): number {
let offset = 0;
for (let cellIndex = 0; cellIndex < column; cellIndex += 1) {
offset += row.cells[cellIndex]?.text.length || 1;
}
return offset;
}
export function terminalLinkAtPosition(
rows: GhosttySnapshot["rowData"],
rowIndex: number,
column: number,
): string | null {
return terminalLinkAtPositionWithRange(rows, rowIndex, column)?.text ?? null;
}
export interface TerminalLinkWithRange {
readonly text: string;
readonly range: GhosttyCellRange;
}
function terminalColumnAtOffset(row: GhosttySnapshot["rowData"][number], offset: number): number {
for (let column = 0; column < row.cells.length; column += 1) {
const nextOffset = terminalColumnOffset(row, column + 1);
if (offset < nextOffset) return column;
}
return Math.max(0, row.cells.length - 1);
}
export function terminalLinkAtPositionWithRange(
rows: GhosttySnapshot["rowData"],
rowIndex: number,
column: number,
): TerminalLinkWithRange | null {
const wrappedLine = collectWrappedTerminalLinkLine(rowIndex + 1, (index) => {
const row = rows[index];
if (!row) return null;
return {
isWrapped: row.isWrapContinuation,
translateToString: (trimRight = false) => terminalRowText(row, trimRight),
};
});
if (!wrappedLine) return null;
// Only viewport rows are available: a wrapped line whose head scrolled above
// the viewport would resolve a truncated match into a wrong link.
const firstSegment = wrappedLine.segments[0];
if (firstSegment && rows[firstSegment.bufferLineNumber - 1]?.isWrapContinuation) {
return null;
}
const segment = wrappedLine.segments.find((value) => value.bufferLineNumber === rowIndex + 1);
const row = rows[rowIndex];
if (!segment || !row) return null;
const lastSegment = wrappedLine.segments.at(-1);
const lastRow = lastSegment ? rows[lastSegment.bufferLineNumber - 1] : undefined;
// Ghostty's soft-wrap flag is authoritative: when the last collected row
// still wraps onward, its continuation is outside the viewport.
const continuesBelowViewport = lastRow !== undefined && lastRow.wrapsToNext;
const offset = segment.startIndex + terminalColumnOffset(row, column);
for (const match of extractTerminalLinks(wrappedLine.text)) {
if (offset >= match.start && offset < match.end) {
// A truncated tail must not activate as a complete link.
if (match.end === wrappedLine.text.length && continuesBelowViewport) return null;
const startSegment = wrappedLine.segments.find(
(value) => match.start >= value.startIndex && match.start < value.endIndex,
);
const endSegment = wrappedLine.segments.find(
(value) => match.end - 1 >= value.startIndex && match.end - 1 < value.endIndex,
);
const startRow = startSegment ? rows[startSegment.bufferLineNumber - 1] : undefined;
const endRow = endSegment ? rows[endSegment.bufferLineNumber - 1] : undefined;
if (!startSegment || !endSegment || !startRow || !endRow) return null;
return {
text: match.text,
range: {
start: {
x: terminalColumnAtOffset(startRow, match.start - startSegment.startIndex),
y: startSegment.bufferLineNumber - 1,
},
end: {
x: terminalColumnAtOffset(endRow, match.end - 1 - endSegment.startIndex),
y: endSegment.bufferLineNumber - 1,
},
},
};
}
}
return null;
}
export function terminalLinkAtColumn(row: GhosttySnapshot["rowData"][number], column: number) {
return terminalLinkAtPosition([row], 0, column);
}