Skip to content

Commit 865b840

Browse files
committed
Fix CLI help display width measurement
1 parent acc1e53 commit 865b840

3 files changed

Lines changed: 78 additions & 2 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"effect": patch
3+
---
4+
5+
Align CLI help tables by terminal display width for wide, emoji, combining, and zero-width graphemes.

‎packages/effect/src/unstable/cli/CliOutput.ts‎

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -380,10 +380,47 @@ const stripAnsi = (text: string): string => {
380380
}
381381

382382
/**
383-
* Gets the visual length of a string (excluding ANSI codes).
383+
* Gets the terminal display width of a string (excluding ANSI codes).
384384
* @internal
385385
*/
386-
const visualLength = (text: string): number => stripAnsi(text).length
386+
const graphemeSegmenter = new Intl.Segmenter(undefined, { granularity: "grapheme" })
387+
388+
const zeroWidthCodePoints = /[\p{Mark}\p{Control}\p{Default_Ignorable_Code_Point}]/gu
389+
const emojiPresentation = /\p{Emoji_Presentation}/u
390+
391+
const isFullWidthCodePoint = (codePoint: number): boolean => {
392+
if (codePoint < 0x1100) return false
393+
394+
return codePoint <= 0x115f ||
395+
codePoint === 0x2329 ||
396+
codePoint === 0x232a ||
397+
(codePoint >= 0x2e80 && codePoint <= 0x303e) ||
398+
(codePoint >= 0x3040 && codePoint <= 0xa4cf) ||
399+
(codePoint >= 0xac00 && codePoint <= 0xd7a3) ||
400+
(codePoint >= 0xf900 && codePoint <= 0xfaff) ||
401+
(codePoint >= 0xfe10 && codePoint <= 0xfe19) ||
402+
(codePoint >= 0xfe30 && codePoint <= 0xfe6f) ||
403+
(codePoint >= 0xff00 && codePoint <= 0xff60) ||
404+
(codePoint >= 0xffe0 && codePoint <= 0xffe6) ||
405+
(codePoint >= 0x1b000 && codePoint <= 0x1b2ff) ||
406+
(codePoint >= 0x1f200 && codePoint <= 0x1f251) ||
407+
(codePoint >= 0x20000 && codePoint <= 0x3fffd)
408+
}
409+
410+
const graphemeWidth = (grapheme: string): number => {
411+
const visible = grapheme.replace(zeroWidthCodePoints, "")
412+
if (visible.length === 0) return 0
413+
if (emojiPresentation.test(grapheme) || grapheme.includes("\uFE0F")) return 2
414+
return isFullWidthCodePoint(visible.codePointAt(0)!) ? 2 : 1
415+
}
416+
417+
const visualLength = (text: string): number => {
418+
let length = 0
419+
for (const { segment } of graphemeSegmenter.segment(stripAnsi(text))) {
420+
length += graphemeWidth(segment)
421+
}
422+
return length
423+
}
387424

388425
/**
389426
* Helper function to pad strings to a specified width.

‎packages/effect/test/unstable/cli/Help.test.ts‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,40 @@ describe("Command help output", () => {
130130
expect(shortLine!.indexOf("Short flag description")).toBe(longLine!.indexOf("Long flag description"))
131131
}).pipe(Effect.provide(TestLayer)))
132132

133+
it.effect("aligns flag descriptions when flag names contain wide graphemes", () =>
134+
Effect.gen(function*() {
135+
const command = Command.make("tool", {
136+
file: Flag.boolean("ファイル").pipe(Flag.withDescription("Wide flag description")),
137+
emoji: Flag.boolean("👩‍💻").pipe(Flag.withDescription("Emoji flag description")),
138+
verbose: Flag.boolean("verbose").pipe(Flag.withDescription("ASCII flag description"))
139+
})
140+
const run = Command.runWith(command, { version: "1.0.0" })
141+
142+
yield* run(["--help"])
143+
144+
const lines = (yield* TestConsole.logLines).join("\n").split("\n")
145+
assert.include(lines, " --ファイル Wide flag description")
146+
assert.include(lines, " --👩‍💻 Emoji flag description")
147+
assert.include(lines, " --verbose ASCII flag description")
148+
}).pipe(Effect.provide(TestLayer)))
149+
150+
it.effect("aligns flag descriptions when flag names contain zero-width code points", () =>
151+
Effect.gen(function*() {
152+
const command = Command.make("tool", {
153+
combining: Flag.boolean("e\u0301").pipe(Flag.withDescription("Combining flag description")),
154+
zeroWidth: Flag.boolean("a\u200Bb").pipe(Flag.withDescription("Zero-width flag description")),
155+
ascii: Flag.boolean("abcd").pipe(Flag.withDescription("ASCII flag description"))
156+
})
157+
const run = Command.runWith(command, { version: "1.0.0" })
158+
159+
yield* run(["--help"])
160+
161+
const lines = (yield* TestConsole.logLines).join("\n").split("\n")
162+
assert.include(lines, " --e\u0301 Combining flag description")
163+
assert.include(lines, " --a\u200Bb Zero-width flag description")
164+
assert.include(lines, " --abcd ASCII flag description")
165+
}).pipe(Effect.provide(TestLayer)))
166+
133167
it.effect("separates long subcommand and argument names from their descriptions", () =>
134168
Effect.gen(function*() {
135169
const child = Command.make("account:set-password", {

0 commit comments

Comments
 (0)