gitea-tea/vendor/github.com/muesli/termenv/style.go
6543 89e93d90b3 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>
2020-09-19 16:00:50 +00:00

121 lines
2.2 KiB
Go

package termenv
import (
"fmt"
"strings"
"github.com/mattn/go-runewidth"
)
const (
ResetSeq = "0"
BoldSeq = "1"
FaintSeq = "2"
ItalicSeq = "3"
UnderlineSeq = "4"
BlinkSeq = "5"
ReverseSeq = "7"
CrossOutSeq = "9"
OverlineSeq = "53"
)
// Style is a string that various rendering styles can be applied to.
type Style struct {
string
styles []string
}
// String returns a new Style.
func String(s ...string) Style {
return Style{
string: strings.Join(s, " "),
}
}
func (t Style) String() string {
return t.Styled(t.string)
}
// Styled renders s with all applied styles.
func (t Style) Styled(s string) string {
if len(t.styles) == 0 {
return s
}
seq := strings.Join(t.styles, ";")
if seq == "" {
return s
}
return fmt.Sprintf("%s%sm%s%sm", CSI, seq, s, CSI+ResetSeq)
}
// Foreground sets a foreground color.
func (t Style) Foreground(c Color) Style {
if c != nil {
t.styles = append(t.styles, c.Sequence(false))
}
return t
}
// Background sets a background color.
func (t Style) Background(c Color) Style {
if c != nil {
t.styles = append(t.styles, c.Sequence(true))
}
return t
}
// Bold enables bold rendering.
func (t Style) Bold() Style {
t.styles = append(t.styles, BoldSeq)
return t
}
// Faint enables faint rendering.
func (t Style) Faint() Style {
t.styles = append(t.styles, FaintSeq)
return t
}
// Italic enables italic rendering.
func (t Style) Italic() Style {
t.styles = append(t.styles, ItalicSeq)
return t
}
// Underline enables underline rendering.
func (t Style) Underline() Style {
t.styles = append(t.styles, UnderlineSeq)
return t
}
// Overline enables overline rendering.
func (t Style) Overline() Style {
t.styles = append(t.styles, OverlineSeq)
return t
}
// Blink enables blink mode.
func (t Style) Blink() Style {
t.styles = append(t.styles, BlinkSeq)
return t
}
// Reverse enables reverse color mode.
func (t Style) Reverse() Style {
t.styles = append(t.styles, ReverseSeq)
return t
}
// CrossOut enables crossed-out rendering.
func (t Style) CrossOut() Style {
t.styles = append(t.styles, CrossOutSeq)
return t
}
// Width returns the width required to print all runes in Style.
func (t Style) Width() int {
return runewidth.StringWidth(t.string)
}