Use glamour and termev to render/colorize content (#181)
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>
This commit is contained in:
33
vendor/github.com/muesli/reflow/ansi/buffer.go
generated
vendored
Normal file
33
vendor/github.com/muesli/reflow/ansi/buffer.go
generated
vendored
Normal file
@@ -0,0 +1,33 @@
|
||||
package ansi
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
|
||||
"github.com/mattn/go-runewidth"
|
||||
)
|
||||
|
||||
// Buffer is a buffer aware of ANSI escape sequences.
|
||||
type Buffer struct {
|
||||
bytes.Buffer
|
||||
}
|
||||
|
||||
// PrintableRuneCount returns the amount of printable runes in the buffer.
|
||||
func (w Buffer) PrintableRuneCount() int {
|
||||
var n int
|
||||
var ansi bool
|
||||
for _, c := range w.String() {
|
||||
if c == '\x1B' {
|
||||
// ANSI escape sequence
|
||||
ansi = true
|
||||
} else if ansi {
|
||||
if (c >= 0x40 && c <= 0x5a) || (c >= 0x61 && c <= 0x7a) {
|
||||
// ANSI sequence terminated
|
||||
ansi = false
|
||||
}
|
||||
} else {
|
||||
n += runewidth.StringWidth(string(c))
|
||||
}
|
||||
}
|
||||
|
||||
return n
|
||||
}
|
||||
65
vendor/github.com/muesli/reflow/ansi/writer.go
generated
vendored
Normal file
65
vendor/github.com/muesli/reflow/ansi/writer.go
generated
vendored
Normal file
@@ -0,0 +1,65 @@
|
||||
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))
|
||||
}
|
||||
Reference in New Issue
Block a user