Debian Codegraph @

graphemes.go

raw symbols SCIP
1 package displaywidth
2
3 import (
4 "github.com/clipperhouse/uax29/v2/graphemes"
5 )
6
7 // Graphemes is an iterator over grapheme clusters.
8 //
9 // Iterate using the Next method, and get the width of the current grapheme
10 // using the Width method.
11 type Graphemes[T ~string | []byte] struct {
12 iter *graphemes.Iterator[T]
13 options Options
14 }
15
16 // Next advances the iterator to the next grapheme cluster.
17 func (g *Graphemes[T]) Next() bool {
18 return g.iter.Next()
19 }
20
21 // Value returns the current grapheme cluster.
22 func (g *Graphemes[T]) Value() T {
23 return g.iter.Value()
24 }
25
26 // Width returns the display width of the current grapheme cluster.
27 func (g *Graphemes[T]) Width() int {
28 return graphemeWidth(g.Value(), g.options)
29 }
30
31 // StringGraphemes returns an iterator over grapheme clusters for the given
32 // string.
33 //
34 // Iterate using the Next method, and get the width of the current grapheme
35 // using the Width method.
36 func StringGraphemes(s string) Graphemes[string] {
37 return DefaultOptions.StringGraphemes(s)
38 }
39
40 // StringGraphemes returns an iterator over grapheme clusters for the given
41 // string, with the given options.
42 //
43 // Iterate using the Next method, and get the width of the current grapheme
44 // using the Width method.
45 func (options Options) StringGraphemes(s string) Graphemes[string] {
46 g := graphemes.FromString(s)
47 g.AnsiEscapeSequences = options.ControlSequences
48 g.AnsiEscapeSequences8Bit = options.ControlSequences8Bit
49
50 return Graphemes[string]{iter: g, options: options}
51 }
52
53 // BytesGraphemes returns an iterator over grapheme clusters for the given
54 // []byte.
55 //
56 // Iterate using the Next method, and get the width of the current grapheme
57 // using the Width method.
58 func BytesGraphemes(s []byte) Graphemes[[]byte] {
59 return DefaultOptions.BytesGraphemes(s)
60 }
61
62 // BytesGraphemes returns an iterator over grapheme clusters for the given
63 // []byte, with the given options.
64 //
65 // Iterate using the Next method, and get the width of the current grapheme
66 // using the Width method.
67 func (options Options) BytesGraphemes(s []byte) Graphemes[[]byte] {
68 g := graphemes.FromBytes(s)
69 g.AnsiEscapeSequences = options.ControlSequences
70 g.AnsiEscapeSequences8Bit = options.ControlSequences8Bit
71
72 return Graphemes[[]byte]{iter: g, options: options}
73 }