-
Notifications
You must be signed in to change notification settings - Fork 1
/
state.go
165 lines (137 loc) · 3.19 KB
/
state.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
package main
import (
"fmt"
"io"
)
// Color is a struct that represents a color
// combination that is used by escape sequences
type Color struct {
typ int // -1: no, 0: index, 1: absolute
color uint32 // RGB color
}
// Equal returns true on euqal color
func (o *Color) Equal(c *Color) bool {
return o.typ == -1 && c.typ == -1 ||
o.typ == c.typ && o.color == c.color
}
// HasColor returns true if it has color
func (o *Color) HasColor() bool {
return o.typ != -1
}
// SetNone sets to no color
func (o *Color) SetNone() {
o.typ = -1
}
// SetRGB sets color to specified color
func (o *Color) SetRGB(r, g, b int) {
o.typ = 1
o.color = uint32((r&0xff)<<16 + (g&0xff)<<8 + (b & 0xff))
}
// SetIndex sets indexed color
func (o *Color) SetIndex(i int) {
o.typ = 0
o.color = uint32(i & 0xff)
}
// String returns the string representation of color
// that can be used in CSS style
func (o *Color) String() string {
if o.typ == 0 {
return Palette[o.color]
} else if o.typ == 1 {
return fmt.Sprintf("#%06x", o.color)
} else {
panic("invalid color")
}
}
// State is the style state machine
type State struct {
bold bool
italic bool
underline bool
blink bool
fgcolor Color
bgcolor Color
}
// Init inits
func (o *State) Init() {
o.bold = false
o.italic = false
o.underline = false
o.blink = false
o.fgcolor.SetNone()
o.bgcolor.SetNone()
}
// Empty returns true if the state is empty
func (o *State) Empty() bool {
return !o.bold && !o.italic && !o.underline && !o.blink &&
!o.fgcolor.HasColor() && !o.bgcolor.HasColor()
}
// AnyChange compares to a backup state
// and returns true if there is at least one change made
func (o *State) AnyChange(s *State) bool {
return o.bold != s.bold ||
o.italic != s.italic ||
o.underline != s.underline ||
o.blink != s.blink ||
!o.fgcolor.Equal(&s.fgcolor) ||
!o.bgcolor.Equal(&s.bgcolor)
}
// AnyClose returns true if at least one attribute has closed
func (o *State) AnyClose(s *State) bool {
if s.bold && !o.bold {
return true
}
if s.italic && !o.italic {
return true
}
if s.underline && !o.underline {
return true
}
if s.blink && !o.blink {
return true
}
if s.fgcolor.HasColor() && !o.fgcolor.HasColor() {
return true
}
if s.bgcolor.HasColor() && !o.bgcolor.HasColor() {
return true
}
return false
}
// WriteStyles outputs the current styles
func (o *State) WriteStyles(w io.Writer, s *State) error {
var err error
f := func(b bool, s string) {
if err == nil && b {
_, err = w.Write([]byte(s))
}
}
f(true, " style=\"")
f(o.bold, "font-weight:bold;")
f(o.italic, "font-style:italic;")
f(o.underline, "text-decoration:underline;")
if o.fgcolor.HasColor() && !o.fgcolor.Equal(&s.fgcolor) {
f(true, "color:"+o.fgcolor.String()+";")
}
if o.bgcolor.HasColor() && !o.bgcolor.Equal(&s.bgcolor) {
f(true, "background-color:"+o.bgcolor.String()+";")
}
f(true, "\"")
return err
}
// WriteClasses outputs css classes if needed
func (o *State) WriteClasses(w io.Writer, s *State) error {
if !o.blink {
return nil
}
var err error
f := func(b bool, s string) {
if err == nil && b {
_, err = w.Write([]byte(s))
}
}
f(true, " class=\"")
f(o.blink, "aes2htm-blink")
f(true, "\"")
return err
}