Skip to content

ansi: document that StringWidth treats tabs as zero width - #864

Open
c-tonneslan wants to merge 1 commit into
charmbracelet:mainfrom
c-tonneslan:docs/ansi-stringwidth-tab-note
Open

ansi: document that StringWidth treats tabs as zero width#864
c-tonneslan wants to merge 1 commit into
charmbracelet:mainfrom
c-tonneslan:docs/ansi-stringwidth-tab-note

Conversation

@c-tonneslan

Copy link
Copy Markdown

Closes #644.

ansi.StringWidth(\"\t\") returns 0 because tab is a C0 control character whose display width depends on terminal tabstop settings the package can't know about. That's intentional but unexpected if you're feeding raw user input through it. @aymanbagabas confirmed in the issue thread that this is by design and suggested a doc note with a strings.ReplaceAll example, so that's what this does. Same caveat noted on StringWidthWc.

This trips people up: ansi.StringWidth("\t") returns 0 because tab is a
C0 control character whose display width depends entirely on terminal
tabstop settings, and the package can't know what those are. The same
goes for the rest of the C0 range.

Maintainer signed off on documenting this in charmbracelet#644 and suggested showing
how to pre-expand tabs with strings.ReplaceAll, so do that on
StringWidth and point StringWidthWc at the same caveat.

Closes charmbracelet#644

Signed-off-by: Charlie Tonneslan <cst0520@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug: x/ansi incorrect calculations of \t

1 participant