charwidth

package
v1.14.0 Latest Latest
Warning

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

Go to latest
Published: Aug 25, 2026 License: MIT Imports: 1 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package charwidth answers the terminal display width of Unicode codepoints and grapheme clusters. It is the single source of truth shared by the SGR-aware ansi package and the plain text-editing primitives, and it is the Go port of Ruby's lib/char_width.rb.

Deliberately dependency-free apart from grapheme segmentation: no terminal, renderer, or SGR knowledge. Given a bare rune or grapheme it answers 0, 1, or 2 cells.

The tables below are transcribed from the Ruby kernel rather than taken from a general width library, because the Ruby answer is the oracle the rest of the port must agree with. In particular text-presentation symbols (⚠ U+26A0, ✓ U+2713) stay one cell, which is how terminals actually render them without a U+FE0F selector.

Index

Constants

View Source
const VariationSelector = 0xFE0F

VariationSelector is U+FE0F, the emoji presentation selector.

Variables

This section is empty.

Functions

func Cluster

func Cluster(gc string) int

Cluster reports the display width of one grapheme cluster. A U+FE0F selector forces emoji (wide) presentation; otherwise the first visible codepoint sets the width and trailing combining marks add nothing.

func Clusters

func Clusters(s string) []string

Clusters splits s into UAX #29 extended grapheme clusters, the same units Ruby's String#each_grapheme_cluster yields.

func FirstCluster

func FirstCluster(s string) (cluster, rest string)

FirstCluster returns the leading grapheme cluster of s and the remainder.

func Rune

func Rune(cp rune) int

Rune reports the display width of a single codepoint: 0 (combining or zero-width), 2 (wide or emoji) or 1 (everything else). Printable ASCII is by far the hottest input, so it returns before touching the range tables.

func String

func String(s string) int

String is the display width of a plain (escape-free) string in cells.

Types

This section is empty.

Jump to

Keyboard shortcuts

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