twwidth

package
v1.0.8 Latest Latest
Warning

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

Go to latest
Published: Jul 4, 2025 License: MIT Imports: 5 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Display

func Display(cond *runewidth.Condition, str string) int

Display calculates the visual width of a string, excluding ANSI escape sequences, using the provided runewidth condition. Unlike Width, it does not use caching and is intended for cases where a specific condition is required. This function is thread-safe with respect to the provided condition.

Example:

cond := runewidth.NewCondition()
width := twdw.Display(cond, "Hello\x1b[31mWorld") // Returns 10

func Filter

func Filter() *regexp.Regexp

Filter compiles and returns a regular expression for matching ANSI escape sequences, including CSI (Control Sequence Introducer) and OSC (Operating System Command) sequences. The returned regex can be used to strip ANSI codes from strings.

func SetCondition

func SetCondition(newCond *runewidth.Condition)

SetCondition updates the global runewidth.Condition used for width calculations. When the condition is changed, the width cache is cleared. This function is thread-safe.

Example:

newCond := runewidth.NewCondition()
newCond.EastAsianWidth = true
twdw.SetCondition(newCond)

func SetEastAsian

func SetEastAsian(enable bool)

SetEastAsian enables or disables East Asian width handling for width calculations. When the setting changes, the width cache is cleared to ensure accuracy. This function is thread-safe.

Example:

twdw.SetEastAsian(true) // Enable East Asian width handling

func Truncate

func Truncate(s string, maxWidth int, suffix ...string) string

Truncate shortens a string to fit within a specified visual width, optionally appending a suffix (e.g., "..."). It preserves ANSI escape sequences and adds a reset sequence (\x1b[0m) if needed to prevent formatting bleed. The function respects the global East Asian width setting and is thread-safe.

If maxWidth is negative, an empty string is returned. If maxWidth is zero and a suffix is provided, the suffix is returned. If the string's visual width is less than or equal to maxWidth, the string (and suffix, if provided and fits) is returned unchanged.

Example:

s := twdw.Truncate("Hello\x1b[31mWorld", 5, "...") // Returns "Hello..."
s = twdw.Truncate("Hello", 10) // Returns "Hello"

func Width

func Width(str string) int

Width calculates the visual width of a string, excluding ANSI escape sequences, using the go-runewidth package for accurate Unicode handling. It accounts for the current East Asian width setting and caches results for performance. This function is thread-safe.

Example:

width := twdw.Width("Hello\x1b[31mWorld") // Returns 10

func WidthNoCache

func WidthNoCache(str string) int

WidthNoCache calculates the visual width of a string without using or updating the global cache. It uses the current global East Asian width setting. This function is intended for internal use (e.g., benchmarking) and is thread-safe.

Example:

width := twdw.WidthNoCache("Hello\x1b[31mWorld") // Returns 10

Types

This section is empty.

Jump to

Keyboard shortcuts

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