CLI: dedupe config validate errors and expose allowed values

This commit is contained in:
Gustavo Madeira Santana
2026-03-02 20:05:12 -05:00
parent a44843507f
commit f26853f14c
41 changed files with 1393 additions and 134 deletions

View File

@@ -0,0 +1,12 @@
import { describe, expect, it } from "vitest";
import { sanitizeTerminalText } from "./safe-text.js";
describe("sanitizeTerminalText", () => {
it("removes C1 control characters", () => {
expect(sanitizeTerminalText("a\u009bb\u0085c")).toBe("abc");
});
it("escapes line controls while preserving printable text", () => {
expect(sanitizeTerminalText("a\tb\nc\rd")).toBe("a\\tb\\nc\\rd");
});
});

20
src/terminal/safe-text.ts Normal file
View File

@@ -0,0 +1,20 @@
import { stripAnsi } from "./ansi.js";
/**
* Normalize untrusted text for single-line terminal/log rendering.
*/
export function sanitizeTerminalText(input: string): string {
const normalized = stripAnsi(input)
.replace(/\r/g, "\\r")
.replace(/\n/g, "\\n")
.replace(/\t/g, "\\t");
let sanitized = "";
for (const char of normalized) {
const code = char.charCodeAt(0);
const isControl = (code >= 0x00 && code <= 0x1f) || (code >= 0x7f && code <= 0x9f);
if (!isControl) {
sanitized += char;
}
}
return sanitized;
}