Skip to content

OpenAI's Codex Security CLI and TypeScript SDK for finding, validating, and fixing security vulnerabilities. npm: https://www.npmjs.com/package/@openai/codex-security

fix(history): wrap text by terminal display width (#1513)

wj6be9e20da57a
diff --git a/sdk/typescript/package.json b/sdk/typescript/package.json
index 9bcf6a7c7..8abea373f 100644
--- a/sdk/typescript/package.json
+++ b/sdk/typescript/package.json
@@ -89,6 +89,7 @@
"react": "19.3.0",
"semver": "7.8.5",
"smol-toml": "1.9.0",
+ "string-width": "8.2.2",
"yaml": "2.9.1",
"yauzl": "3.4.0",
"zod": "4.6.5"
diff --git a/sdk/typescript/pnpm-lock.yaml b/sdk/typescript/pnpm-lock.yaml
index f1a942ea1..e937c526a 100644
--- a/sdk/typescript/pnpm-lock.yaml
+++ b/sdk/typescript/pnpm-lock.yaml
@@ -61,6 +61,9 @@ importers:
smol-toml:
specifier: 1.9.0
version: 1.9.0
+ string-width:
+ specifier: 8.2.2
+ version: 8.2.2
yaml:
specifier: 2.9.1
version: 2.9.1
diff --git a/sdk/typescript/src/scan-history-renderer.ts b/sdk/typescript/src/scan-history-renderer.ts
index 3e98c6b1e..410d47530 100644
--- a/sdk/typescript/src/scan-history-renderer.ts
+++ b/sdk/typescript/src/scan-history-renderer.ts
@@ -1,4 +1,5 @@
import { basename, relative } from "node:path";
+import stringWidth from "string-width";
import type { JsonObject } from "./config.js";
export type HistoryCommand =
@@ -13,6 +14,9 @@ type RendererOptions = {
};
const STALE_SCAN_MILLISECONDS = 24 * 60 * 60 * 1_000;
+const GRAPHEME_SEGMENTER = new Intl.Segmenter(undefined, {
+ granularity: "grapheme",
+});
const STATUS_STYLES: Record<string, { color: number; icon: string }> = {
resolved: { color: 32, icon: "✓" },
@@ -79,13 +83,28 @@ export function renderScanHistory(
const wrap = (value: string, indent: number, prefix?: string): void => {
const available = width - indent - 2;
let line = "";
+ let lineWidth = 0;
for (const word of clean(value).split(/\s+/)) {
- if (line.length > 0 && line.length + word.length + 1 > available) {
+ if (line.length > 0 && lineWidth + stringWidth(word) + 1 > available) {
lines.push(`${prefix || " ".repeat(indent)}${line}`);
prefix = undefined;
- line = word;
- } else {
- line = line.length > 0 ? `${line} ${word}` : word;
+ line = "";
+ lineWidth = 0;
+ }
+ if (line.length > 0) {
+ line += " ";
+ lineWidth += 1;
+ }
+ for (const { segment } of GRAPHEME_SEGMENTER.segment(word)) {
+ const segmentWidth = stringWidth(segment);
+ if (lineWidth + segmentWidth > available) {
+ lines.push(`${prefix || " ".repeat(indent)}${line}`);
+ prefix = undefined;
+ line = "";
+ lineWidth = 0;
+ }
+ line += segment;
+ lineWidth += segmentWidth;
}
}
if (line.length > 0) {
diff --git a/sdk/typescript/tests-ts/scan-history-renderer.test.ts b/sdk/typescript/tests-ts/scan-history-renderer.test.ts
index 279df741b..da6264837 100644
--- a/sdk/typescript/tests-ts/scan-history-renderer.test.ts
+++ b/sdk/typescript/tests-ts/scan-history-renderer.test.ts
@@ -3,6 +3,56 @@ import { describe, expect, test } from "bun:test";
import { renderScanHistory } from "../src/scan-history-renderer.js";
describe("scan history renderer", () => {
+ test.each([
+ [
+ "spaced Chinese",
+ "权限校验失败 导致任意用户读取其他用户的私人扫描结果",
+ ["权限校验失败", "导致任意用户读取其他用户的私人扫", "描结果"],
+ ],
+ [
+ "unspaced Chinese",
+ "中文".repeat(20),
+ ["中文".repeat(8), "中文".repeat(8), "中文".repeat(4)],
+ ],
+ ["mixed text", "a".repeat(31) + "中文", ["a".repeat(31), "中文"]],
+ [
+ "combining marks",
+ "e\u0301".repeat(33),
+ ["e\u0301".repeat(32), "e\u0301"],
+ ],
+ ["joined emoji", "👩‍💻".repeat(17), ["👩‍💻".repeat(16), "👩‍💻"]],
+ ["long ASCII word", "a".repeat(65), ["a".repeat(32), "a".repeat(32), "a"]],
+ [
+ "English words",
+ "A finding title with several words that should wrap",
+ ["A finding title with several", "words that should wrap"],
+ ],
+ ] as const)(
+ "wraps %s titles at terminal columns",
+ (_name, title, expected) => {
+ for (const color of [false, true]) {
+ const output = renderScanHistory(
+ {
+ repository: "/repo",
+ findings: [{ title, severity: "high", path: "source.ts" }],
+ },
+ "findings",
+ { columns: 48, color },
+ );
+ const lines = stripVTControlCharacters(output).split("\n");
+ const start = lines.findIndex((line) => line.startsWith(" HIGH"));
+ const end = lines.indexOf(" source.ts");
+ expect(lines.slice(start, end)).toEqual(
+ expected.map(
+ (line, index) =>
+ `${index === 0 ? " HIGH " : " "}${line}`,
+ ),
+ );
+ if (color) expect(output).toContain("\u001B[31mHIGH \u001B[0m");
+ }
+ },
+ );
+
test("separates current repository findings from earlier observations", () => {
const text = renderScanHistory(
{