Documentation
¶
Index ¶
- Variables
- func Bytes(s []byte) int
- func Rune(r rune) int
- func String(s string) int
- func TruncateBytes(s []byte, maxWidth int, tail []byte) []byte
- func TruncateString(s string, maxWidth int, tail string) string
- type Graphemes
- type Options
- func (options Options) Bytes(s []byte) int
- func (options Options) BytesGraphemes(s []byte) Graphemes[[]byte]
- func (options Options) Rune(r rune) int
- func (options Options) String(s string) int
- func (options Options) StringGraphemes(s string) Graphemes[string]
- func (options Options) TruncateBytes(s []byte, maxWidth int, tail []byte) []byte
- func (options Options) TruncateString(s string, maxWidth int, tail string) string
Constants ¶
This section is empty.
Variables ¶
var DefaultOptions = Options{EastAsianWidth: false}
DefaultOptions is the default options for the display width calculation, which is EastAsianWidth: false.
Functions ¶
func Bytes ¶
Bytes calculates the display width of a []byte, by iterating over grapheme clusters in the byte slice and summing their widths.
func Rune ¶ added in v0.2.0
Rune calculates the display width of a rune. You should almost certainly use String or Bytes for most purposes.
The smallest unit of display width is a grapheme cluster, not a rune. Iterating over runes to measure width is incorrect in many cases.
func String ¶
String calculates the display width of a string, by iterating over grapheme clusters in the string and summing their widths.
func TruncateBytes ¶ added in v0.7.0
TruncateBytes truncates a []byte to the given maxWidth, and appends the given tail if the []byte is truncated.
It ensures the total width, including the width of the tail, is less than or equal to maxWidth.
func TruncateString ¶ added in v0.7.0
TruncateString truncates a string to the given maxWidth, and appends the given tail if the string is truncated.
It ensures the total width, including the width of the tail, is less than or equal to maxWidth.
Types ¶
type Graphemes ¶ added in v0.6.0
Graphemes is an iterator over grapheme clusters.
Iterate using the Next method, and get the width of the current grapheme using the Width method.
func BytesGraphemes ¶ added in v0.6.0
BytesGraphemes returns an iterator over grapheme clusters for the given []byte.
Iterate using the Next method, and get the width of the current grapheme using the Width method.
func StringGraphemes ¶ added in v0.6.0
StringGraphemes returns an iterator over grapheme clusters for the given string.
Iterate using the Next method, and get the width of the current grapheme using the Width method.
func (*Graphemes[T]) Next ¶ added in v0.6.0
Next advances the iterator to the next grapheme cluster.
type Options ¶
type Options struct {
EastAsianWidth bool
}
Options allows you to specify the treatment of ambiguous East Asian characters. When EastAsianWidth is false (default), ambiguous East Asian characters are treated as width 1. When EastAsianWidth is true, ambiguous East Asian characters are treated as width 2.
func (Options) Bytes ¶ added in v0.2.0
Bytes calculates the display width of a []byte, for the given options, by iterating over grapheme clusters in the slice and summing their widths.
func (Options) BytesGraphemes ¶ added in v0.6.0
BytesGraphemes returns an iterator over grapheme clusters for the given []byte, with the given options.
Iterate using the Next method, and get the width of the current grapheme using the Width method.
func (Options) Rune ¶ added in v0.2.0
Rune calculates the display width of a rune, for the given options.
You should almost certainly use String or Bytes for most purposes.
The smallest unit of display width is a grapheme cluster, not a rune. Iterating over runes to measure width is incorrect in many cases.
func (Options) String ¶ added in v0.2.0
String calculates the display width of a string, for the given options, by iterating over grapheme clusters in the string and summing their widths.
func (Options) StringGraphemes ¶ added in v0.6.0
StringGraphemes returns an iterator over grapheme clusters for the given string, with the given options.
Iterate using the Next method, and get the width of the current grapheme using the Width method.
func (Options) TruncateBytes ¶ added in v0.7.0
TruncateBytes truncates a []byte to the given maxWidth, and appends the given tail if the []byte is truncated.
It ensures the total width, including the width of the tail, is less than or equal to maxWidth.
func (Options) TruncateString ¶ added in v0.7.0
TruncateString truncates a string to the given maxWidth, and appends the given tail if the string is truncated.
It ensures the total width, including the width of the tail, is less than or equal to maxWidth.