textwidth

package
v0.3.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 19, 2026 License: MIT Imports: 1 Imported by: 0

Documentation

Overview

Package textwidth measures how many terminal columns a string occupies.

The diagram engine sized every box, centered every label and placed every edge caption with len(s) — the byte count. That is the display width only for ASCII. A label carrying an emoji measured four columns per glyph and drew two, so its box came out too wide and its text sat off-center; CJK had the opposite problem, measuring one column per glyph and drawing two, so the text overran its border.

The rules implemented here are the ones that matter for diagram labels: combining marks and zero-width joiners take no space, East Asian Wide and Fullwidth characters and emoji take two columns, and everything else takes one. This is a deliberate subset of UAX #11 rather than the whole table — enough to place a label correctly, and small enough that this package keeps its property of depending on nothing outside the standard library.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Max

func Max(lines []string) int

Max returns the width of the widest line in lines.

func Rune

func Rune(r rune) int

Rune returns the number of terminal columns r occupies: 0, 1 or 2.

func String

func String(s string) int

String returns the number of terminal columns s occupies.

Types

type Cell

type Cell struct {
	Rune  rune
	Width int
}

Cell is one rune together with the columns it occupies.

func Split

func Split(s string) []Cell

Split walks s and reports each rune with the width it will actually render at, resolving the one thing a per-rune answer cannot: a variation selector.

U+FE0F asks for emoji presentation, which makes an otherwise narrow symbol render two columns wide. It carries no width of its own, so measuring rune by rune misses it — "🛠️" is two columns and "🛠" is one, and the only difference between them is the selector that follows.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL