36b3af2e3d
PR Check / Code Quality: Format (push) Failing after 1s
PR Check / Code Quality: Lint (darwin) (push) Failing after 0s
PR Check / Code Quality: Lint (freebsd) (push) Failing after 1s
PR Check / Code Quality: Lint (windows) (push) Failing after 1s
PR Check / Code Quality: Lint (linux) (push) Failing after 1s
PR Check / Security: Vulnerability Scan (push) Failing after 0s
Update Documentation / update-docs (push) Failing after 2s
PR Check / Code Quality: Vendor (push) Failing after 1s
PR Check / Code Quality: Coverage (push) Failing after 0s
PR Check / Tests: Unit (macos-latest) (push) Has been cancelled
PR Check / Tests: Unit (ubuntu-24.04) (push) Has been cancelled
PR Check / Tests: Unit (ubuntu-24.04-arm) (push) Has been cancelled
PR Check / Tests: Unit (windows-latest) (push) Has been cancelled
41 lines
708 B
Go
41 lines
708 B
Go
package ansi
|
|
|
|
import (
|
|
"bytes"
|
|
|
|
"github.com/mattn/go-runewidth"
|
|
)
|
|
|
|
// Buffer is a buffer aware of ANSI escape sequences.
|
|
type Buffer struct {
|
|
bytes.Buffer
|
|
}
|
|
|
|
// PrintableRuneWidth returns the cell width of all printable runes in the
|
|
// buffer.
|
|
func (w Buffer) PrintableRuneWidth() int {
|
|
return PrintableRuneWidth(w.String())
|
|
}
|
|
|
|
// PrintableRuneWidth returns the cell width of the given string.
|
|
func PrintableRuneWidth(s string) int {
|
|
var n int
|
|
var ansi bool
|
|
|
|
for _, c := range s {
|
|
if c == Marker {
|
|
// ANSI escape sequence
|
|
ansi = true
|
|
} else if ansi {
|
|
if IsTerminator(c) {
|
|
// ANSI sequence terminated
|
|
ansi = false
|
|
}
|
|
} else {
|
|
n += runewidth.RuneWidth(c)
|
|
}
|
|
}
|
|
|
|
return n
|
|
}
|