-
Notifications
You must be signed in to change notification settings - Fork 2
/
format.go
167 lines (137 loc) · 3.29 KB
/
format.go
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package midterm
import (
"sort"
"strings"
"github.com/muesli/termenv"
)
// Constants for property bit positions
const (
ResetBit uint8 = 1 << iota
BoldBit
FaintBit
ItalicBit
UnderlineBit
BlinkBit
ReverseBit
ConcealBit
// not worth the memory footprint
// CrossOutBit
// OverlineBit
)
// Format represents the text formatting options.
type Format struct {
// Fg and Bg are the foreground and background colors.
Fg, Bg termenv.Color
// Properties packed into a single byte.
Properties uint8
}
// Helper methods to set properties
func (f *Format) SetReset(value bool) {
f.setProperty(ResetBit, value)
}
func (f *Format) IsReset() bool {
return f.hasProperty(ResetBit)
}
func (f *Format) SetBold(value bool) {
f.setProperty(BoldBit, value)
}
func (f *Format) IsBold() bool {
return f.hasProperty(BoldBit)
}
func (f *Format) SetFaint(value bool) {
f.setProperty(FaintBit, value)
}
func (f *Format) IsFaint() bool {
return f.hasProperty(FaintBit)
}
func (f *Format) SetItalic(value bool) {
f.setProperty(ItalicBit, value)
}
func (f *Format) IsItalic() bool {
return f.hasProperty(ItalicBit)
}
func (f *Format) SetUnderline(value bool) {
f.setProperty(UnderlineBit, value)
}
func (f *Format) IsUnderline() bool {
return f.hasProperty(UnderlineBit)
}
func (f *Format) SetBlink(value bool) {
f.setProperty(BlinkBit, value)
}
func (f *Format) IsBlink() bool {
return f.hasProperty(BlinkBit)
}
func (f *Format) SetReverse(value bool) {
f.setProperty(ReverseBit, value)
}
func (f *Format) IsReverse() bool {
return f.hasProperty(ReverseBit)
}
func (f *Format) SetConceal(value bool) {
f.setProperty(ConcealBit, value)
}
func (f *Format) IsConceal() bool {
return f.hasProperty(ConcealBit)
}
// func (f *Format) SetCrossOut(value bool) {
// f.setProperty(CrossOutBit, value)
// }
//
// func (f *Format) IsCrossOut() bool {
// return f.hasProperty(CrossOutBit)
// }
//
// func (f *Format) SetOverline(value bool) {
// f.setProperty(OverlineBit, value)
// }
//
// func (f *Format) IsOverline() bool {
// return f.hasProperty(OverlineBit)
// }
// Helper method to set a property bit
func (f *Format) setProperty(bit uint8, value bool) {
if value {
f.Properties |= bit
} else {
f.Properties &^= bit
}
}
// Helper method to check if a property bit is set
func (f *Format) hasProperty(bit uint8) bool {
return f.Properties&bit != 0
}
func toCss(c termenv.Color) string {
return termenv.ConvertToRGB(c).Hex()
}
func (f Format) css() string {
parts := make([]string, 0)
fg, bg := f.Fg, f.Bg
if f.IsReverse() {
bg, fg = fg, bg
}
parts = append(parts, "color:"+toCss(fg))
parts = append(parts, "background-color:"+toCss(bg))
if f.IsBold() {
parts = append(parts, "font-weight:bold")
}
if f.IsFaint() {
parts = append(parts, "opacity:0.33")
}
if f.IsUnderline() {
parts = append(parts, "text-decoration:underline")
}
if f.IsConceal() {
parts = append(parts, "display:none")
}
if f.IsBlink() {
parts = append(parts, "text-decoration:blink")
}
// We're not in performance sensitive code. Although this sort
// isn't strictly necessary, it gives us the nice property that
// the style of a particular set of attributes will always be
// generated the same way. As a result, we can use the html
// output in tests.
sort.StringSlice(parts).Sort()
return strings.Join(parts, ";")
}