termgui 0.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/Gemfile +14 -0
- data/README.md +321 -0
- data/lib/termgui.rb +1 -0
- data/src/action.rb +58 -0
- data/src/box.rb +90 -0
- data/src/color.rb +174 -0
- data/src/cursor.rb +69 -0
- data/src/editor/editor_base.rb +152 -0
- data/src/editor/editor_base_handlers.rb +116 -0
- data/src/element.rb +61 -0
- data/src/element_bounds.rb +111 -0
- data/src/element_box.rb +64 -0
- data/src/element_render.rb +102 -0
- data/src/element_style.rb +51 -0
- data/src/emitter.rb +102 -0
- data/src/emitter_state.rb +19 -0
- data/src/enterable.rb +93 -0
- data/src/event.rb +92 -0
- data/src/focus.rb +102 -0
- data/src/geometry.rb +53 -0
- data/src/image.rb +60 -0
- data/src/input.rb +85 -0
- data/src/input_grab.rb +17 -0
- data/src/input_time.rb +97 -0
- data/src/key.rb +114 -0
- data/src/log.rb +24 -0
- data/src/node.rb +117 -0
- data/src/node_attributes.rb +27 -0
- data/src/node_visit.rb +52 -0
- data/src/renderer.rb +119 -0
- data/src/renderer_cursor.rb +18 -0
- data/src/renderer_draw.rb +28 -0
- data/src/renderer_image.rb +31 -0
- data/src/renderer_print.rb +40 -0
- data/src/screen.rb +96 -0
- data/src/screen_element.rb +59 -0
- data/src/screen_input.rb +43 -0
- data/src/screen_renderer.rb +53 -0
- data/src/style.rb +149 -0
- data/src/tco/colouring.rb +248 -0
- data/src/tco/config.rb +57 -0
- data/src/tco/palette.rb +603 -0
- data/src/tco/tco_termgui.rb +30 -0
- data/src/termgui.rb +29 -0
- data/src/util/css.rb +98 -0
- data/src/util/css_query.rb +23 -0
- data/src/util/easing.rb +364 -0
- data/src/util/hash_object.rb +131 -0
- data/src/util/imagemagick.rb +27 -0
- data/src/util/justify.rb +20 -0
- data/src/util/unicode-categories.rb +572 -0
- data/src/util/wrap.rb +102 -0
- data/src/util.rb +110 -0
- data/src/widget/button.rb +33 -0
- data/src/widget/checkbox.rb +47 -0
- data/src/widget/col.rb +30 -0
- data/src/widget/image.rb +106 -0
- data/src/widget/inline.rb +40 -0
- data/src/widget/input_number.rb +73 -0
- data/src/widget/inputbox.rb +85 -0
- data/src/widget/label.rb +33 -0
- data/src/widget/modal.rb +69 -0
- data/src/widget/row.rb +26 -0
- data/src/widget/selectbox.rb +100 -0
- data/src/widget/textarea.rb +54 -0
- data/src/xml/xml.rb +80 -0
- data/test/action_test.rb +34 -0
- data/test/box_test.rb +15 -0
- data/test/css_test.rb +39 -0
- data/test/editor/editor_base_test.rb +201 -0
- data/test/element_bounds_test.rb +77 -0
- data/test/element_box_test.rb +8 -0
- data/test/element_render_test.rb +124 -0
- data/test/element_style_test.rb +85 -0
- data/test/element_test.rb +10 -0
- data/test/emitter_test.rb +108 -0
- data/test/event_test.rb +19 -0
- data/test/focus_test.rb +37 -0
- data/test/geometry_test.rb +12 -0
- data/test/input_test.rb +47 -0
- data/test/key_test.rb +14 -0
- data/test/log_test.rb +21 -0
- data/test/node_test.rb +105 -0
- data/test/performance/performance1.rb +48 -0
- data/test/renderer_test.rb +74 -0
- data/test/renderer_test_rect.rb +4 -0
- data/test/screen_test.rb +58 -0
- data/test/style_test.rb +18 -0
- data/test/termgui_test.rb +10 -0
- data/test/test_all.rb +30 -0
- data/test/util_hash_object_test.rb +93 -0
- data/test/util_test.rb +26 -0
- data/test/widget/checkbox_test.rb +99 -0
- data/test/widget/col_test.rb +87 -0
- data/test/widget/inline_test.rb +40 -0
- data/test/widget/label_test.rb +94 -0
- data/test/widget/row_test.rb +40 -0
- data/test/wrap_test.rb +11 -0
- data/test/xml_test.rb +77 -0
- metadata +148 -0
data/src/tco/config.rb
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
# adapted from tco
|
2
|
+
# tco - terminal colouring application and library
|
3
|
+
# Copyright (c) 2013, 2014 Radek Pazdera
|
4
|
+
|
5
|
+
module Tco
|
6
|
+
class Config
|
7
|
+
attr_accessor :options, :colour_values, :names, :styles
|
8
|
+
|
9
|
+
def initialize(locations = [])
|
10
|
+
@options = {
|
11
|
+
'palette' => 'extended',
|
12
|
+
'output' => 'term',
|
13
|
+
'disabled' => false
|
14
|
+
}
|
15
|
+
@colour_values = {}
|
16
|
+
@names = {
|
17
|
+
'black' => '@0',
|
18
|
+
'red' => '@1',
|
19
|
+
'green' => '@2',
|
20
|
+
'yellow' => '@3',
|
21
|
+
'blue' => '@4',
|
22
|
+
'magenta' => '@5',
|
23
|
+
'cyan' => '@6',
|
24
|
+
'light-grey' => '@7',
|
25
|
+
'grey' => '@8',
|
26
|
+
'light-red' => '@9',
|
27
|
+
'light-green' => '@10',
|
28
|
+
'light-yellow' => '@11',
|
29
|
+
'light-blue' => '@12',
|
30
|
+
'light-magenta' => '@13',
|
31
|
+
'light-cyan' => '@14',
|
32
|
+
'white' => '@15'
|
33
|
+
}
|
34
|
+
|
35
|
+
@styles = {}
|
36
|
+
|
37
|
+
locations.each do |conf_file|
|
38
|
+
conf_file = File.expand_path conf_file
|
39
|
+
next unless File.exist? conf_file
|
40
|
+
|
41
|
+
load conf_file
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
private
|
46
|
+
|
47
|
+
def name_exists?(colour_name)
|
48
|
+
@names.key? colour_name
|
49
|
+
end
|
50
|
+
|
51
|
+
def parse_bool(value)
|
52
|
+
return true if value =~ /true/i || value =~ /yes/i || value.to_i >= 1
|
53
|
+
|
54
|
+
false
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
data/src/tco/palette.rb
ADDED
@@ -0,0 +1,603 @@
|
|
1
|
+
# rubocop:disable Naming/VariableName, Layout/EndAlignment, Naming/MethodName
|
2
|
+
|
3
|
+
# adapted from tco
|
4
|
+
# tco - terminal colouring application and library
|
5
|
+
# Copyright (c) 2013, 2014 Radek Pazdera
|
6
|
+
|
7
|
+
module Tco
|
8
|
+
class Unknown
|
9
|
+
attr_reader :id
|
10
|
+
|
11
|
+
def initialize(id)
|
12
|
+
@id = id
|
13
|
+
end
|
14
|
+
|
15
|
+
def to_s
|
16
|
+
"@#{@id}"
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
class Colour
|
21
|
+
attr_reader :rgb, :lab
|
22
|
+
|
23
|
+
@@fast = false
|
24
|
+
|
25
|
+
def self.fast=(value)
|
26
|
+
@@fast = value
|
27
|
+
end
|
28
|
+
|
29
|
+
def initialize(rgb, lab = nil)
|
30
|
+
@rgb = rgb
|
31
|
+
@lab = lab || @@fast ? lab : rgb_to_lab(rgb)
|
32
|
+
@hsl = nil
|
33
|
+
@yiq = nil
|
34
|
+
end
|
35
|
+
|
36
|
+
def -(other)
|
37
|
+
if @@fast
|
38
|
+
(@rgb[0].abs2 - other.rgb[0].abs2).abs + (@rgb[1].abs2 - other.rgb[1].abs2).abs + (@rgb[2].abs2 - other.rgb[2].abs2).abs
|
39
|
+
else
|
40
|
+
delta_e_2000 @lab, other.lab
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
def <=>(other)
|
45
|
+
hsl[0] <=> other.hsl[0]
|
46
|
+
end
|
47
|
+
|
48
|
+
def to_s
|
49
|
+
values = @rgb.map do |v|
|
50
|
+
v = v.to_i.to_s 16
|
51
|
+
case v.length
|
52
|
+
when 0 then '00'
|
53
|
+
when 1 then '0' + v
|
54
|
+
when 2 then v
|
55
|
+
end
|
56
|
+
end
|
57
|
+
'#' + values.join('')
|
58
|
+
end
|
59
|
+
|
60
|
+
def to_hash
|
61
|
+
(@rgb[0] + 255) + ((@rgb[1] + 255) * 1000) + ((@rgb[2] + 255) * 1000 * 1000)
|
62
|
+
end
|
63
|
+
|
64
|
+
def hsl
|
65
|
+
if @hsl == nil
|
66
|
+
@hsl = rgb_to_hsl @rgb
|
67
|
+
else
|
68
|
+
@hsl
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
def yiq
|
73
|
+
if @yiq == nil
|
74
|
+
@yiq = rgb_to_yiq @rgb
|
75
|
+
else
|
76
|
+
@yiq
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
private
|
81
|
+
|
82
|
+
# source: http://www.easyrgb.com/index.php?X=MATH&H=02#text2
|
83
|
+
def rgb_to_xyz(colour)
|
84
|
+
r, g, b = colour.map do |v|
|
85
|
+
v /= 255.0
|
86
|
+
v = if v > 0.04045
|
87
|
+
((v + 0.055) / 1.055)**2.4
|
88
|
+
else
|
89
|
+
v / 12.92
|
90
|
+
end
|
91
|
+
v *= 100
|
92
|
+
end
|
93
|
+
|
94
|
+
# Observer = 2°, Illuminant = D65
|
95
|
+
x = r * 0.4124 + g * 0.3576 + b * 0.1805
|
96
|
+
y = r * 0.2126 + g * 0.7152 + b * 0.0722
|
97
|
+
z = r * 0.0193 + g * 0.1192 + b * 0.9505
|
98
|
+
|
99
|
+
[x, y, z]
|
100
|
+
end
|
101
|
+
|
102
|
+
def xyz_to_lab(colour)
|
103
|
+
f = lambda { |t|
|
104
|
+
return t**(1.0 / 3) if t > (6.0 / 29)**3
|
105
|
+
|
106
|
+
return (1.0 / 3) * ((29.0 / 6)**2) * t + (4.0 / 29)
|
107
|
+
}
|
108
|
+
|
109
|
+
x, y, z = colour
|
110
|
+
xn, yn, zn = rgb_to_xyz([255, 255, 255])
|
111
|
+
l = 116 * f.call(y / yn) - 16
|
112
|
+
a = 500 * (f.call(x / xn) - f.call(y / yn))
|
113
|
+
b = 200 * (f.call(y / yn) - f.call(z / zn))
|
114
|
+
|
115
|
+
[l, a, b]
|
116
|
+
end
|
117
|
+
|
118
|
+
def rgb_to_lab(rgb_val)
|
119
|
+
xyz_to_lab rgb_to_xyz rgb_val
|
120
|
+
end
|
121
|
+
|
122
|
+
def rgb_to_hsl(rgb_val)
|
123
|
+
r, g, b = rgb_val.map { |v| v / 255.0 }
|
124
|
+
|
125
|
+
min, max = [r, g, b].minmax
|
126
|
+
delta = max - min
|
127
|
+
|
128
|
+
lig = (max + min) / 2.0
|
129
|
+
|
130
|
+
if delta == 0
|
131
|
+
hue = 0
|
132
|
+
sat = 0
|
133
|
+
else
|
134
|
+
sat = if lig < 0.5
|
135
|
+
delta / (0.0 + (max + min))
|
136
|
+
else
|
137
|
+
delta / (2.0 - max - min)
|
138
|
+
end
|
139
|
+
|
140
|
+
delta_r = (((max - r) / 6.0) + (delta / 2.0)) / delta
|
141
|
+
delta_g = (((max - g) / 6.0) + (delta / 2.0)) / delta
|
142
|
+
delta_b = (((max - b) / 6.0) + (delta / 2.0)) / delta
|
143
|
+
|
144
|
+
hue = case max
|
145
|
+
when r then delta_b - delta_g
|
146
|
+
when g then (1.0 / 3) + delta_r - delta_b
|
147
|
+
when b then (2.0 / 3) + delta_g - delta_r
|
148
|
+
end
|
149
|
+
|
150
|
+
hue += 1 if hue < 0
|
151
|
+
hue -= 1 if hue > 1
|
152
|
+
end
|
153
|
+
|
154
|
+
[360 * hue, 100 * sat, 100 * lig]
|
155
|
+
end
|
156
|
+
|
157
|
+
def rgb_to_yiq(rgb_val)
|
158
|
+
r, g, b = rgb_val
|
159
|
+
|
160
|
+
y = 0.299 * r + 0.587 * g + 0.114 * b
|
161
|
+
i = 0.569 * r - 0.275 * g - 0.321 * b
|
162
|
+
q = 0.212 * r - 0.523 * g + 0.311 * b
|
163
|
+
|
164
|
+
[y, i, q]
|
165
|
+
end
|
166
|
+
|
167
|
+
def rad_to_deg(v)
|
168
|
+
(v * 180) / Math::PI
|
169
|
+
end
|
170
|
+
|
171
|
+
def deg_to_rad(v)
|
172
|
+
(v * Math::PI) / 180
|
173
|
+
end
|
174
|
+
|
175
|
+
def CieLab2Hue(a, b)
|
176
|
+
bias = 0
|
177
|
+
return 0 if a >= 0 && b == 0
|
178
|
+
return 180 if a < 0 && b == 0
|
179
|
+
return 90 if a == 0 && b > 0
|
180
|
+
return 270 if a == 0 && b < 0
|
181
|
+
|
182
|
+
bias = if a > 0 && b > 0 then 0
|
183
|
+
elsif a < 0 then 180
|
184
|
+
elsif a > 0 && b < 0 then 360
|
185
|
+
end
|
186
|
+
|
187
|
+
rad_to_deg(Math.atan(b / a)) + bias
|
188
|
+
end
|
189
|
+
|
190
|
+
def delta_e_2000(lab1, lab2)
|
191
|
+
l1, a1, b1 = lab1
|
192
|
+
l2, a2, b2 = lab2
|
193
|
+
kl = 1
|
194
|
+
kc = 1
|
195
|
+
kh = 1
|
196
|
+
|
197
|
+
xC1 = Math.sqrt(a1**2 + b1**2)
|
198
|
+
xC2 = Math.sqrt(a2**2 + b2**2)
|
199
|
+
xCX = (xC1 + xC2) / 2
|
200
|
+
xGX = 0.5 * (1 - Math.sqrt(xCX**7 / (xCX**7 + 25**7)))
|
201
|
+
xNN = (1 + xGX) * a1
|
202
|
+
xC1 = Math.sqrt(xNN**2 + b1**2)
|
203
|
+
xH1 = CieLab2Hue(xNN, b1)
|
204
|
+
xNN = (1 + xGX) * a2
|
205
|
+
xC2 = Math.sqrt(xNN**2 + b2**2)
|
206
|
+
xH2 = CieLab2Hue(xNN, b2)
|
207
|
+
xDL = l2 - l1
|
208
|
+
xDC = xC2 - xC1
|
209
|
+
if (xC1 * xC2) == 0
|
210
|
+
xDH = 0
|
211
|
+
else
|
212
|
+
xNN = (xH2 - xH1).round(12)
|
213
|
+
xDH = if xNN.abs <= 180
|
214
|
+
xH2 - xH1
|
215
|
+
else
|
216
|
+
if xNN > 180
|
217
|
+
xH2 - xH1 - 360
|
218
|
+
else
|
219
|
+
xH2 - xH1 + 360
|
220
|
+
end
|
221
|
+
end
|
222
|
+
end
|
223
|
+
xDH = 2 * Math.sqrt(xC1 * xC2) * Math.sin(deg_to_rad(xDH / 2.0))
|
224
|
+
xLX = (l1 + l2) / 2.0
|
225
|
+
xCY = (xC1 + xC2) / 2.0
|
226
|
+
if xC1 * xC2 == 0
|
227
|
+
xHX = xH1 + xH2
|
228
|
+
else
|
229
|
+
xNN = (xH1 - xH2).round(12).abs
|
230
|
+
xHX = if xNN > 180
|
231
|
+
if xH2 + xH1 < 360
|
232
|
+
xH1 + xH2 + 360
|
233
|
+
else
|
234
|
+
xH1 + xH2 - 360
|
235
|
+
end
|
236
|
+
else
|
237
|
+
xH1 + xH2
|
238
|
+
end
|
239
|
+
xHX /= 2.0
|
240
|
+
end
|
241
|
+
xTX = 1 - 0.17 * Math.cos(deg_to_rad(xHX - 30)) + 0.24 * Math.cos(deg_to_rad(2 * xHX)) + 0.32 * Math.cos(deg_to_rad(3 * xHX + 6)) - 0.20 * Math.cos(deg_to_rad(4 * xHX - 63))
|
242
|
+
xPH = 30 * Math.exp(-((xHX - 275) / 25.0) * ((xHX - 275) / 25.0))
|
243
|
+
xRC = 2 * Math.sqrt(xCY**7 / (xCY**7 + 25**7))
|
244
|
+
xSL = 1 + ((0.015 * ((xLX - 50) * (xLX - 50))) / Math.sqrt(20 + ((xLX - 50) * (xLX - 50))))
|
245
|
+
xSC = 1 + 0.045 * xCY
|
246
|
+
xSH = 1 + 0.015 * xCY * xTX
|
247
|
+
xRT = -Math.sin(deg_to_rad(2 * xPH)) * xRC
|
248
|
+
xDL /= (kl * xSL)
|
249
|
+
xDC /= (kc * xSC)
|
250
|
+
xDH /= (kh * xSH)
|
251
|
+
|
252
|
+
Math.sqrt(xDL**2 + xDC**2 + xDH**2 + xRT * xDC * xDH)
|
253
|
+
end
|
254
|
+
end
|
255
|
+
|
256
|
+
class Palette
|
257
|
+
attr_reader :type
|
258
|
+
|
259
|
+
def initialize(type)
|
260
|
+
set_type type
|
261
|
+
|
262
|
+
@cache = {}
|
263
|
+
@palette = [
|
264
|
+
# ANSI colours (the first 16) are configurable by users in most
|
265
|
+
# terminals. Therefore, they're not used for colour matching, unless
|
266
|
+
# they were explicitly configured in tco.conf.
|
267
|
+
#
|
268
|
+
# The colour values in comments are the defaults for xterm.
|
269
|
+
Unknown.new(0), # [0, 0, 0]
|
270
|
+
Unknown.new(1), # [205, 0, 0]
|
271
|
+
Unknown.new(2), # [0, 205, 0]
|
272
|
+
Unknown.new(3), # [205, 205, 0]
|
273
|
+
Unknown.new(4), # [0, 0, 238]
|
274
|
+
Unknown.new(5), # [205, 0, 205]
|
275
|
+
Unknown.new(6), # [0, 205, 205]
|
276
|
+
Unknown.new(7), # [229, 229, 229]
|
277
|
+
Unknown.new(8), # [127, 127, 127]
|
278
|
+
Unknown.new(9), # [255, 0, 0]
|
279
|
+
Unknown.new(10), # [0, 255, 0]
|
280
|
+
Unknown.new(11), # [255, 255, 0]
|
281
|
+
Unknown.new(12), # [92, 92, 255]
|
282
|
+
Unknown.new(13), # [255, 0, 255]
|
283
|
+
Unknown.new(14), # [0, 255, 255]
|
284
|
+
Unknown.new(15), # [255, 255, 255]
|
285
|
+
|
286
|
+
# The colours bellow are the definitions from xterm extended
|
287
|
+
# colour palette. They should be the same across terminals.
|
288
|
+
Colour.new([0, 0, 0]),
|
289
|
+
Colour.new([0, 0, 95]),
|
290
|
+
Colour.new([0, 0, 135]),
|
291
|
+
Colour.new([0, 0, 175]),
|
292
|
+
Colour.new([0, 0, 215]),
|
293
|
+
Colour.new([0, 0, 255]),
|
294
|
+
Colour.new([0, 95, 0]),
|
295
|
+
Colour.new([0, 95, 95]),
|
296
|
+
Colour.new([0, 95, 135]),
|
297
|
+
Colour.new([0, 95, 175]),
|
298
|
+
Colour.new([0, 95, 215]),
|
299
|
+
Colour.new([0, 95, 255]),
|
300
|
+
Colour.new([0, 135, 0]),
|
301
|
+
Colour.new([0, 135, 95]),
|
302
|
+
Colour.new([0, 135, 135]),
|
303
|
+
Colour.new([0, 135, 175]),
|
304
|
+
Colour.new([0, 135, 215]),
|
305
|
+
Colour.new([0, 135, 255]),
|
306
|
+
Colour.new([0, 175, 0]),
|
307
|
+
Colour.new([0, 175, 95]),
|
308
|
+
Colour.new([0, 175, 135]),
|
309
|
+
Colour.new([0, 175, 175]),
|
310
|
+
Colour.new([0, 175, 215]),
|
311
|
+
Colour.new([0, 175, 255]),
|
312
|
+
Colour.new([0, 215, 0]),
|
313
|
+
Colour.new([0, 215, 95]),
|
314
|
+
Colour.new([0, 215, 135]),
|
315
|
+
Colour.new([0, 215, 175]),
|
316
|
+
Colour.new([0, 215, 215]),
|
317
|
+
Colour.new([0, 215, 255]),
|
318
|
+
Colour.new([0, 255, 0]),
|
319
|
+
Colour.new([0, 255, 95]),
|
320
|
+
Colour.new([0, 255, 135]),
|
321
|
+
Colour.new([0, 255, 175]),
|
322
|
+
Colour.new([0, 255, 215]),
|
323
|
+
Colour.new([0, 255, 255]),
|
324
|
+
Colour.new([95, 0, 0]),
|
325
|
+
Colour.new([95, 0, 95]),
|
326
|
+
Colour.new([95, 0, 135]),
|
327
|
+
Colour.new([95, 0, 175]),
|
328
|
+
Colour.new([95, 0, 215]),
|
329
|
+
Colour.new([95, 0, 255]),
|
330
|
+
Colour.new([95, 95, 0]),
|
331
|
+
Colour.new([95, 95, 95]),
|
332
|
+
Colour.new([95, 95, 135]),
|
333
|
+
Colour.new([95, 95, 175]),
|
334
|
+
Colour.new([95, 95, 215]),
|
335
|
+
Colour.new([95, 95, 255]),
|
336
|
+
Colour.new([95, 135, 0]),
|
337
|
+
Colour.new([95, 135, 95]),
|
338
|
+
Colour.new([95, 135, 135]),
|
339
|
+
Colour.new([95, 135, 175]),
|
340
|
+
Colour.new([95, 135, 215]),
|
341
|
+
Colour.new([95, 135, 255]),
|
342
|
+
Colour.new([95, 175, 0]),
|
343
|
+
Colour.new([95, 175, 95]),
|
344
|
+
Colour.new([95, 175, 135]),
|
345
|
+
Colour.new([95, 175, 175]),
|
346
|
+
Colour.new([95, 175, 215]),
|
347
|
+
Colour.new([95, 175, 255]),
|
348
|
+
Colour.new([95, 215, 0]),
|
349
|
+
Colour.new([95, 215, 95]),
|
350
|
+
Colour.new([95, 215, 135]),
|
351
|
+
Colour.new([95, 215, 175]),
|
352
|
+
Colour.new([95, 215, 215]),
|
353
|
+
Colour.new([95, 215, 255]),
|
354
|
+
Colour.new([95, 255, 0]),
|
355
|
+
Colour.new([95, 255, 95]),
|
356
|
+
Colour.new([95, 255, 135]),
|
357
|
+
Colour.new([95, 255, 175]),
|
358
|
+
Colour.new([95, 255, 215]),
|
359
|
+
Colour.new([95, 255, 255]),
|
360
|
+
Colour.new([135, 0, 0]),
|
361
|
+
Colour.new([135, 0, 95]),
|
362
|
+
Colour.new([135, 0, 135]),
|
363
|
+
Colour.new([135, 0, 175]),
|
364
|
+
Colour.new([135, 0, 215]),
|
365
|
+
Colour.new([135, 0, 255]),
|
366
|
+
Colour.new([135, 95, 0]),
|
367
|
+
Colour.new([135, 95, 95]),
|
368
|
+
Colour.new([135, 95, 135]),
|
369
|
+
Colour.new([135, 95, 175]),
|
370
|
+
Colour.new([135, 95, 215]),
|
371
|
+
Colour.new([135, 95, 255]),
|
372
|
+
Colour.new([135, 135, 0]),
|
373
|
+
Colour.new([135, 135, 95]),
|
374
|
+
Colour.new([135, 135, 135]),
|
375
|
+
Colour.new([135, 135, 175]),
|
376
|
+
Colour.new([135, 135, 215]),
|
377
|
+
Colour.new([135, 135, 255]),
|
378
|
+
Colour.new([135, 175, 0]),
|
379
|
+
Colour.new([135, 175, 95]),
|
380
|
+
Colour.new([135, 175, 135]),
|
381
|
+
Colour.new([135, 175, 175]),
|
382
|
+
Colour.new([135, 175, 215]),
|
383
|
+
Colour.new([135, 175, 255]),
|
384
|
+
Colour.new([135, 215, 0]),
|
385
|
+
Colour.new([135, 215, 95]),
|
386
|
+
Colour.new([135, 215, 135]),
|
387
|
+
Colour.new([135, 215, 175]),
|
388
|
+
Colour.new([135, 215, 215]),
|
389
|
+
Colour.new([135, 215, 255]),
|
390
|
+
Colour.new([135, 255, 0]),
|
391
|
+
Colour.new([135, 255, 95]),
|
392
|
+
Colour.new([135, 255, 135]),
|
393
|
+
Colour.new([135, 255, 175]),
|
394
|
+
Colour.new([135, 255, 215]),
|
395
|
+
Colour.new([135, 255, 255]),
|
396
|
+
Colour.new([175, 0, 0]),
|
397
|
+
Colour.new([175, 0, 95]),
|
398
|
+
Colour.new([175, 0, 135]),
|
399
|
+
Colour.new([175, 0, 175]),
|
400
|
+
Colour.new([175, 0, 215]),
|
401
|
+
Colour.new([175, 0, 255]),
|
402
|
+
Colour.new([175, 95, 0]),
|
403
|
+
Colour.new([175, 95, 95]),
|
404
|
+
Colour.new([175, 95, 135]),
|
405
|
+
Colour.new([175, 95, 175]),
|
406
|
+
Colour.new([175, 95, 215]),
|
407
|
+
Colour.new([175, 95, 255]),
|
408
|
+
Colour.new([175, 135, 0]),
|
409
|
+
Colour.new([175, 135, 95]),
|
410
|
+
Colour.new([175, 135, 135]),
|
411
|
+
Colour.new([175, 135, 175]),
|
412
|
+
Colour.new([175, 135, 215]),
|
413
|
+
Colour.new([175, 135, 255]),
|
414
|
+
Colour.new([175, 175, 0]),
|
415
|
+
Colour.new([175, 175, 95]),
|
416
|
+
Colour.new([175, 175, 135]),
|
417
|
+
Colour.new([175, 175, 175]),
|
418
|
+
Colour.new([175, 175, 215]),
|
419
|
+
Colour.new([175, 175, 255]),
|
420
|
+
Colour.new([175, 215, 0]),
|
421
|
+
Colour.new([175, 215, 95]),
|
422
|
+
Colour.new([175, 215, 135]),
|
423
|
+
Colour.new([175, 215, 175]),
|
424
|
+
Colour.new([175, 215, 215]),
|
425
|
+
Colour.new([175, 215, 255]),
|
426
|
+
Colour.new([175, 255, 0]),
|
427
|
+
Colour.new([175, 255, 95]),
|
428
|
+
Colour.new([175, 255, 135]),
|
429
|
+
Colour.new([175, 255, 175]),
|
430
|
+
Colour.new([175, 255, 215]),
|
431
|
+
Colour.new([175, 255, 255]),
|
432
|
+
Colour.new([215, 0, 0]),
|
433
|
+
Colour.new([215, 0, 95]),
|
434
|
+
Colour.new([215, 0, 135]),
|
435
|
+
Colour.new([215, 0, 175]),
|
436
|
+
Colour.new([215, 0, 215]),
|
437
|
+
Colour.new([215, 0, 255]),
|
438
|
+
Colour.new([215, 95, 0]),
|
439
|
+
Colour.new([215, 95, 95]),
|
440
|
+
Colour.new([215, 95, 135]),
|
441
|
+
Colour.new([215, 95, 175]),
|
442
|
+
Colour.new([215, 95, 215]),
|
443
|
+
Colour.new([215, 95, 255]),
|
444
|
+
Colour.new([215, 135, 0]),
|
445
|
+
Colour.new([215, 135, 95]),
|
446
|
+
Colour.new([215, 135, 135]),
|
447
|
+
Colour.new([215, 135, 175]),
|
448
|
+
Colour.new([215, 135, 215]),
|
449
|
+
Colour.new([215, 135, 255]),
|
450
|
+
Colour.new([215, 175, 0]),
|
451
|
+
Colour.new([215, 175, 95]),
|
452
|
+
Colour.new([215, 175, 135]),
|
453
|
+
Colour.new([215, 175, 175]),
|
454
|
+
Colour.new([215, 175, 215]),
|
455
|
+
Colour.new([215, 175, 255]),
|
456
|
+
Colour.new([215, 215, 0]),
|
457
|
+
Colour.new([215, 215, 95]),
|
458
|
+
Colour.new([215, 215, 135]),
|
459
|
+
Colour.new([215, 215, 175]),
|
460
|
+
Colour.new([215, 215, 215]),
|
461
|
+
Colour.new([215, 215, 255]),
|
462
|
+
Colour.new([215, 255, 0]),
|
463
|
+
Colour.new([215, 255, 95]),
|
464
|
+
Colour.new([215, 255, 135]),
|
465
|
+
Colour.new([215, 255, 175]),
|
466
|
+
Colour.new([215, 255, 215]),
|
467
|
+
Colour.new([215, 255, 255]),
|
468
|
+
Colour.new([255, 0, 0]),
|
469
|
+
Colour.new([255, 0, 95]),
|
470
|
+
Colour.new([255, 0, 135]),
|
471
|
+
Colour.new([255, 0, 175]),
|
472
|
+
Colour.new([255, 0, 215]),
|
473
|
+
Colour.new([255, 0, 255]),
|
474
|
+
Colour.new([255, 95, 0]),
|
475
|
+
Colour.new([255, 95, 95]),
|
476
|
+
Colour.new([255, 95, 135]),
|
477
|
+
Colour.new([255, 95, 175]),
|
478
|
+
Colour.new([255, 95, 215]),
|
479
|
+
Colour.new([255, 95, 255]),
|
480
|
+
Colour.new([255, 135, 0]),
|
481
|
+
Colour.new([255, 135, 95]),
|
482
|
+
Colour.new([255, 135, 135]),
|
483
|
+
Colour.new([255, 135, 175]),
|
484
|
+
Colour.new([255, 135, 215]),
|
485
|
+
Colour.new([255, 135, 255]),
|
486
|
+
Colour.new([255, 175, 0]),
|
487
|
+
Colour.new([255, 175, 95]),
|
488
|
+
Colour.new([255, 175, 135]),
|
489
|
+
Colour.new([255, 175, 175]),
|
490
|
+
Colour.new([255, 175, 215]),
|
491
|
+
Colour.new([255, 175, 255]),
|
492
|
+
Colour.new([255, 215, 0]),
|
493
|
+
Colour.new([255, 215, 95]),
|
494
|
+
Colour.new([255, 215, 135]),
|
495
|
+
Colour.new([255, 215, 175]),
|
496
|
+
Colour.new([255, 215, 215]),
|
497
|
+
Colour.new([255, 215, 255]),
|
498
|
+
Colour.new([255, 255, 0]),
|
499
|
+
Colour.new([255, 255, 95]),
|
500
|
+
Colour.new([255, 255, 135]),
|
501
|
+
Colour.new([255, 255, 175]),
|
502
|
+
Colour.new([255, 255, 215]),
|
503
|
+
Colour.new([255, 255, 255]),
|
504
|
+
Colour.new([8, 8, 8]),
|
505
|
+
Colour.new([18, 18, 18]),
|
506
|
+
Colour.new([28, 28, 28]),
|
507
|
+
Colour.new([38, 38, 38]),
|
508
|
+
Colour.new([48, 48, 48]),
|
509
|
+
Colour.new([58, 58, 58]),
|
510
|
+
Colour.new([68, 68, 68]),
|
511
|
+
Colour.new([78, 78, 78]),
|
512
|
+
Colour.new([88, 88, 88]),
|
513
|
+
Colour.new([98, 98, 98]),
|
514
|
+
Colour.new([108, 108, 108]),
|
515
|
+
Colour.new([118, 118, 118]),
|
516
|
+
Colour.new([128, 128, 128]),
|
517
|
+
Colour.new([138, 138, 138]),
|
518
|
+
Colour.new([148, 148, 148]),
|
519
|
+
Colour.new([158, 158, 158]),
|
520
|
+
Colour.new([168, 168, 168]),
|
521
|
+
Colour.new([178, 178, 178]),
|
522
|
+
Colour.new([188, 188, 188]),
|
523
|
+
Colour.new([198, 198, 198]),
|
524
|
+
Colour.new([208, 208, 208]),
|
525
|
+
Colour.new([218, 218, 218]),
|
526
|
+
Colour.new([228, 228, 228]),
|
527
|
+
Colour.new([238, 238, 238])
|
528
|
+
]
|
529
|
+
end
|
530
|
+
|
531
|
+
def set_colour_value(id, rgb_colour)
|
532
|
+
# raise "Id '#{id}' out of range." unless id.between?(0, @palette.length - 1)
|
533
|
+
@palette[id] = Colour.new(rgb_colour)
|
534
|
+
end
|
535
|
+
|
536
|
+
def get_colour_value(id)
|
537
|
+
# raise "Id '#{id}' out of range." unless id.between?(0, @palette.length - 1)
|
538
|
+
# raise "Value of colour '#{id}' is unknown" if @palette[id].is_a? Unknown
|
539
|
+
|
540
|
+
@palette[id]&.rgb
|
541
|
+
end
|
542
|
+
|
543
|
+
def is_known?(id)
|
544
|
+
# raise "Id '#{id}' out of range." unless id.between?(0, @palette.length - 1)
|
545
|
+
!@palette[id].is_a? Unknown
|
546
|
+
end
|
547
|
+
|
548
|
+
def match_colour(colour)
|
549
|
+
unless colour.is_a? Colour
|
550
|
+
msg = "Unsupported argument type '#{colour.class}', must be 'Colour'."
|
551
|
+
raise ArgumentError, msg
|
552
|
+
end
|
553
|
+
|
554
|
+
colour_key = colour.to_hash
|
555
|
+
if @cache.key? colour_key
|
556
|
+
@cache[colour_key]
|
557
|
+
else
|
558
|
+
distances = colours.map { |c| c.is_a?(Colour) ? c - colour : Float::INFINITY }
|
559
|
+
colour_index = distances.each_with_index.min[1]
|
560
|
+
|
561
|
+
# TODO: No cache eviction is currently in place
|
562
|
+
# We assume that applications won't use milions of different colours.
|
563
|
+
@cache[colour_key] = colour_index
|
564
|
+
colour_index
|
565
|
+
end
|
566
|
+
end
|
567
|
+
|
568
|
+
def reset_cache
|
569
|
+
@cache = {}
|
570
|
+
end
|
571
|
+
|
572
|
+
def colours
|
573
|
+
if @type == 'extended'
|
574
|
+
@palette
|
575
|
+
else
|
576
|
+
@palette[0, 8]
|
577
|
+
end
|
578
|
+
end
|
579
|
+
|
580
|
+
def type=(type)
|
581
|
+
set_type type
|
582
|
+
end
|
583
|
+
|
584
|
+
private
|
585
|
+
|
586
|
+
def set_type(type)
|
587
|
+
@type = case type
|
588
|
+
when 'auto'[0, type.length]
|
589
|
+
if ENV.key?('TERM') && (ENV['TERM'] == 'xterm-256color')
|
590
|
+
'extended'
|
591
|
+
else
|
592
|
+
'ansi'
|
593
|
+
end
|
594
|
+
when 'ansi'[0, type.length]
|
595
|
+
'ansi'
|
596
|
+
when 'extended'[0, type.length]
|
597
|
+
'extended'
|
598
|
+
else
|
599
|
+
raise "Unknown palette type '#{type}'."
|
600
|
+
end
|
601
|
+
end
|
602
|
+
end
|
603
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require_relative 'colouring'
|
2
|
+
require_relative 'config'
|
3
|
+
|
4
|
+
module TermGui
|
5
|
+
@colouring_config = Tco::Config.new
|
6
|
+
@colouring = Tco::Colouring.new @colouring_config
|
7
|
+
|
8
|
+
def self.open_style(style, colouring = @colouring)
|
9
|
+
s = colouring.decorate('__', style)
|
10
|
+
s = s.slice(0, s.length - 4 - 2)
|
11
|
+
s
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.close_style(_colouring = @colouring)
|
15
|
+
@colouring.decorate('', TermGui::Style.new(fg: 'red', bg: 'red', bold: true, underline: true))
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.print(s, style)
|
19
|
+
@colouring.decorate(s, style)
|
20
|
+
end
|
21
|
+
|
22
|
+
def self.fast_colouring(value)
|
23
|
+
Tco::Colour.fast = value
|
24
|
+
@colouring.palette.reset_cache
|
25
|
+
end
|
26
|
+
|
27
|
+
def self.to_rgb(value)
|
28
|
+
@colouring.get_colour_instance(value).rgb
|
29
|
+
end
|
30
|
+
end
|
data/src/termgui.rb
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
require_relative 'emitter'
|
2
|
+
require_relative 'event'
|
3
|
+
require_relative 'input'
|
4
|
+
require_relative 'node'
|
5
|
+
require_relative 'renderer'
|
6
|
+
require_relative 'style'
|
7
|
+
require_relative 'screen'
|
8
|
+
require_relative 'tco/tco_termgui'
|
9
|
+
require_relative 'element'
|
10
|
+
require_relative 'focus'
|
11
|
+
require_relative 'image'
|
12
|
+
require_relative 'action'
|
13
|
+
require_relative 'cursor'
|
14
|
+
require_relative 'enterable'
|
15
|
+
require_relative 'geometry'
|
16
|
+
require_relative 'util/css'
|
17
|
+
require_relative 'widget/button'
|
18
|
+
require_relative 'widget/col'
|
19
|
+
require_relative 'widget/inputbox'
|
20
|
+
require_relative 'widget/image'
|
21
|
+
require_relative 'widget/inline'
|
22
|
+
require_relative 'widget/checkbox'
|
23
|
+
require_relative 'widget/selectbox'
|
24
|
+
require_relative 'widget/input_number'
|
25
|
+
require_relative 'widget/label'
|
26
|
+
require_relative 'widget/modal'
|
27
|
+
require_relative 'widget/row'
|
28
|
+
require_relative 'editor/editor_base_handlers'
|
29
|
+
require_relative 'editor/editor_base'
|