Documentation
¶
Index ¶
- func Display(cond *runewidth.Condition, str string) int
- func Filter() *regexp.Regexp
- func IsEastAsian() bool
- func SetCondition(cond *runewidth.Condition)
- func SetEastAsian(enable bool)
- func Truncate(s string, maxWidth int, suffix ...string) string
- func Width(str string) int
- func WidthNoCache(str string) int
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Display ¶
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 ¶
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 IsEastAsian ¶ added in v1.1.1
func IsEastAsian() bool
IsEastAsian returns the current East Asian width setting. This function is thread-safe.
Example:
if twdw.IsEastAsian() {
// Handle East Asian width characters
}
func SetCondition ¶
SetCondition updates the global runewidth.Condition used for width calculations. This method is kept for backward compatibility. The condition is converted to displaywidth.Options internally for better performance.
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 ¶
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 ¶
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 ¶
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.