Debian Codegraph @

buffer.go

raw
1 package ansi
2
3 import (
4 "bytes"
5
6 "github.com/mattn/go-runewidth"
7 )
8
9 // Buffer is a buffer aware of ANSI escape sequences.
10 type Buffer struct {
11 bytes.Buffer
12 }
13
14 // PrintableRuneWidth returns the cell width of all printable runes in the
15 // buffer.
16 func (w Buffer) PrintableRuneWidth() int {
17 return PrintableRuneWidth(w.String())
18 }
19
20 // PrintableRuneWidth returns the cell width of the given string.
21 func PrintableRuneWidth(s string) int {
22 var n int
23 var ansi bool
24
25 for _, c := range s {
26 if c == Marker {
27 // ANSI escape sequence
28 ansi = true
29 } else if ansi {
30 if IsTerminator(c) {
31 // ANSI sequence terminated
32 ansi = false
33 }
34 } else {
35 n += runewidth.RuneWidth(c)
36 }
37 }
38
39 return n
40 }