Width

package
v0.0.0-...-a39e8fc Latest Latest
Warning

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

Go to latest
Published: Sep 7, 2022 License: GPL-3.0 Imports: 4 Imported by: 0

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 FillLeft

func FillLeft(s string, w int) string

FillLeft return string filled in left by spaces in w cells

func FillRight

func FillRight(s string, w int) string

FillRight return string filled in left by spaces in w cells

func GraphemeClusterCount

func GraphemeClusterCount(s string) (n int)

GraphemeClusterCount returns the number of user-perceived characters (grapheme clusters) for the given string. To calculate this number, it iterates through the string using the Graphemes iterator.

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/

func StringWidth

func StringWidth(s string) (width int)

StringWidth return width as you can see

func Truncate

func Truncate(s string, w int, tail string) string

Truncate return string truncated with w cells

func Wrap

func Wrap(s string, w int) string

Wrap return string wrapped with w cells

Types

type Condition

type Condition struct {
	EastAsianWidth     bool
	StrictEmojiNeutral bool
}

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) FillLeft

func (c *Condition) FillLeft(s string, w int) string

FillLeft return string filled in left by spaces in w cells

func (*Condition) FillRight

func (c *Condition) FillRight(s string, w int) string

FillRight return string filled in left by spaces in w cells

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/

func (*Condition) StringWidth

func (c *Condition) StringWidth(s string) (width int)

StringWidth return width as you can see

func (*Condition) Truncate

func (c *Condition) Truncate(s string, w int, tail string) string

Truncate return string truncated with w cells

func (*Condition) Wrap

func (c *Condition) Wrap(s string, w int) string

Wrap return string wrapped with w cells

type Graphemes

type Graphemes struct {
	// contains filtered or unexported fields
}

Graphemes implements an iterator over Unicode extended grapheme clusters, specified in the Unicode Standard Annex #29. Grapheme clusters correspond to "user-perceived characters". These characters often consist of multiple code points (e.g. the "woman kissing woman" emoji consists of 8 code points: woman + ZWJ + heavy black heart (2 code points) + ZWJ + kiss mark + ZWJ + woman) and the rules described in Annex #29 must be applied to group those code points into clusters perceived by the user as one character.

func NewGraphemes

func NewGraphemes(s string) *Graphemes

NewGraphemes returns a new grapheme cluster iterator.

func (*Graphemes) Bytes

func (g *Graphemes) Bytes() []byte

Bytes returns a byte slice which corresponds to the current grapheme cluster. If the iterator is already past the end or Next() has not yet been called, nil is returned.

func (*Graphemes) Next

func (g *Graphemes) Next() bool

Next advances the iterator by one grapheme cluster and returns false if no clusters are left. This function must be called before the first cluster is accessed.

func (*Graphemes) Positions

func (g *Graphemes) Positions() (int, int)

Positions returns the interval of the current grapheme cluster as byte positions into the original string. The first returned value "from" indexes the first byte and the second returned value "to" indexes the first byte that is not included anymore, i.e. str[from:to] is the current grapheme cluster of the original string "str". If Next() has not yet been called, both values are 0. If the iterator is already past the end, both values are 1.

func (*Graphemes) Reset

func (g *Graphemes) Reset()

Reset puts the iterator into its initial state such that the next call to Next() sets it to the first grapheme cluster again.

func (*Graphemes) Runes

func (g *Graphemes) Runes() []rune

Runes returns a slice of runes (code points) which corresponds to the current grapheme cluster. If the iterator is already past the end or Next() has not yet been called, nil is returned.

func (*Graphemes) Str

func (g *Graphemes) Str() string

Str returns a substring of the original string which corresponds to the current grapheme cluster. If the iterator is already past the end or Next() has not yet been called, an empty string is returned.

Jump to

Keyboard shortcuts

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