Debian Codegraph @

debian/vendor/github.com/micro-editor/tcell/v2/style.go

raw
1 // Copyright 2020 The TCell Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use file except in compliance with the License.
5 // You may obtain a copy of the license at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 package tcell
16
17 // Style represents a complete text style, including both foreground
18 // and background color. We encode it in a 64-bit int for efficiency.
19 // The coding is (MSB): <7b flags><1b><24b fgcolor><7b attr><1b><24b bgcolor>.
20 // The <1b> is set true to indicate that the color is an RGB color, rather
21 // than a named index.
22 //
23 // This gives 24bit color options, if it ever becomes truly necessary.
24 // However, applications must not rely on this encoding.
25 //
26 // Note that not all terminals can display all colors or attributes, and
27 // many might have specific incompatibilities between specific attributes
28 // and color combinations.
29 //
30 // The intention is to extend styles to support paletting, in which case
31 // some flag bit(s) would be set, and the foreground and background colors
32 // would be replaced with a palette number and palette index.
33 //
34 // To use Style, just declare a variable of its type.
35 type Style struct {
36 fg Color
37 bg Color
38 attrs AttrMask
39 }
40
41 // StyleDefault represents a default style, based upon the context.
42 // It is the zero value.
43 var StyleDefault Style
44
45 // styleInvalid is just an arbitrary invalid style used internally.
46 var styleInvalid = Style{attrs: AttrInvalid}
47
48 // Foreground returns a new style based on s, with the foreground color set
49 // as requested. ColorDefault can be used to select the global default.
50 func (s Style) Foreground(c Color) Style {
51 return Style{
52 fg: c,
53 bg: s.bg,
54 attrs: s.attrs,
55 }
56 }
57
58 // Background returns a new style based on s, with the background color set
59 // as requested. ColorDefault can be used to select the global default.
60 func (s Style) Background(c Color) Style {
61 return Style{
62 fg: s.fg,
63 bg: c,
64 attrs: s.attrs,
65 }
66 }
67
68 // Decompose breaks a style up, returning the foreground, background,
69 // and other attributes.
70 func (s Style) Decompose() (fg Color, bg Color, attr AttrMask) {
71 return s.fg, s.bg, s.attrs
72 }
73
74 func (s Style) setAttrs(attrs AttrMask, on bool) Style {
75 if on {
76 return Style{
77 fg: s.fg,
78 bg: s.bg,
79 attrs: s.attrs | attrs,
80 }
81 }
82 return Style{
83 fg: s.fg,
84 bg: s.bg,
85 attrs: s.attrs &^ attrs,
86 }
87 }
88
89 // Normal returns the style with all attributes disabled.
90 func (s Style) Normal() Style {
91 return Style{
92 fg: s.fg,
93 bg: s.bg,
94 }
95 }
96
97 // Bold returns a new style based on s, with the bold attribute set
98 // as requested.
99 func (s Style) Bold(on bool) Style {
100 return s.setAttrs(AttrBold, on)
101 }
102
103 // Blink returns a new style based on s, with the blink attribute set
104 // as requested.
105 func (s Style) Blink(on bool) Style {
106 return s.setAttrs(AttrBlink, on)
107 }
108
109 // Dim returns a new style based on s, with the dim attribute set
110 // as requested.
111 func (s Style) Dim(on bool) Style {
112 return s.setAttrs(AttrDim, on)
113 }
114
115 // Italic returns a new style based on s, with the italic attribute set
116 // as requested.
117 func (s Style) Italic(on bool) Style {
118 return s.setAttrs(AttrItalic, on)
119 }
120
121 // Reverse returns a new style based on s, with the reverse attribute set
122 // as requested. (Reverse usually changes the foreground and background
123 // colors.)
124 func (s Style) Reverse(on bool) Style {
125 return s.setAttrs(AttrReverse, on)
126 }
127
128 // Underline returns a new style based on s, with the underline attribute set
129 // as requested.
130 func (s Style) Underline(on bool) Style {
131 return s.setAttrs(AttrUnderline, on)
132 }
133
134 // StrikeThrough sets strikethrough mode.
135 func (s Style) StrikeThrough(on bool) Style {
136 return s.setAttrs(AttrStrikeThrough, on)
137 }