1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
|
package style
import (
"github.com/gookit/color"
)
// A TextStyle contains a foreground color, background color, and
// decorations (bold/underline/reverse).
//
// Colors may each be either 16-bit or 24-bit RGB colors. When
// we need to produce a string with a TextStyle, if either foreground or
// background color is RGB, we'll promote the other color component to RGB as well.
// We could simplify this code by forcing everything to be RGB, but we're not
// sure how compatible or efficient that would be with various terminals.
// Lazygit will typically stick to 16-bit colors, but users may configure RGB colors.
//
// TextStyles are value objects, not entities, so for example if you want to
// add the bold decoration to a TextStyle, we'll create a new TextStyle with
// that decoration applied.
//
// Decorations are additive, so when we merge two TextStyles, if either is bold
// then the resulting style will also be bold.
//
// So that we aren't rederiving the underlying style each time we want to print
// a string, we derive it when a new TextStyle is created and store it in the
// `style` field.
type TextStyle struct {
fg *Color
bg *Color
decoration Decoration
// making this public so that we can use a type switch to get to the underlying
// value so we can cache styles. This is very much a hack.
Style Sprinter
}
type Sprinter interface {
Sprint(a ...interface{}) string
Sprintf(format string, a ...interface{}) string
}
func New() TextStyle {
s := TextStyle{}
s.Style = s.deriveStyle()
return s
}
func (b TextStyle) Sprint(a ...interface{}) string {
return b.Style.Sprint(a...)
}
func (b TextStyle) Sprintf(format string, a ...interface{}) string {
return b.Style.Sprintf(format, a...)
}
// note that our receiver here is not a pointer which means we're receiving a
// copy of the original TextStyle. This allows us to mutate and return that
// TextStyle receiver without actually modifying the original.
func (b TextStyle) SetBold() TextStyle {
b.decoration.SetBold()
b.Style = b.deriveStyle()
return b
}
func (b TextStyle) SetUnderline() TextStyle {
b.decoration.SetUnderline()
b.Style = b.deriveStyle()
return b
}
func (b TextStyle) SetReverse() TextStyle {
b.decoration.SetReverse()
b.Style = b.deriveStyle()
return b
}
func (b TextStyle) SetBg(color Color) TextStyle {
b.bg = &color
b.Style = b.deriveStyle()
return b
}
func (b TextStyle) SetFg(color Color) TextStyle {
b.fg = &color
b.Style = b.deriveStyle()
return b
}
func (b TextStyle) MergeStyle(other TextStyle) TextStyle {
b.decoration = b.decoration.Merge(other.decoration)
if other.fg != nil {
b.fg = other.fg
}
if other.bg != nil {
b.bg = other.bg
}
b.Style = b.deriveStyle()
return b
}
func (b TextStyle) deriveStyle() Sprinter {
if b.fg == nil && b.bg == nil {
return color.Style(b.decoration.ToOpts())
}
isRgb := (b.fg != nil && b.fg.IsRGB()) || (b.bg != nil && b.bg.IsRGB())
if isRgb {
return b.deriveRGBStyle()
}
return b.deriveBasicStyle()
}
func (b TextStyle) deriveBasicStyle() color.Style {
style := make([]color.Color, 0, 5)
if b.fg != nil {
style = append(style, *b.fg.basic)
}
if b.bg != nil {
style = append(style, *b.bg.basic)
}
style = append(style, b.decoration.ToOpts()...)
return color.Style(style)
}
func (b TextStyle) deriveRGBStyle() *color.RGBStyle {
style := &color.RGBStyle{}
if b.fg != nil {
style.SetFg(*b.fg.ToRGB(false).rgb)
}
if b.bg != nil {
// We need to convert the bg firstly to a foreground color,
// For more info see
style.SetBg(*b.bg.ToRGB(true).rgb)
}
style.SetOpts(b.decoration.ToOpts())
return style
}
|