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:
21
vendor/github.com/muesli/reflow/LICENSE
generated
vendored
Normal file
21
vendor/github.com/muesli/reflow/LICENSE
generated
vendored
Normal file
@@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2019 Christian Muehlhaeuser
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
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))
|
||||
}
|
||||
111
vendor/github.com/muesli/reflow/indent/indent.go
generated
vendored
Normal file
111
vendor/github.com/muesli/reflow/indent/indent.go
generated
vendored
Normal file
@@ -0,0 +1,111 @@
|
||||
package indent
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"io"
|
||||
"strings"
|
||||
|
||||
"github.com/muesli/reflow/ansi"
|
||||
)
|
||||
|
||||
type IndentFunc func(w io.Writer)
|
||||
|
||||
type Writer struct {
|
||||
Indent uint
|
||||
IndentFunc IndentFunc
|
||||
|
||||
ansiWriter *ansi.Writer
|
||||
buf bytes.Buffer
|
||||
skipIndent bool
|
||||
ansi bool
|
||||
}
|
||||
|
||||
func NewWriter(indent uint, indentFunc IndentFunc) *Writer {
|
||||
w := &Writer{
|
||||
Indent: indent,
|
||||
IndentFunc: indentFunc,
|
||||
}
|
||||
w.ansiWriter = &ansi.Writer{
|
||||
Forward: &w.buf,
|
||||
}
|
||||
return w
|
||||
}
|
||||
|
||||
func NewWriterPipe(forward io.Writer, indent uint, indentFunc IndentFunc) *Writer {
|
||||
return &Writer{
|
||||
Indent: indent,
|
||||
IndentFunc: indentFunc,
|
||||
ansiWriter: &ansi.Writer{
|
||||
Forward: forward,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// Bytes is shorthand for declaring a new default indent-writer instance,
|
||||
// used to immediately indent a byte slice.
|
||||
func Bytes(b []byte, indent uint) []byte {
|
||||
f := NewWriter(indent, nil)
|
||||
_, _ = f.Write(b)
|
||||
|
||||
return f.Bytes()
|
||||
}
|
||||
|
||||
// String is shorthand for declaring a new default indent-writer instance,
|
||||
// used to immediately indent a string.
|
||||
func String(s string, indent uint) string {
|
||||
return string(Bytes([]byte(s), indent))
|
||||
}
|
||||
|
||||
// Write is used to write content to the indent buffer.
|
||||
func (w *Writer) Write(b []byte) (int, error) {
|
||||
for _, c := range string(b) {
|
||||
if c == '\x1B' {
|
||||
// ANSI escape sequence
|
||||
w.ansi = true
|
||||
} else if w.ansi {
|
||||
if (c >= 0x41 && c <= 0x5a) || (c >= 0x61 && c <= 0x7a) {
|
||||
// ANSI sequence terminated
|
||||
w.ansi = false
|
||||
}
|
||||
} else {
|
||||
if !w.skipIndent {
|
||||
w.ansiWriter.ResetAnsi()
|
||||
if w.IndentFunc != nil {
|
||||
for i := 0; i < int(w.Indent); i++ {
|
||||
w.IndentFunc(w.ansiWriter)
|
||||
}
|
||||
} else {
|
||||
_, err := w.ansiWriter.Write([]byte(strings.Repeat(" ", int(w.Indent))))
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
}
|
||||
|
||||
w.skipIndent = true
|
||||
w.ansiWriter.RestoreAnsi()
|
||||
}
|
||||
|
||||
if c == '\n' {
|
||||
// end of current line
|
||||
w.skipIndent = false
|
||||
}
|
||||
}
|
||||
|
||||
_, err := w.ansiWriter.Write([]byte(string(c)))
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
}
|
||||
|
||||
return len(b), nil
|
||||
}
|
||||
|
||||
// Bytes returns the indented result as a byte slice.
|
||||
func (w *Writer) Bytes() []byte {
|
||||
return w.buf.Bytes()
|
||||
}
|
||||
|
||||
// String returns the indented result as a string.
|
||||
func (w *Writer) String() string {
|
||||
return w.buf.String()
|
||||
}
|
||||
132
vendor/github.com/muesli/reflow/padding/padding.go
generated
vendored
Normal file
132
vendor/github.com/muesli/reflow/padding/padding.go
generated
vendored
Normal file
@@ -0,0 +1,132 @@
|
||||
package padding
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"io"
|
||||
"strings"
|
||||
|
||||
"github.com/mattn/go-runewidth"
|
||||
|
||||
"github.com/muesli/reflow/ansi"
|
||||
)
|
||||
|
||||
type PaddingFunc func(w io.Writer)
|
||||
|
||||
type Writer struct {
|
||||
Padding uint
|
||||
PadFunc PaddingFunc
|
||||
|
||||
ansiWriter *ansi.Writer
|
||||
buf bytes.Buffer
|
||||
lineLen int
|
||||
ansi bool
|
||||
}
|
||||
|
||||
func NewWriter(width uint, paddingFunc PaddingFunc) *Writer {
|
||||
w := &Writer{
|
||||
Padding: width,
|
||||
PadFunc: paddingFunc,
|
||||
}
|
||||
w.ansiWriter = &ansi.Writer{
|
||||
Forward: &w.buf,
|
||||
}
|
||||
return w
|
||||
}
|
||||
|
||||
func NewWriterPipe(forward io.Writer, width uint, paddingFunc PaddingFunc) *Writer {
|
||||
return &Writer{
|
||||
Padding: width,
|
||||
PadFunc: paddingFunc,
|
||||
ansiWriter: &ansi.Writer{
|
||||
Forward: forward,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// Bytes is shorthand for declaring a new default padding-writer instance,
|
||||
// used to immediately pad a byte slice.
|
||||
func Bytes(b []byte, width uint) []byte {
|
||||
f := NewWriter(width, nil)
|
||||
_, _ = f.Write(b)
|
||||
f.Close()
|
||||
|
||||
return f.Bytes()
|
||||
}
|
||||
|
||||
// String is shorthand for declaring a new default padding-writer instance,
|
||||
// used to immediately pad a string.
|
||||
func String(s string, width uint) string {
|
||||
return string(Bytes([]byte(s), width))
|
||||
}
|
||||
|
||||
// Write is used to write content to the padding buffer.
|
||||
func (w *Writer) Write(b []byte) (int, error) {
|
||||
for _, c := range string(b) {
|
||||
if c == '\x1B' {
|
||||
// ANSI escape sequence
|
||||
w.ansi = true
|
||||
} else if w.ansi {
|
||||
if (c >= 0x41 && c <= 0x5a) || (c >= 0x61 && c <= 0x7a) {
|
||||
// ANSI sequence terminated
|
||||
w.ansi = false
|
||||
}
|
||||
} else {
|
||||
w.lineLen += runewidth.StringWidth(string(c))
|
||||
|
||||
if c == '\n' {
|
||||
// end of current line
|
||||
err := w.pad()
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
w.ansiWriter.ResetAnsi()
|
||||
w.lineLen = 0
|
||||
}
|
||||
}
|
||||
|
||||
_, err := w.ansiWriter.Write([]byte(string(c)))
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
}
|
||||
|
||||
return len(b), nil
|
||||
}
|
||||
|
||||
func (w *Writer) pad() error {
|
||||
if w.Padding > 0 && uint(w.lineLen) < w.Padding {
|
||||
if w.PadFunc != nil {
|
||||
for i := 0; i < int(w.Padding)-w.lineLen; i++ {
|
||||
w.PadFunc(w.ansiWriter)
|
||||
}
|
||||
} else {
|
||||
_, err := w.ansiWriter.Write([]byte(strings.Repeat(" ", int(w.Padding)-w.lineLen)))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Close will finish the padding operation. Always call it before trying to
|
||||
// retrieve the final result.
|
||||
func (w *Writer) Close() error {
|
||||
// don't pad empty trailing lines
|
||||
if w.lineLen == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
return w.pad()
|
||||
}
|
||||
|
||||
// Bytes returns the padded result as a byte slice.
|
||||
func (w *Writer) Bytes() []byte {
|
||||
return w.buf.Bytes()
|
||||
}
|
||||
|
||||
// String returns the padded result as a string.
|
||||
func (w *Writer) String() string {
|
||||
return w.buf.String()
|
||||
}
|
||||
167
vendor/github.com/muesli/reflow/wordwrap/wordwrap.go
generated
vendored
Normal file
167
vendor/github.com/muesli/reflow/wordwrap/wordwrap.go
generated
vendored
Normal file
@@ -0,0 +1,167 @@
|
||||
package wordwrap
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"strings"
|
||||
"unicode"
|
||||
|
||||
"github.com/muesli/reflow/ansi"
|
||||
)
|
||||
|
||||
var (
|
||||
defaultBreakpoints = []rune{'-'}
|
||||
defaultNewline = []rune{'\n'}
|
||||
)
|
||||
|
||||
// WordWrap contains settings and state for customisable text reflowing with
|
||||
// support for ANSI escape sequences. This means you can style your terminal
|
||||
// output without affecting the word wrapping algorithm.
|
||||
type WordWrap struct {
|
||||
Limit int
|
||||
Breakpoints []rune
|
||||
Newline []rune
|
||||
KeepNewlines bool
|
||||
|
||||
buf bytes.Buffer
|
||||
space bytes.Buffer
|
||||
word ansi.Buffer
|
||||
|
||||
lineLen int
|
||||
ansi bool
|
||||
}
|
||||
|
||||
// NewWriter returns a new instance of a word-wrapping writer, initialized with
|
||||
// default settings.
|
||||
func NewWriter(limit int) *WordWrap {
|
||||
return &WordWrap{
|
||||
Limit: limit,
|
||||
Breakpoints: defaultBreakpoints,
|
||||
Newline: defaultNewline,
|
||||
KeepNewlines: true,
|
||||
}
|
||||
}
|
||||
|
||||
// Bytes is shorthand for declaring a new default WordWrap instance,
|
||||
// used to immediately word-wrap a byte slice.
|
||||
func Bytes(b []byte, limit int) []byte {
|
||||
f := NewWriter(limit)
|
||||
_, _ = f.Write(b)
|
||||
f.Close()
|
||||
|
||||
return f.Bytes()
|
||||
}
|
||||
|
||||
// String is shorthand for declaring a new default WordWrap instance,
|
||||
// used to immediately word-wrap a string.
|
||||
func String(s string, limit int) string {
|
||||
return string(Bytes([]byte(s), limit))
|
||||
}
|
||||
|
||||
func (w *WordWrap) addSpace() {
|
||||
w.lineLen += w.space.Len()
|
||||
w.buf.Write(w.space.Bytes())
|
||||
w.space.Reset()
|
||||
}
|
||||
|
||||
func (w *WordWrap) addWord() {
|
||||
if w.word.Len() > 0 {
|
||||
w.addSpace()
|
||||
w.lineLen += w.word.PrintableRuneCount()
|
||||
w.buf.Write(w.word.Bytes())
|
||||
w.word.Reset()
|
||||
}
|
||||
}
|
||||
|
||||
func (w *WordWrap) addNewLine() {
|
||||
w.buf.WriteRune('\n')
|
||||
w.lineLen = 0
|
||||
w.space.Reset()
|
||||
}
|
||||
|
||||
func inGroup(a []rune, c rune) bool {
|
||||
for _, v := range a {
|
||||
if v == c {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// Write is used to write more content to the word-wrap buffer.
|
||||
func (w *WordWrap) Write(b []byte) (int, error) {
|
||||
if w.Limit == 0 {
|
||||
return w.buf.Write(b)
|
||||
}
|
||||
|
||||
s := string(b)
|
||||
if !w.KeepNewlines {
|
||||
s = strings.Replace(strings.TrimSpace(s), "\n", " ", -1)
|
||||
}
|
||||
|
||||
for _, c := range s {
|
||||
if c == '\x1B' {
|
||||
// ANSI escape sequence
|
||||
w.word.WriteRune(c)
|
||||
w.ansi = true
|
||||
} else if w.ansi {
|
||||
w.word.WriteRune(c)
|
||||
if (c >= 0x40 && c <= 0x5a) || (c >= 0x61 && c <= 0x7a) {
|
||||
// ANSI sequence terminated
|
||||
w.ansi = false
|
||||
}
|
||||
} else if inGroup(w.Newline, c) {
|
||||
// end of current line
|
||||
// see if we can add the content of the space buffer to the current line
|
||||
if w.word.Len() == 0 {
|
||||
if w.lineLen+w.space.Len() > w.Limit {
|
||||
w.lineLen = 0
|
||||
} else {
|
||||
// preserve whitespace
|
||||
w.buf.Write(w.space.Bytes())
|
||||
}
|
||||
w.space.Reset()
|
||||
}
|
||||
|
||||
w.addWord()
|
||||
w.addNewLine()
|
||||
} else if unicode.IsSpace(c) {
|
||||
// end of current word
|
||||
w.addWord()
|
||||
w.space.WriteRune(c)
|
||||
} else if inGroup(w.Breakpoints, c) {
|
||||
// valid breakpoint
|
||||
w.addSpace()
|
||||
w.addWord()
|
||||
w.buf.WriteRune(c)
|
||||
} else {
|
||||
// any other character
|
||||
w.word.WriteRune(c)
|
||||
|
||||
// add a line break if the current word would exceed the line's
|
||||
// character limit
|
||||
if w.lineLen+w.space.Len()+w.word.PrintableRuneCount() > w.Limit &&
|
||||
w.word.PrintableRuneCount() < w.Limit {
|
||||
w.addNewLine()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return len(b), nil
|
||||
}
|
||||
|
||||
// Close will finish the word-wrap operation. Always call it before trying to
|
||||
// retrieve the final result.
|
||||
func (w *WordWrap) Close() error {
|
||||
w.addWord()
|
||||
return nil
|
||||
}
|
||||
|
||||
// Bytes returns the word-wrapped result as a byte slice.
|
||||
func (w *WordWrap) Bytes() []byte {
|
||||
return w.buf.Bytes()
|
||||
}
|
||||
|
||||
// String returns the word-wrapped result as a string.
|
||||
func (w *WordWrap) String() string {
|
||||
return w.buf.String()
|
||||
}
|
||||
Reference in New Issue
Block a user