Skip to content

util: add getStringWidth() - #66642

Closed
marcopiraccini wants to merge 1 commit into
nodejs:mainfrom
marcopiraccini:util-get-string-width
Closed

marcopiraccini wants to merge 1 commit into
nodejs:mainfrom
marcopiraccini:util-get-string-width

Conversation

@marcopiraccini

@marcopiraccini marcopiraccini commented Oct 10, 2026 •

Copy link
Copy Markdown
Contributor

Expose getStringWidth() from internal/util/inspect as util.getStringWidth(str), marked experimental.

It returns an estimate of the terminal columns a string takes: full-width characters count as two, zero-width characters as zero, and ANSI escape sequences are ignored. Node.js already uses it in readline, the REPL, util.inspect() and the test runner; string-width on npm serves the same need (about 520M downloads a week), and Bun and Deno expose equivalents.

The ICU options (ambiguousAsFullWidth, expandEmojiSequence) are not exposed yet, which is why the API starts as experimental. Joined emoji sequences are counted per component.

Precedent: util.stripVTControlCharacters(), exposed the same way in #40214.

Refs: #40214

Expose the string width helper that readline, the REPL and the test
runner already use. It returns the number of terminal columns a string
occupies: full-width characters count as two, zero-width characters as
zero, and ANSI escape sequences are ignored. The API is experimental.

Refs: nodejs#40214
Signed-off-by: marcopiraccini <marco.piraccini@gmail.com>
@nodejs-github-bot nodejs-github-bot added needs-ci PRs that need a full CI run. util Issues and PRs related to the built-in util module. labels Oct 10, 2026
@marcopiraccini

Copy link
Copy Markdown
Contributor Author

This needs more investigation, actually. Closing for the time being.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs-ci PRs that need a full CI run. util Issues and PRs related to the built-in util module.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants