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

refactor(cli): share terminal grapheme handling (#2232)

Michael D'Angelofb7ffc9f34b8
diff --git a/sdk/typescript/src/cli.ts b/sdk/typescript/src/cli.ts
index 9112520e1..d61e7ef5d 100644
--- a/sdk/typescript/src/cli.ts
+++ b/sdk/typescript/src/cli.ts
@@ -310,6 +310,7 @@ import {
type HistoryCommand,
} from "./scan-history-renderer.js";
import { ScanDashboard } from "./scan-dashboard.js";
+import { graphemes } from "./terminal-columns.js";
import {
ACCOUNT_LIMITS_UNAVAILABLE,
formatAccountLimits,
@@ -407,9 +408,6 @@ const HIDE_CURSOR = "\u001B[?25l";
const SHOW_CURSOR = "\u001B[?25h";
const CHILD_TERMINATION_GRACE_MS = 1_000;
const DUPLICATE_SIGNAL_WINDOW_MS = 500;
-const PUBLICATION_GRAPHEME_SEGMENTER = new Intl.Segmenter(undefined, {
- granularity: "grapheme",
-});
type Writable = Pick<NodeJS.WriteStream, "write"> & {
on?(event: "error", listener: (error: Error) => void): unknown;
@@ -637,9 +635,7 @@ function publicationScanAge(timestamp: string, now: number): string {
}
function publicationDisplayWidth(value: string): number {
- const segments = PUBLICATION_GRAPHEME_SEGMENTER.segment(
- stripVTControlCharacters(value),
- );
+ const segments = graphemes.segment(stripVTControlCharacters(value));
let width = 0;
for (const { segment } of segments) {
@@ -663,9 +659,7 @@ function fitProgressLine(line: string, width: number): string {
const plain = stripVTControlCharacters(line);
let ellipsisIndex = 0;
let used = 0;
- for (const { index, segment } of PUBLICATION_GRAPHEME_SEGMENTER.segment(
- plain,
- )) {
+ for (const { index, segment } of graphemes.segment(plain)) {
used += stringWidth(segment.toWellFormed());
if (used > width)
return width > 0 ? `${plain.slice(0, ellipsisIndex)}…` : "";
diff --git a/sdk/typescript/src/patch-tui.tsx b/sdk/typescript/src/patch-tui.tsx
index 108c6d492..8484cd5a7 100644
--- a/sdk/typescript/src/patch-tui.tsx
+++ b/sdk/typescript/src/patch-tui.tsx
@@ -5,6 +5,7 @@ import { Box, Text, render, useApp, useInput, useStdout } from "ink";
import { useEffect, useEffectEvent, useMemo, useRef, useState } from "react";
import type { Finding, FindingCodeEvidence } from "./models.js";
import { SCAN_SEVERITIES } from "./scan-settings.js";
+import { graphemes } from "./terminal-columns.js";
const SEVERITY_COLORS = {
critical: "redBright",
@@ -18,9 +19,6 @@ type PatchSeverity = keyof typeof SEVERITY_COLORS;
const SEVERITIES = Object.keys(SEVERITY_COLORS) as PatchSeverity[];
const HEADING = /^[A-Z][A-Z ]+$/u;
const SOURCE_LINE = /^([› ]\s*\d* │ )(.*)$/u;
-const GRAPHEME_SEGMENTER = new Intl.Segmenter(undefined, {
- granularity: "grapheme",
-});
const DETAIL_ORDER = [
"summary",
@@ -491,12 +489,10 @@ export function PatchTui({
});
setEditing(null);
} else if (key.backspace || key.delete) {
- setDraft((previous) =>
- previous.slice(
- 0,
- Array.from(GRAPHEME_SEGMENTER.segment(previous)).at(-1)?.index ?? 0,
- ),
- );
+ setDraft((text) => {
+ const last = graphemes.segment(text).containing(text.length - 1);
+ return text.slice(0, last?.index ?? 0);
+ });
} else if (!key.ctrl && !key.meta && input.length > 0) {
setDraft((previous) => previous + input);
}