Skip to content

Commit e52f53d

Browse files
committed
add string width to display package
1 parent 9fc13f3 commit e52f53d

File tree

3 files changed

+32
-3
lines changed

3 files changed

+32
-3
lines changed

bun.lock

Lines changed: 20 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

display/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,8 @@
2929
"devDependencies": {
3030
"@types/node": "22",
3131
"@types/bun": "^1.2.11"
32+
},
33+
"dependencies": {
34+
"string-width": "^8.1.0"
3235
}
3336
}

display/src/grapheme-image.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import GraphemeSplitter from 'grapheme-splitter'
22
import { isEqual } from 'lodash'
3+
import stringWidth from 'string-width'
34
import stripAnsi from 'strip-ansi'
45

56
import {
@@ -11,6 +12,7 @@ import {
1112
moveCursor,
1213
} from './ansi'
1314

15+
export const BLANK_GRAPHEME = ' ' as $GraphemeString
1416
type $GraphemeString = string & { readonly _brand: 'GraphemeString' }
1517

1618
export type Grapheme = {
@@ -28,10 +30,15 @@ export function toGraphemeString(grapheme: string): $GraphemeString {
2830
const stripped = stripAnsi(grapheme)
2931
const numGraphemes = splitter.countGraphemes(stripped)
3032
if (numGraphemes === 0) {
31-
return ' ' as $GraphemeString
33+
return BLANK_GRAPHEME
3234
}
3335

34-
return splitter.iterateGraphemes(stripped).next().value as $GraphemeString
36+
const first = splitter.iterateGraphemes(stripped).next()
37+
.value as $GraphemeString
38+
if (stringWidth(first) < 1) {
39+
return BLANK_GRAPHEME
40+
}
41+
return first
3542
}
3643

3744
function equalStyles(a: Grapheme, b: Grapheme): boolean {

0 commit comments

Comments
 (0)