runewidth

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: May 22, 2024 License: MIT Imports: 3 Imported by: 2

README

runewidth provides functions to get fixed width of the character or string.

This is a fork of https://github.com/mattn/go-runewidth, updated to the newest Unicode. It also removes various helper functions, so all that remains is just the runewidth.RuneWidth() function:

runewidth.RuneWidth('a')
runewidth.RuneWidth('つ')
runewidth.RuneWidth('🤷')

Note this can NOT be used to get the width of the string:

// Broken! Do not do this.
l := 0
for _, r := range str {
    l += runewidth.RuneWidth(r)
}

Use https://github.com/arp242/termtext or https://github.com/rivo/uniseg for getting the width of a string.

This is mostly useful in conjunction with the uniseg package, as used in termtext.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// EastAsianWidth will be set true if the current locale is CJK
	EastAsianWidth bool

	// StrictEmojiNeutral should be set false if handle broken fonts
	StrictEmojiNeutral bool = true

	// DefaultCondition is a condition in current locale
	DefaultCondition = &Condition{
		EastAsianWidth:     false,
		StrictEmojiNeutral: true,
	}
)

Functions

func CreateLUT

func CreateLUT()

CreateLUT will create an in-memory lookup table of 557055 bytes for faster operation. This should not be called concurrently with other operations.

func IsAmbiguousWidth

func IsAmbiguousWidth(r rune) bool

IsAmbiguousWidth returns whether is ambiguous width or not.

func IsEastAsian

func IsEastAsian() bool

IsEastAsian return true if the current locale is CJK

func IsNeutralWidth

func IsNeutralWidth(r rune) bool

IsNeutralWidth returns whether is neutral width or not.

func RuneWidth

func RuneWidth(r rune) int

RuneWidth returns the number of cells in r. See http://www.unicode.org/reports/tr11/

Types

type Condition

type Condition struct {
	EastAsianWidth     bool
	StrictEmojiNeutral bool
	// contains filtered or unexported fields
}

Condition have flag EastAsianWidth whether the current locale is CJK or not.

func NewCondition

func NewCondition() *Condition

NewCondition return new instance of Condition which is current locale.

func (*Condition) CreateLUT

func (c *Condition) CreateLUT()

CreateLUT will create an in-memory lookup table of 557056 bytes for faster operation. This should not be called concurrently with other operations on c. If options in c is changed, CreateLUT should be called again.

func (*Condition) RuneWidth

func (c *Condition) RuneWidth(r rune) int

RuneWidth returns the number of cells in r. See http://www.unicode.org/reports/tr11/

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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