mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-07 17:51:24 +00:00
chore: Enable "curly" rule to avoid single-statement if confusion/errors.
This commit is contained in:
@@ -3,7 +3,9 @@ import { visibleWidth } from "./ansi.js";
|
||||
import { stylePromptTitle } from "./prompt-style.js";
|
||||
|
||||
function splitLongWord(word: string, maxLen: number): string[] {
|
||||
if (maxLen <= 0) return [word];
|
||||
if (maxLen <= 0) {
|
||||
return [word];
|
||||
}
|
||||
const chars = Array.from(word);
|
||||
const parts: string[] = [];
|
||||
for (let i = 0; i < chars.length; i += maxLen) {
|
||||
@@ -13,7 +15,9 @@ function splitLongWord(word: string, maxLen: number): string[] {
|
||||
}
|
||||
|
||||
function wrapLine(line: string, maxWidth: number): string[] {
|
||||
if (line.trim().length === 0) return [line];
|
||||
if (line.trim().length === 0) {
|
||||
return [line];
|
||||
}
|
||||
const match = line.match(/^(\s*)([-*\u2022]\s+)?(.*)$/);
|
||||
const indent = match?.[1] ?? "";
|
||||
const bullet = match?.[2] ?? "";
|
||||
@@ -37,7 +41,9 @@ function wrapLine(line: string, maxWidth: number): string[] {
|
||||
lines.push(prefix + first);
|
||||
prefix = nextPrefix;
|
||||
available = nextWidth;
|
||||
for (const part of parts) lines.push(prefix + part);
|
||||
for (const part of parts) {
|
||||
lines.push(prefix + part);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
current = word;
|
||||
@@ -58,7 +64,9 @@ function wrapLine(line: string, maxWidth: number): string[] {
|
||||
const parts = splitLongWord(word, available);
|
||||
const first = parts.shift() ?? "";
|
||||
lines.push(prefix + first);
|
||||
for (const part of parts) lines.push(prefix + part);
|
||||
for (const part of parts) {
|
||||
lines.push(prefix + part);
|
||||
}
|
||||
current = "";
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -1,17 +1,25 @@
|
||||
let activeStream: NodeJS.WriteStream | null = null;
|
||||
|
||||
export function registerActiveProgressLine(stream: NodeJS.WriteStream): void {
|
||||
if (!stream.isTTY) return;
|
||||
if (!stream.isTTY) {
|
||||
return;
|
||||
}
|
||||
activeStream = stream;
|
||||
}
|
||||
|
||||
export function clearActiveProgressLine(): void {
|
||||
if (!activeStream?.isTTY) return;
|
||||
if (!activeStream?.isTTY) {
|
||||
return;
|
||||
}
|
||||
activeStream.write("\r\x1b[2K");
|
||||
}
|
||||
|
||||
export function unregisterActiveProgressLine(stream?: NodeJS.WriteStream): void {
|
||||
if (!activeStream) return;
|
||||
if (stream && activeStream !== stream) return;
|
||||
if (!activeStream) {
|
||||
return;
|
||||
}
|
||||
if (stream && activeStream !== stream) {
|
||||
return;
|
||||
}
|
||||
activeStream = null;
|
||||
}
|
||||
|
||||
@@ -20,7 +20,9 @@ export function createSafeStreamWriter(options: SafeStreamWriterOptions = {}): S
|
||||
let notified = false;
|
||||
|
||||
const noteBrokenPipe = (err: NodeJS.ErrnoException, stream: NodeJS.WriteStream) => {
|
||||
if (notified) return;
|
||||
if (notified) {
|
||||
return;
|
||||
}
|
||||
notified = true;
|
||||
options.onBrokenPipe?.(err, stream);
|
||||
};
|
||||
@@ -35,7 +37,9 @@ export function createSafeStreamWriter(options: SafeStreamWriterOptions = {}): S
|
||||
};
|
||||
|
||||
const write = (stream: NodeJS.WriteStream, text: string): boolean => {
|
||||
if (closed) return false;
|
||||
if (closed) {
|
||||
return false;
|
||||
}
|
||||
try {
|
||||
options.beforeWrite?.();
|
||||
} catch (err) {
|
||||
|
||||
@@ -50,14 +50,18 @@ describe("renderTable", () => {
|
||||
|
||||
const ESC = "\u001b";
|
||||
for (let i = 0; i < out.length; i += 1) {
|
||||
if (out[i] !== ESC) continue;
|
||||
if (out[i] !== ESC) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// SGR: ESC [ ... m
|
||||
if (out[i + 1] === "[") {
|
||||
let j = i + 2;
|
||||
while (j < out.length) {
|
||||
const ch = out[j];
|
||||
if (ch === "m") break;
|
||||
if (ch === "m") {
|
||||
break;
|
||||
}
|
||||
if (ch && ch >= "0" && ch <= "9") {
|
||||
j += 1;
|
||||
continue;
|
||||
|
||||
@@ -21,15 +21,21 @@ export type RenderTableOptions = {
|
||||
};
|
||||
|
||||
function repeat(ch: string, n: number): string {
|
||||
if (n <= 0) return "";
|
||||
if (n <= 0) {
|
||||
return "";
|
||||
}
|
||||
return ch.repeat(n);
|
||||
}
|
||||
|
||||
function padCell(text: string, width: number, align: Align): string {
|
||||
const w = visibleWidth(text);
|
||||
if (w >= width) return text;
|
||||
if (w >= width) {
|
||||
return text;
|
||||
}
|
||||
const pad = width - w;
|
||||
if (align === "right") return `${repeat(" ", pad)}${text}`;
|
||||
if (align === "right") {
|
||||
return `${repeat(" ", pad)}${text}`;
|
||||
}
|
||||
if (align === "center") {
|
||||
const left = Math.floor(pad / 2);
|
||||
const right = pad - left;
|
||||
@@ -39,7 +45,9 @@ function padCell(text: string, width: number, align: Align): string {
|
||||
}
|
||||
|
||||
function wrapLine(text: string, width: number): string[] {
|
||||
if (width <= 0) return [text];
|
||||
if (width <= 0) {
|
||||
return [text];
|
||||
}
|
||||
|
||||
// ANSI-aware wrapping: never split inside ANSI SGR/OSC-8 sequences.
|
||||
// We don't attempt to re-open styling per line; terminals keep SGR state
|
||||
@@ -55,7 +63,9 @@ function wrapLine(text: string, width: number): string[] {
|
||||
let j = i + 2;
|
||||
while (j < text.length) {
|
||||
const ch = text[j];
|
||||
if (ch === "m") break;
|
||||
if (ch === "m") {
|
||||
break;
|
||||
}
|
||||
if (ch && ch >= "0" && ch <= "9") {
|
||||
j += 1;
|
||||
continue;
|
||||
@@ -85,14 +95,18 @@ function wrapLine(text: string, width: number): string[] {
|
||||
}
|
||||
|
||||
const cp = text.codePointAt(i);
|
||||
if (!cp) break;
|
||||
if (!cp) {
|
||||
break;
|
||||
}
|
||||
const ch = String.fromCodePoint(cp);
|
||||
tokens.push({ kind: "char", value: ch });
|
||||
i += ch.length;
|
||||
}
|
||||
|
||||
const firstCharIndex = tokens.findIndex((t) => t.kind === "char");
|
||||
if (firstCharIndex < 0) return [text];
|
||||
if (firstCharIndex < 0) {
|
||||
return [text];
|
||||
}
|
||||
let lastCharIndex = -1;
|
||||
for (let i = tokens.length - 1; i >= 0; i -= 1) {
|
||||
if (tokens[i]?.kind === "char") {
|
||||
@@ -129,12 +143,16 @@ function wrapLine(text: string, width: number): string[] {
|
||||
|
||||
const pushLine = (value: string) => {
|
||||
const cleaned = value.replace(/\s+$/, "");
|
||||
if (cleaned.trim().length === 0) return;
|
||||
if (cleaned.trim().length === 0) {
|
||||
return;
|
||||
}
|
||||
lines.push(cleaned);
|
||||
};
|
||||
|
||||
const flushAt = (breakAt: number | null) => {
|
||||
if (buf.length === 0) return;
|
||||
if (buf.length === 0) {
|
||||
return;
|
||||
}
|
||||
if (breakAt == null || breakAt <= 0) {
|
||||
pushLine(bufToString());
|
||||
buf.length = 0;
|
||||
@@ -166,11 +184,15 @@ function wrapLine(text: string, width: number): string[] {
|
||||
const ch = token.value;
|
||||
if (skipNextLf) {
|
||||
skipNextLf = false;
|
||||
if (ch === "\n") continue;
|
||||
if (ch === "\n") {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
if (ch === "\n" || ch === "\r") {
|
||||
flushAt(buf.length);
|
||||
if (ch === "\r") skipNextLf = true;
|
||||
if (ch === "\r") {
|
||||
skipNextLf = true;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if (bufVisible + 1 > width && bufVisible > 0) {
|
||||
@@ -179,21 +201,33 @@ function wrapLine(text: string, width: number): string[] {
|
||||
|
||||
buf.push(token);
|
||||
bufVisible += 1;
|
||||
if (isBreakChar(ch)) lastBreakIndex = buf.length;
|
||||
if (isBreakChar(ch)) {
|
||||
lastBreakIndex = buf.length;
|
||||
}
|
||||
}
|
||||
|
||||
flushAt(buf.length);
|
||||
if (!lines.length) return [""];
|
||||
if (!prefixAnsi && !suffixAnsi) return lines;
|
||||
if (!lines.length) {
|
||||
return [""];
|
||||
}
|
||||
if (!prefixAnsi && !suffixAnsi) {
|
||||
return lines;
|
||||
}
|
||||
return lines.map((line) => {
|
||||
if (!line) return line;
|
||||
if (!line) {
|
||||
return line;
|
||||
}
|
||||
return `${prefixAnsi}${line}${suffixAnsi}`;
|
||||
});
|
||||
}
|
||||
|
||||
function normalizeWidth(n: number | undefined): number | undefined {
|
||||
if (n == null) return undefined;
|
||||
if (!Number.isFinite(n) || n <= 0) return undefined;
|
||||
if (n == null) {
|
||||
return undefined;
|
||||
}
|
||||
if (!Number.isFinite(n) || n <= 0) {
|
||||
return undefined;
|
||||
}
|
||||
return Math.floor(n);
|
||||
}
|
||||
|
||||
@@ -259,13 +293,19 @@ export function renderTable(opts: RenderTableOptions): string {
|
||||
while (over > 0) {
|
||||
let progressed = false;
|
||||
for (const i of order) {
|
||||
if ((widths[i] ?? 0) <= (minWidths[i] ?? 0)) continue;
|
||||
if ((widths[i] ?? 0) <= (minWidths[i] ?? 0)) {
|
||||
continue;
|
||||
}
|
||||
widths[i] = (widths[i] ?? 0) - 1;
|
||||
over -= 1;
|
||||
progressed = true;
|
||||
if (over <= 0) break;
|
||||
if (over <= 0) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!progressed) {
|
||||
break;
|
||||
}
|
||||
if (!progressed) break;
|
||||
}
|
||||
};
|
||||
|
||||
@@ -298,13 +338,19 @@ export function renderTable(opts: RenderTableOptions): string {
|
||||
while (extra > 0) {
|
||||
let progressed = false;
|
||||
for (const i of flexCols) {
|
||||
if ((widths[i] ?? 0) >= (caps[i] ?? Number.POSITIVE_INFINITY)) continue;
|
||||
if ((widths[i] ?? 0) >= (caps[i] ?? Number.POSITIVE_INFINITY)) {
|
||||
continue;
|
||||
}
|
||||
widths[i] = (widths[i] ?? 0) + 1;
|
||||
extra -= 1;
|
||||
progressed = true;
|
||||
if (extra <= 0) break;
|
||||
if (extra <= 0) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!progressed) {
|
||||
break;
|
||||
}
|
||||
if (!progressed) break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user