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, ControlSequences: false}
DefaultOptions is the default options for the display width calculation, which is EastAsianWidth false and ControlSequences 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 specifies whether to treat ambiguous East Asian characters
// as width 1 or 2. When false (default), ambiguous East Asian characters
// are treated as width 1. When true, they are width 2.
EastAsianWidth bool
// ControlSequences specifies whether to ignore ECMA-48 escape sequences
// when calculating the display width. When false (default), ANSI escape
// sequences are treated as just a series of characters. When true, they are
// treated as a single zero-width unit.
//
// Note that this option is about *sequences*. Individual control characters
// are already treated as zero-width. With this option, ANSI sequences such as
// "\x1b[31m" and "\x1b[0m" do not count towards the width of a string.
ControlSequences bool
}
Options allows you to specify the treatment of ambiguous East Asian characters and ANSI escape sequences.
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 visible width, including the width of the tail, is less than or equal to maxWidth.
When [Options.ControlSequences] is true, ANSI escape sequences that appear after the truncation point are preserved in the output. This ensures that escape sequences such as SGR resets are not lost, preventing color bleed in terminal output.
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 visible width, including the width of the tail, is less than or equal to maxWidth.
When [Options.ControlSequences] is true, ANSI escape sequences that appear after the truncation point are preserved in the output. This ensures that escape sequences such as SGR resets are not lost, preventing color bleed in terminal output.