Files
likang 7e01147566
CI / typecheck + test + build (push) Has been cancelled
Initial commit: import VibeMake PCB Studio
2026-11-03 11:20:08 +08:00

175 lines
5.5 KiB
TypeScript

// SPDX-License-Identifier: GPL-3.0-or-later
// Copyright (C) 2026 ZiroEDA and contributors.
// Portions derived from KiCad, copyright The KiCad Developers. See NOTICE.md.
/**
* Reporting. Counterpart: `include/reporter.h` (REPORTER) and
* `include/widgets/report_severity.h` (SEVERITY).
*
* A REPORTER is the write end of a message stream; the widget that displays it
* (WX_HTML_REPORT_PANEL) is the read end. Lines carry a severity and a location
* - head lines print first, tail lines last, everything else in report order.
*/
/** SEVERITY, a bit flag, so a filter is a mask (report_severity.h). */
export const RPT_SEVERITY_UNDEFINED = 0x01;
export const RPT_SEVERITY_INFO = 0x02;
export const RPT_SEVERITY_EXCLUSION = 0x04;
export const RPT_SEVERITY_ACTION = 0x08;
export const RPT_SEVERITY_WARNING = 0x10;
export const RPT_SEVERITY_ERROR = 0x20;
export const RPT_SEVERITY_IGNORE = 0x40;
export const RPT_SEVERITY_DEBUG = 0x80;
export type Severity = number;
/** REPORTER::LOCATION. */
export type ReportLocation = 'head' | 'body' | 'tail';
/** WX_HTML_REPORT_PANEL::REPORT_LINE. */
export interface ReportLine {
message: string;
severity: Severity;
location: ReportLocation;
}
/** REPORTER, Report()/ReportTail()/ReportHead() onto a message sink. */
export class Reporter {
readonly lines: ReportLine[] = [];
report(message: string, severity: Severity = RPT_SEVERITY_UNDEFINED): this {
this.lines.push({ message, severity, location: 'body' });
return this;
}
reportHead(message: string, severity: Severity = RPT_SEVERITY_UNDEFINED): this {
this.lines.push({ message, severity, location: 'head' });
return this;
}
reportTail(message: string, severity: Severity = RPT_SEVERITY_UNDEFINED): this {
this.lines.push({ message, severity, location: 'tail' });
return this;
}
/** `REPORTER::Report`: a message in the body. */
Report(aText: string, aSeverity: Severity = RPT_SEVERITY_UNDEFINED): this {
return this.report(aText, aSeverity);
}
/** `REPORTER::ReportHead`: a message placed before the body. */
ReportHead(aText: string, aSeverity: Severity = RPT_SEVERITY_UNDEFINED): this {
return this.reportHead(aText, aSeverity);
}
/** `REPORTER::ReportTail`: a message placed after the body. */
ReportTail(aText: string, aSeverity: Severity = RPT_SEVERITY_UNDEFINED): this {
return this.reportTail(aText, aSeverity);
}
clear(): void {
this.lines.length = 0;
}
/** REPORTER::HasMessage. */
hasMessage(): boolean {
return this.lines.length > 0;
}
/** WX_HTML_REPORT_PANEL::Count, lines matching a severity mask. */
count(severityMask: Severity): number {
return this.lines.filter((l) => severityMask & l.severity).length;
}
}
/**
* The panel's display order: head lines, then the body (optionally sorted by
* severity, as Flush( true ) does), then tail lines.
*/
export function orderedReportLines(lines: readonly ReportLine[], sort = false): ReportLine[] {
const head = lines.filter((l) => l.location === 'head');
const body = lines.filter((l) => l.location === 'body');
const tail = lines.filter((l) => l.location === 'tail');
if (sort) body.sort((a, b) => a.severity - b.severity);
return [...head, ...body, ...tail];
}
/** WX_HTML_REPORT_PANEL::generatePlainText, the text a saved report holds. */
export function reportLineToPlainText(line: ReportLine): string {
switch (line.severity) {
case RPT_SEVERITY_ERROR:
return `Error: ${line.message}\n`;
case RPT_SEVERITY_WARNING:
return `Warning: ${line.message}\n`;
case RPT_SEVERITY_INFO:
return `Info: ${line.message}\n`;
default:
return `${line.message}\n`;
}
}
/** `SeverityFromString( aSeverity )` (common/widgets/ui_common.cpp:56). */
export function SeverityFromString(aSeverity: string): Severity {
if (aSeverity === 'warning') return RPT_SEVERITY_WARNING;
else if (aSeverity === 'ignore') return RPT_SEVERITY_IGNORE;
else return RPT_SEVERITY_ERROR;
}
/** `SeverityToString( aSeverity )` (common/widgets/ui_common.cpp:67). */
export function SeverityToString(aSeverity: Severity): string {
if (aSeverity === RPT_SEVERITY_IGNORE) return 'ignore';
else if (aSeverity === RPT_SEVERITY_WARNING) return 'warning';
else return 'error';
}
/** `WX_STRING_REPORTER`: messages kept as one string, a line each. */
export class WX_STRING_REPORTER extends Reporter {
private m_string = '';
override Report(aText: string, aSeverity: Severity = RPT_SEVERITY_UNDEFINED): this {
super.report(aText, aSeverity);
this.m_string += `${aText}\n`;
return this;
}
override report(aText: string, aSeverity: Severity = RPT_SEVERITY_UNDEFINED): this {
return this.Report(aText, aSeverity);
}
HasMessage(): boolean {
return this.m_string !== '';
}
GetMessages(): string {
return this.m_string;
}
Clear(): void {
super.clear();
this.m_string = '';
}
}
/**
* `NULL_REPORTER` (reporter.h): a singleton reporter that throws away everything it is told.
*/
export class NULL_REPORTER extends Reporter {
private static s_instance: NULL_REPORTER | null = null;
static GetInstance(): NULL_REPORTER {
NULL_REPORTER.s_instance ??= new NULL_REPORTER();
return NULL_REPORTER.s_instance;
}
override report(_message: string, _severity: Severity = RPT_SEVERITY_UNDEFINED): this {
return this;
}
override reportHead(_message: string, _severity: Severity = RPT_SEVERITY_UNDEFINED): this {
return this;
}
override reportTail(_message: string, _severity: Severity = RPT_SEVERITY_UNDEFINED): this {
return this;
}
}