| 1 |
package displaywidth |
| 2 |
|
| 3 |
import ( |
| 4 |
"github.com/clipperhouse/uax29/v2/graphemes" |
| 5 |
) |
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
type Graphemes[T ~string | []byte] struct { |
| 12 |
iter *graphemes.Iterator[T] |
| 13 |
options Options |
| 14 |
} |
| 15 |
|
| 16 |
|
| 17 |
func (g *Graphemes[T]) Next() bool { |
| 18 |
return g.iter.Next() |
| 19 |
} |
| 20 |
|
| 21 |
|
| 22 |
func (g *Graphemes[T]) Value() T { |
| 23 |
return g.iter.Value() |
| 24 |
} |
| 25 |
|
| 26 |
|
| 27 |
func (g *Graphemes[T]) Width() int { |
| 28 |
return graphemeWidth(g.Value(), g.options) |
| 29 |
} |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
func StringGraphemes(s string) Graphemes[string] { |
| 37 |
return DefaultOptions.StringGraphemes(s) |
| 38 |
} |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 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 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
func BytesGraphemes(s []byte) Graphemes[[]byte] { |
| 59 |
return DefaultOptions.BytesGraphemes(s) |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 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 |
} |