mirror of
https://gitea.com/gitea/tea.git
synced 2024-12-04 14:46:40 -05:00
89e93d90b3
Merge branch 'master' into use-glamour select Glamour Theme based on BackgroundColor Merge branch 'master' into use-glamour Merge branch 'master' into use-glamour update termev update go.mod label color colorate use glamour for issue content Vendor: Add glamour Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com> Co-authored-by: 6543 <6543@obermui.de> Reviewed-on: https://gitea.com/gitea/tea/pulls/181 Reviewed-by: techknowlogick <techknowlogick@gitea.io> Reviewed-by: Lunny Xiao <xiaolunwen@gmail.com>
66 lines
1.2 KiB
Go
66 lines
1.2 KiB
Go
package ansi
|
|
|
|
import (
|
|
"io"
|
|
"strings"
|
|
)
|
|
|
|
type Writer struct {
|
|
Forward io.Writer
|
|
|
|
ansi bool
|
|
ansiseq string
|
|
lastseq string
|
|
seqchanged bool
|
|
}
|
|
|
|
// Write is used to write content to the ANSI buffer.
|
|
func (w *Writer) Write(b []byte) (int, error) {
|
|
for _, c := range string(b) {
|
|
if c == '\x1B' {
|
|
// ANSI escape sequence
|
|
w.ansi = true
|
|
w.seqchanged = true
|
|
w.ansiseq += string(c)
|
|
} else if w.ansi {
|
|
w.ansiseq += string(c)
|
|
if (c >= 0x41 && c <= 0x5a) || (c >= 0x61 && c <= 0x7a) {
|
|
// ANSI sequence terminated
|
|
w.ansi = false
|
|
|
|
_, _ = w.Forward.Write([]byte(w.ansiseq))
|
|
if strings.HasSuffix(w.ansiseq, "[0m") {
|
|
// reset sequence
|
|
w.lastseq = ""
|
|
} else if strings.HasSuffix(w.ansiseq, "m") {
|
|
// color code
|
|
w.lastseq = w.ansiseq
|
|
}
|
|
w.ansiseq = ""
|
|
}
|
|
} else {
|
|
_, err := w.Forward.Write([]byte(string(c)))
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
}
|
|
}
|
|
|
|
return len(b), nil
|
|
}
|
|
|
|
func (w *Writer) LastSequence() string {
|
|
return w.lastseq
|
|
}
|
|
|
|
func (w *Writer) ResetAnsi() {
|
|
if !w.seqchanged {
|
|
return
|
|
}
|
|
_, _ = w.Forward.Write([]byte("\x1b[0m"))
|
|
}
|
|
|
|
func (w *Writer) RestoreAnsi() {
|
|
_, _ = w.Forward.Write([]byte(w.lastseq))
|
|
}
|