import type { ComposerItem } from "../model/composerMenu.ts"; import { colors } from "./theme.ts"; const MAX_ROWS = 8; /** * The web composer's / $ @ menu, just above the message box: ↑/↓ or ctrl+n/p move, tab or * enter chooses, a click chooses, esc closes. */ export function ComposerMenu(props: { readonly items: readonly ComposerItem[]; readonly selected: number; readonly empty: string; readonly width: number; readonly onPick: (index: number) => void }) { const { items, selected } = props; const start = Math.max(0, Math.min(selected - Math.floor(MAX_ROWS / 2), items.length - MAX_ROWS)); const inner = Math.max(10, props.width - 4); return ( {items.length === 0 ? ( {props.empty} ) : ( items.slice(start, start + MAX_ROWS).map((item, offset) => { const index = start + offset; const label = truncate(item.label, Math.floor(inner * 0.5)); return ( props.onPick(index)}> {label} {` ${truncate(item.description, inner - label.length - 2)}`} ); }) )} {items.length > MAX_ROWS ? {`${selected + 1} of ${items.length}`} : null} ); } function truncate(text: string, width: number): string { const single = text.replace(/\s+/g, " "); return single.length > width ? `${single.slice(0, Math.max(0, width - 1))}…` : single; }