// A run as a `Doc`, for a person's terminal: the same rows and issues as the log lines, with colour, status marks // and links, all of which the kit's renderers decide how to draw. import type { Block as DocBlock, Inline, InlineInput, TokenName } from "@effected/cli-kit"; import { Doc, Fmt, Status } from "@effected/cli-kit"; import { DiffDoc } from "./DiffDoc.js"; import { layoutIssues } from "./issues.js"; import type { LaidLine } from "./Lines.js"; import { GAP, STATUS, TONE, alignColumns, layoutGroup, locationOf, oneLine, verdictText } from "./Lines.js"; import type { Issue, Level, RunView, Tone } from "./RunView.js"; /** How {@link renderDoc} draws. */ export interface DocOptions { readonly level: Level; /** `false` leaves the header and the groups out, for printing below a live frame that already drew them. */ readonly rows: boolean; /** One line per group, as a watch rebuild prints. */ readonly compact: boolean; } type LineOf = ReadonlyArray; const TOKEN: Readonly> = { success: "success", warning: "warning", error: "error", muted: "muted", }; /** A mark is one column wide on a terminal that redraws, and so is a place kept for one. */ const NO_MARK = " "; const groupLine = (spinner: string | undefined) => (line: LaidLine): LineOf => [ line.indent, ...(line.mark === undefined ? [] : line.mark === "blank" ? [GAP] : spinner !== undefined && line.mark === "running" ? [Doc.text(spinner, "accent"), " "] : spinner !== undefined && line.mark === "pending" ? [NO_MARK, " "] : [Doc.status(Status.core, STATUS[line.mark]), " "]), ...line.pieces.flatMap((piece, index): LineOf => { const text = Doc.text(piece.text, piece.tone !== undefined ? TOKEN[piece.tone] : undefined); const cell = piece.link !== undefined ? Doc.link(piece.link, text, { suffix: false }) : text; // Nothing follows a row's last cell, so its padding would only be trailing space. const last = line.pieces.slice(index + 1).every((later) => later.text === ""); return last ? [cell] : [cell, " ".repeat(piece.pad)]; }), ]; const issueLine = (issue: Issue, indent: string): LineOf => { const token: TokenName = issue.level === "error" ? "error" : "warning"; const location = locationOf(issue)[0]; return [ indent, ...(location !== undefined ? [ issue.link !== undefined ? Doc.link(issue.link, Doc.text(location, token), { suffix: false }) : Doc.text(location, token), GAP, ] : []), ...(issue.code !== undefined ? [ issue.codeUrl !== undefined ? Doc.link({ url: issue.codeUrl }, Doc.text(oneLine(issue.code), token), { suffix: false }) : Doc.text(oneLine(issue.code), token), GAP, ] : []), Doc.text(oneLine(issue.text), token), ...(issue.ciFatal === true ? [Doc.text(`${GAP}[fails CI]`, token)] : []), ]; }; const plainIssue = (issue: Issue): string => [...locationOf(issue), ...(issue.code !== undefined ? [oneLine(issue.code)] : []), oneLine(issue.text)].join(GAP); const block = (lines: ReadonlyArray): ReadonlyArray => lines.length > 0 ? [Doc.lines(lines as ReadonlyArray, { wrap: false })] : []; /** What {@link renderRows} draws. */ export interface RowsOptions { /** One line per group, as a rebuild under `--watch` prints them. */ readonly compact: boolean; /** * The frame of a spinner, while the run is going: a running row is marked with it and a pending row with nothing. * Left out, every row has its status mark, as the settled report draws it. */ readonly spinner?: string; } const noticeLines = (view: RunView): ReadonlyArray => view.notices.map((notice): LineOf => [Doc.text(oneLine(notice), "warning")]); const topLines = (view: RunView, options: RowsOptions): ReadonlyArray => [ [ `${view.command} `, view.subjectLink !== undefined ? Doc.link(view.subjectLink, Doc.text(oneLine(view.subject), "emphasis"), { suffix: false }) : Doc.text(oneLine(view.subject), "emphasis"), ], ...noticeLines(view), ...view.groups.flatMap((group) => layoutGroup(group, options.compact).map(groupLine(options.spinner))), ]; /** * The header, the notices and every group's rows: the top of {@link renderDoc}'s report, and the live frame, which * settles to exactly it. * * @param view - the run so far, or finished * @param options - whether each group is one line, and the spinner's frame while the run is going */ export const renderRows = (view: RunView, options: RowsOptions): ReadonlyArray => block(topLines(view, options)); /** * The settled report for a terminal. * * @remarks * The header and the rows are one block and everything below them another, so a blank line parts them. Diffs follow * the rows, capped per document, and whole at the verbose level. Every string goes in as `Doc.text` data, which the * renderers strip of escape and control characters. The header carries no timestamp: a person is watching this run. * * @param view - the run * @param options - the level, whether to draw the rows, and whether each group is one line */ export const renderDoc = (view: RunView, options: DocOptions): ReadonlyArray => { const { level } = options; const notices = noticeLines(view); const drawRows = options.rows && level !== "quiet"; const top = drawRows ? topLines(view, { compact: options.compact }) : []; const verbose: ReadonlyArray = level === "verbose" ? [ ...view.blocks.flatMap( (entry): ReadonlyArray => [ [oneLine(entry.title)], ...alignColumns(entry.rows.map((row) => row.map((cell) => oneLine(cell.text)))).map( (line): LineOf => [`${GAP}${line}`], ), ], ), ...(view.suppressed.length > 0 ? [ [`suppressed (${view.suppressed.length})`] as LineOf, ...view.suppressed.map( (issue): LineOf => [Doc.text(`${GAP}${oneLine(issue.source)}${GAP}${plainIssue(issue)}`, "muted")], ), ] : []), ] : []; const diffs = level === "quiet" ? [] : view.diffs.map(({ path, diff }) => DiffDoc.render(path, diff, level === "verbose" ? { maxLines: Number.MAX_SAFE_INTEGER } : undefined), ); const layout = layoutIssues(view.issues, { level, rollup: true }); const issuesPath = view.verdict.issuesPath; const check = view.check; const rest: ReadonlyArray = [ ...(drawRows ? [] : notices), ...layout.lines.map((issue) => issueLine(issue, "")), ...(layout.api !== undefined ? [ [`api (${Fmt.plural(layout.api.total, "warning")})`] as LineOf, ...layout.api.full.map((issue) => issueLine(issue, GAP)), ...layout.api.rollup.map( (entry): LineOf => [ `${GAP}${[`${entry.count} x ${oneLine(entry.code)}`, ...(entry.file !== undefined ? [oneLine(entry.file)] : [])].join(GAP)}`, ], ), ...(layout.api.rollup.length > 0 ? [ [ Doc.text( `${GAP}all ${layout.api.total}: ${issuesPath !== undefined ? `${oneLine(issuesPath)}, or ` : ""}--verbose`, "muted", ), ] as LineOf, ] : []), ] : []), ...(layout.otherWarnings > 0 ? [[Fmt.plural(layout.otherWarnings, "other warning")] as LineOf] : []), ...(layout.firstError !== undefined ? [["first error: ", ...issueLine(layout.firstError, "")] as LineOf] : []), ...(check !== undefined && (level !== "quiet" || check.drift.length > 0) ? [ ["check"] as LineOf, ...(check.drift.length > 0 ? alignColumns( check.drift.map((entry) => [oneLine(entry.target), oneLine(entry.kind), oneLine(entry.path)]), ) : ["matches the committed build"] ).map((line): LineOf => [`${GAP}${line}`]), ] : []), ...view.hints.map((hint): LineOf => [oneLine(hint)]), ...(view.failure !== undefined ? [[Doc.text(`Failed: ${oneLine(view.failure)}`, "error")] as LineOf] : []), [ Doc.status(Status.core, TONE[view.verdict.tone]), " ", Doc.text(verdictText(view.verdict), TOKEN[view.verdict.tone]), ], ]; // A blank line parts the rows from what follows them, unless that is the verdict alone, and sets the diffs apart. const blank = block([[""]]); const middle = [...block(verbose), ...diffs.flatMap((diff, index) => (index === 0 ? [diff] : [...blank, diff]))]; const body = middle.length > 0 ? [...middle, ...(diffs.length > 0 ? blank : []), ...block(rest)] : block(rest); // With the rows left out a live frame drew them, and the same blank line parts the report from it. const below = top.length > 0 || (!options.rows && level !== "quiet"); const parted = below && (middle.length > 0 || rest.length > 1); return [...block(top), ...(parted ? blank : []), ...body]; };