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.
Files changed (101) hide show
  1. checksums.yaml +7 -0
  2. data/Gemfile +14 -0
  3. data/README.md +321 -0
  4. data/lib/termgui.rb +1 -0
  5. data/src/action.rb +58 -0
  6. data/src/box.rb +90 -0
  7. data/src/color.rb +174 -0
  8. data/src/cursor.rb +69 -0
  9. data/src/editor/editor_base.rb +152 -0
  10. data/src/editor/editor_base_handlers.rb +116 -0
  11. data/src/element.rb +61 -0
  12. data/src/element_bounds.rb +111 -0
  13. data/src/element_box.rb +64 -0
  14. data/src/element_render.rb +102 -0
  15. data/src/element_style.rb +51 -0
  16. data/src/emitter.rb +102 -0
  17. data/src/emitter_state.rb +19 -0
  18. data/src/enterable.rb +93 -0
  19. data/src/event.rb +92 -0
  20. data/src/focus.rb +102 -0
  21. data/src/geometry.rb +53 -0
  22. data/src/image.rb +60 -0
  23. data/src/input.rb +85 -0
  24. data/src/input_grab.rb +17 -0
  25. data/src/input_time.rb +97 -0
  26. data/src/key.rb +114 -0
  27. data/src/log.rb +24 -0
  28. data/src/node.rb +117 -0
  29. data/src/node_attributes.rb +27 -0
  30. data/src/node_visit.rb +52 -0
  31. data/src/renderer.rb +119 -0
  32. data/src/renderer_cursor.rb +18 -0
  33. data/src/renderer_draw.rb +28 -0
  34. data/src/renderer_image.rb +31 -0
  35. data/src/renderer_print.rb +40 -0
  36. data/src/screen.rb +96 -0
  37. data/src/screen_element.rb +59 -0
  38. data/src/screen_input.rb +43 -0
  39. data/src/screen_renderer.rb +53 -0
  40. data/src/style.rb +149 -0
  41. data/src/tco/colouring.rb +248 -0
  42. data/src/tco/config.rb +57 -0
  43. data/src/tco/palette.rb +603 -0
  44. data/src/tco/tco_termgui.rb +30 -0
  45. data/src/termgui.rb +29 -0
  46. data/src/util/css.rb +98 -0
  47. data/src/util/css_query.rb +23 -0
  48. data/src/util/easing.rb +364 -0
  49. data/src/util/hash_object.rb +131 -0
  50. data/src/util/imagemagick.rb +27 -0
  51. data/src/util/justify.rb +20 -0
  52. data/src/util/unicode-categories.rb +572 -0
  53. data/src/util/wrap.rb +102 -0
  54. data/src/util.rb +110 -0
  55. data/src/widget/button.rb +33 -0
  56. data/src/widget/checkbox.rb +47 -0
  57. data/src/widget/col.rb +30 -0
  58. data/src/widget/image.rb +106 -0
  59. data/src/widget/inline.rb +40 -0
  60. data/src/widget/input_number.rb +73 -0
  61. data/src/widget/inputbox.rb +85 -0
  62. data/src/widget/label.rb +33 -0
  63. data/src/widget/modal.rb +69 -0
  64. data/src/widget/row.rb +26 -0
  65. data/src/widget/selectbox.rb +100 -0
  66. data/src/widget/textarea.rb +54 -0
  67. data/src/xml/xml.rb +80 -0
  68. data/test/action_test.rb +34 -0
  69. data/test/box_test.rb +15 -0
  70. data/test/css_test.rb +39 -0
  71. data/test/editor/editor_base_test.rb +201 -0
  72. data/test/element_bounds_test.rb +77 -0
  73. data/test/element_box_test.rb +8 -0
  74. data/test/element_render_test.rb +124 -0
  75. data/test/element_style_test.rb +85 -0
  76. data/test/element_test.rb +10 -0
  77. data/test/emitter_test.rb +108 -0
  78. data/test/event_test.rb +19 -0
  79. data/test/focus_test.rb +37 -0
  80. data/test/geometry_test.rb +12 -0
  81. data/test/input_test.rb +47 -0
  82. data/test/key_test.rb +14 -0
  83. data/test/log_test.rb +21 -0
  84. data/test/node_test.rb +105 -0
  85. data/test/performance/performance1.rb +48 -0
  86. data/test/renderer_test.rb +74 -0
  87. data/test/renderer_test_rect.rb +4 -0
  88. data/test/screen_test.rb +58 -0
  89. data/test/style_test.rb +18 -0
  90. data/test/termgui_test.rb +10 -0
  91. data/test/test_all.rb +30 -0
  92. data/test/util_hash_object_test.rb +93 -0
  93. data/test/util_test.rb +26 -0
  94. data/test/widget/checkbox_test.rb +99 -0
  95. data/test/widget/col_test.rb +87 -0
  96. data/test/widget/inline_test.rb +40 -0
  97. data/test/widget/label_test.rb +94 -0
  98. data/test/widget/row_test.rb +40 -0
  99. data/test/wrap_test.rb +11 -0
  100. data/test/xml_test.rb +77 -0
  101. metadata +148 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: d6f70b10cb11cfc112b4ff9887817ffe36595facee0801121e4ffd1471c9a379
4
+ data.tar.gz: cec5a76cadece64fa9bd381c43ad1ab207949991c2de1c683a5e5a6cac582c76
5
+ SHA512:
6
+ metadata.gz: c6b601e0ebd61b28b32ed6740a585b5eb8d3c9f7451ed3615f6bd918646fe7a845e54cb34a091f464625f4790ecaa86f0a54be40c327220c4830e8b612fb310b
7
+ data.tar.gz: d569497fe644f5d51c91fa6e7e2c50da6abc7a8ce5ea9739198449b34bff01d8ef8ad0a83ee16f4362d1c5b46b1278ea4bc3c46715aee2cd8193de6a5d411623
data/Gemfile ADDED
@@ -0,0 +1,14 @@
1
+ source 'https://rubygems.org'
2
+
3
+ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ gem 'chunky_png'
6
+ # gem 'justify'
7
+ gem 'rexml'
8
+ gem 'strings'
9
+
10
+ gem 'filewatcher', group: :development
11
+ gem 'rubocop', require: false, group: :development
12
+ gem 'solargraph', require: false, group: :development
13
+ gem 'test-unit', group: :development
14
+ # gem 'yarn', require: false, group: :development
data/README.md ADDED
@@ -0,0 +1,321 @@
1
+ # termgui
2
+
3
+ Repository: https://github.com/cancerberoSgx/termgui
4
+
5
+ * Command line graphical user interface Ruby toolkit.
6
+ * Create desktop-like interfaces in the command line.
7
+ * Personal ruby-learning project at the beginning
8
+ * 100% ruby, no binaries, no dependencies
9
+ * some ideas taken from npm.org/blessed and my own npm.org/accursed
10
+ * HTML DOM like high level API with styles, layouts, box-model, cascade styles
11
+ * xml / edb support for declaring components in XML syntax
12
+ * rendered optionally supporting a buffer to print out the current screen for testing
13
+ * very well tested
14
+ * flexible managers for focus, actions, scroll, keys, input grab, input grab, cursor
15
+ * event loop supporting set_timeout, wait_for, set_interval
16
+ * text-area / editor support
17
+ * all colors and attributes
18
+ * some basic high level widgets: Button, Label, InputBox, TextArea, Editor, Select,
19
+ * independent low level APIs can be used without the high level overhead for listening stdin, drawing, etc
20
+ * box drawing, easing, css parser and dom selector
21
+
22
+
23
+ ## TODO / Status
24
+
25
+ See [TODO](TODO.md)
26
+
27
+
28
+ ## Motivation
29
+
30
+ * I don't want to use ncurses based dependencies since is binary
31
+ * I know there are a couple of initiatives 100% in ruby now, but I didn't knew them when I started this project
32
+ * I'm the author of npm.org/accursed which is a similar library so I can of already implemented this in JavaScript
33
+ * Initially a pet project to learn Ruby
34
+
35
+ ## Usage
36
+
37
+ ```
38
+ gem install termgui
39
+ ```
40
+
41
+ ### Example
42
+
43
+ ```rb
44
+ require 'termgui'
45
+ screen = Screen.new
46
+ left = screen.append_child Col.new(width: 0.4, height: 0.99, style: { bg: 'red' })
47
+ (0..8).map { |i| left.append_child Label.new(text: "Label_#{i}") }
48
+ right = screen.append_child Col.new(width: 0.6, height: 0.99, x: 0.4, style: {bg: 'blue'))
49
+ (0..4).map do |i|
50
+ right.append_child Button.new(
51
+ text: "Button_#{i}", x: 0.5,
52
+ style: {focus: {fg: '#ed5525'}},
53
+ action: proc { open_modal(screen: screen, title: "Button_#{i}") }
54
+ )
55
+ end
56
+ screen.start
57
+ ```
58
+
59
+ Result:
60
+
61
+ ![readme_screenshot01](readme_screenshot01.jpg)
62
+
63
+ ## Development commands
64
+
65
+ ```sh
66
+ cd termui
67
+ bundler install
68
+ sh bin/test
69
+ sh bin/doc
70
+ sh bin/dev # rails server
71
+ sh bin/watch # tests in watch mode
72
+ ```
73
+
74
+ ## API Reference
75
+
76
+ TODO
77
+
78
+ ### low level (working) examples
79
+
80
+ TODO
81
+
82
+ #### No DOM - just draw
83
+
84
+ TODO
85
+
86
+ #### Only Input
87
+
88
+ TODO
89
+
90
+ #### Only Renderer
91
+
92
+ TODO
93
+
94
+
95
+
96
+
97
+ ## Element attributes
98
+
99
+ Some high level element attributes implemented:
100
+
101
+ ### style-cascade
102
+
103
+ By default, children inherit parent style. If `element.get_attribute('style-cascade') == 'prevent'` it won't happen - this is the children won't be affected by its parent style and only its own is rendered.
104
+
105
+ ### focusable, focused and screen.focus.keys, focus, blur
106
+
107
+ elements with focusable attribute will be able to be focused when user press focus keys (configurable in screen.focus.keys ). By default screen.focus.keys == { next: ['tab'], prev: ['S-tab'] }.
108
+
109
+ focusable elements will emit "focus" and "blur" events
110
+
111
+ When focused, the attribute focused will be true and the element is able to receive "action" event (see actionable, action-keys below)
112
+
113
+ ### actionable, action-keys, action
114
+
115
+ this is useful to implement actionable widgets like buttons that, when focused, can emit "action" events when certain keys are pressed (by default ENTER)
116
+
117
+ focused elements with attribute "actionable" will emit "action" events if user press action-keys (enter by default) when they are focused. Action keys can be configured globally using screen.action.keys or by element with attribute action-keys. Both could be a string or array.
118
+
119
+
120
+ ### action-on-focus
121
+
122
+ automatically trigger an action event wneh an element is focused. helpful for selectbox so no enter is needed for working
123
+
124
+ ### escape-on-blur
125
+
126
+ automatically escapes an entered element on blur. helpful for selectbox so no escape is needed for switching focus - so it behaves like buttons
127
+
128
+
129
+ ### enterable, entered, change, input, escape, escape-keys
130
+
131
+ This is useful to implement textarea / textinput widgets for which we don't want to trigger focus or action events when user is writing text. When an enterable element (that also should be focusable) receives "action" it is set to "entered" mode. (whey you are writing text, you want TAB S-tab, enter, etc to actually insert characters and don't emit "focus" "action", etc events...)
132
+
133
+ When an element is on this mode (only one at a time) the rest of the elements will stop receiving common events like focus or action until it leaves the entered mode. . This could happen programmatically or by receiving "escape" event, by default pressing ESC will provoke "escape" event which will set entered = false and enable normal events again (like focus, action, etc). When entered==true, the entered element will listen for input independently and emit "input" events.
134
+
135
+ the enter event by default is provoked by "action" (enter) so it can be configured individually using action-keys.
136
+
137
+ the escape event by default pressing "escape" can be configurable per element using attribute escape-keys (just like action-keys)
138
+
139
+ TODO: configure to better play with focus: enter-on-focus to automatically "entered" without "action" and automatically "escape" on "blur" (focus will keep working on this case). Also is not clear how escape plays with change
140
+
141
+
142
+
143
+
144
+ ## Performance notes
145
+
146
+ * disabling renderer buffer speeds up rendering about 30%: `screen.renderer.no_buffer = true`. Genereally don't needed in production.
147
+
148
+
149
+ ## API example prototypes (WIP)
150
+
151
+ ** initial design stories**
152
+
153
+ ### layout
154
+
155
+ TODO / proposal
156
+
157
+ ```
158
+ require 'termgui'
159
+ class AppExplorer < Column
160
+ def initialize(model)
161
+ super
162
+ @model=model
163
+ @text = append_child(text: Textarea.new model.text, onChange: {|e| print e.key})
164
+ @text.onChange {|e| print e.key}
165
+ end
166
+ end
167
+ screen = Screen.new
168
+ main = Row.new
169
+ left = main.append_child(Column.new 0.3)
170
+ right = main.append_child(Column.new 0.7)
171
+ explorer = left.append_child(AppExplorer.new model)
172
+ editor = right.append_child(AppEditor.new model)
173
+ screen start
174
+ ```
175
+
176
+ ### structure
177
+
178
+ TODO / proposal
179
+
180
+ ```
181
+ class MyWidget < Column
182
+ def initialize
183
+ super 0.5
184
+ append_children [
185
+ {type: Row, height: 0.6, children: [
186
+ {type: Input, value: 'edit me', width: 0.5, onChange: {|e|print e} },
187
+ {type: Label, text: 'edit me'},
188
+ ]}
189
+ {type: Button, text: 'click me', onClick: {|e|print e}},
190
+ ]
191
+ end
192
+ end
193
+ ```
194
+
195
+ #### aside
196
+
197
+ ```
198
+ {type: Button, text: 'click me', onclick: {|e|print e}},
199
+ vs
200
+ Button.new text: 'click me', onclick: {|e|print e}},
201
+
202
+ {type: Row, height: 0.6, children: [
203
+ {type: Input, value: 'edit me', width: 0.5, onChange: {|e|print e} },
204
+ {type: Label, text: 'hello'},
205
+ ]}
206
+ vs
207
+ Row.new height: 0.6, children: [
208
+ Input.new value: 'edit me', width: 0.5, onChange: {|e|print e},
209
+ Label new: label: 'hello'
210
+ ]
211
+ ```
212
+
213
+ ### style
214
+
215
+ style = {
216
+ '.primary': {
217
+ bg: 'red',
218
+ fg: 'black'
219
+ bold: true
220
+ }
221
+ }
222
+ screen.append_child(Column.new children: [
223
+ Label.new text: 'are you sure?',
224
+ Button.new
225
+ ])
226
+
227
+
228
+ ### high level no layout
229
+
230
+ TODO / proposal
231
+
232
+ ```
233
+ s=Screen.new
234
+ b=Button.new(parent: s.document, width: 0.3, height: 0.3, left: 0, top: 0, label: 'click me', onClick: { |e| alert "#{e.target.label} clicked!" })
235
+ s.start
236
+ ```
237
+
238
+ ### example low level
239
+
240
+ (no HTML DOM at all, just drawing)
241
+ ```
242
+ screen = Screen.new
243
+ screen.renderer.rect(2,3,9,3,'-', {fg: 'yellow', bg: 'gray'})
244
+ screen.renderer.text(x: 3, y: 4,text; 'click me', style: Style.new(fg: '#ffee11', bg: 'black', bold: true))
245
+ ```
246
+
247
+ ### events
248
+
249
+ ```
250
+ screen=Screen.new
251
+ screen.event.add_listener('key', {|e| exit 0 if e.key=='q'})
252
+ renderer.text(text: 'press q to exit')
253
+ ```
254
+
255
+
256
+
257
+ ## Design
258
+
259
+ ### concepts
260
+
261
+ Screen: contains document, renderer, buffer, Input
262
+
263
+ Renderer: responsible of drawing given pixels to the terminal
264
+
265
+ Buffer: maintains screen as bitmap structure (so users can read the current screen contents like a bitmap)
266
+
267
+ Document: Node subclass analog to html's (access to parent screen)
268
+
269
+ Node: DOM like representation analog to html's (children, attributes)
270
+
271
+ Element: Node subclass analog to html's (border, margin, padding)
272
+
273
+ Input: responsible of user input - notifies screen - emitter
274
+
275
+ ## Summary
276
+
277
+ I'm author of npm.org/flor that although has superior terminal support (tput) I would like to re implement a similar library for ruby, writing it from scratch (currently learning ruby).
278
+
279
+ * low level html-canvas like to set attributes and write strings
280
+ * try to stick to html canvas api for Renderer
281
+ * user is responsible of setting the 'active style' like canvas' stroke-width - this simplifies renderer
282
+ * renderer of styled strings supporting cursor management,
283
+ * responsible of translating user's `{bg: 'red', s: 'hello'}` into a string with ansi codes
284
+ * screen maintains a virtual Buffer so current drawn screen can be accessed like a bitmap
285
+ * a DOM like API for children, attributes, box model, style
286
+ * supports user input events also like html dom EventSource (element.add_listener('key', ...))
287
+ * basic widget implementations: button,input,textarea
288
+ * style: fg, bg, ch, bold, etc.
289
+ * focus management: focused/focusable - element.style.focus
290
+ * input event loop : set_timeout
291
+ * easy keyboard event representation and API
292
+
293
+ ## Future
294
+
295
+ add features from npm.org/flor:
296
+
297
+ * scroll (element.scrollX=0.2) - dom support
298
+ * a xmlish syntax for defining GUI.
299
+ * support function attributes for event handlers as ruby fragments
300
+
301
+ ## Side projects
302
+
303
+ * cli/driver for ruby : for properly testing termgui we need cli-driver for ruby. see probes/stdin.rb for working exec and writing to process stdin async
304
+
305
+
306
+ ## Design notes
307
+
308
+ TODO
309
+
310
+ Screen, renderer, input are responsible of basic terminal styles like bg, fg, bold, etc.
311
+
312
+ On top of the screen, renderer and input a document object model like HTML DOM is supported. See Node, Element, Style, etc. Some features based on HTML supported are:
313
+
314
+ * box model similar
315
+ * children rendering
316
+ * text
317
+ * element query
318
+ * border
319
+ * padding
320
+
321
+ Some high level utilities, like the focus/action management, work on top of this DOM so probably 99% of users will want to go that way for building their GUIs.
data/lib/termgui.rb ADDED
@@ -0,0 +1 @@
1
+ require_relative '../src/termgui'
data/src/action.rb ADDED
@@ -0,0 +1,58 @@
1
+ require_relative 'event'
2
+ require_relative 'emitter'
3
+ require_relative 'util'
4
+
5
+ module TermGui
6
+ # action manager - it notifies focused elements on user input
7
+ # WIP
8
+ # defines action concept semantics like "action", "input"
9
+ # "focus": user press tab and it set focus on the next focusable element
10
+ # "enter": user focus a button and press enter or space. The button will receive an "action" event.
11
+ # "input": user focus a textarea, press enter ("action") and this enters in the "edit" mode
12
+ # meaning all user input now it's being notified ONLY to the textarea. Nor even the focus manager or
13
+ # others will be notified.
14
+ # "escape": when in a textarea "edit" mode, user press ESCAPE to exit it, meaning now user input will
15
+ # be notified to all listeners. Example: when in edit mode, pressing TAB will just print a tab in the
16
+ # textarea and it won't change focus as usual. For that to happen user presses escape to exit the "edit" mode.
17
+ # Attributes enabling each action:
18
+ # "enterable"
19
+ # "focusable"
20
+ # in the case of scape it will be enabled iff enterable is true
21
+ # For example a Label is not focusable or enterable. A Button is focusable but not enterable. A textatra is focusable and enterable.
22
+ class ActionManager < Emitter
23
+ attr_accessor :keys
24
+
25
+ def initialize(event: nil, focus: nil)
26
+ super()
27
+ @event = event
28
+ @focus = focus
29
+ @event.add_any_key_listener { |e| handle_key e }
30
+ install(:action)
31
+ @keys = ['enter']
32
+ end
33
+
34
+ def handle_key(e)
35
+ focused = @focus.focused
36
+ return unless focused && !focused.get_attribute('entered')
37
+
38
+ action_keys = to_array(focused.get_attribute('action-keys') || keys)
39
+ if action_keys.include?(e.key) && focused.get_attribute('focusable')
40
+ event = ActionEvent.new focused, e
41
+ focused_action = focused.get_attribute('action')
42
+ focused_action&.call(event)
43
+ trigger event.name, event
44
+ focused.trigger event.name, event
45
+ end
46
+ end
47
+ end
48
+
49
+ # An event representing an action, like a button "clicked"
50
+ class ActionEvent < NodeEvent
51
+ def initialize(target, original_event = nil)
52
+ super 'action', target, original_event
53
+ end
54
+ end
55
+ end
56
+
57
+ ActionEvent = TermGui::ActionEvent
58
+ ActionManager = TermGui::ActionManager
data/src/box.rb ADDED
@@ -0,0 +1,90 @@
1
+ BOXES = {
2
+ single: {
3
+ topLeft: '┌',
4
+ topRight: '┐',
5
+ bottomRight: '┘',
6
+ bottomLeft: '└',
7
+ vertical: '│',
8
+ horizontal: '─'
9
+ },
10
+ double: {
11
+ topLeft: '╔',
12
+ topRight: '╗',
13
+ bottomRight: '╝',
14
+ bottomLeft: '╚',
15
+ vertical: '║',
16
+ horizontal: '═'
17
+ },
18
+ round: {
19
+ topLeft: '╭',
20
+ topRight: '╮',
21
+ bottomRight: '╯',
22
+ bottomLeft: '╰',
23
+ vertical: '│',
24
+ horizontal: '─'
25
+ },
26
+ bold: {
27
+ topLeft: '┏',
28
+ topRight: '┓',
29
+ bottomRight: '┛',
30
+ bottomLeft: '┗',
31
+ vertical: '┃',
32
+ horizontal: '━'
33
+ },
34
+ single_double: {
35
+ topLeft: '╓',
36
+ topRight: '╖',
37
+ bottomRight: '╜',
38
+ bottomLeft: '╙',
39
+ vertical: '║',
40
+ horizontal: '─'
41
+ },
42
+ double_single: {
43
+ topLeft: '╒',
44
+ topRight: '╕',
45
+ bottomRight: '╛',
46
+ bottomLeft: '╘',
47
+ vertical: '│',
48
+ horizontal: '═'
49
+ },
50
+ classic: {
51
+ topLeft: '+',
52
+ topRight: '+',
53
+ bottomRight: '+',
54
+ bottomLeft: '+',
55
+ vertical: '|',
56
+ horizontal: '-'
57
+ }
58
+ }.freeze
59
+
60
+ def boxes
61
+ BOXES
62
+ end
63
+
64
+ def draw_box(width: 0, height: 0, style: :single, content: ' ')
65
+ box = BOXES[style ? style.to_sym : :single]
66
+ lines = []
67
+
68
+ (0..height - 1).each do |y|
69
+ line = []
70
+ (0..width - 1).each do |x|
71
+ line .push(if y.zero? && x.zero?
72
+ box[:topLeft]
73
+ elsif y == height - 1 && x.zero?
74
+ box[:bottomLeft]
75
+ elsif y.zero? && x == width - 1
76
+ box[:topRight]
77
+ elsif y == height - 1 && x == width - 1
78
+ box[:bottomRight]
79
+ elsif y == height - 1 || y.zero?
80
+ box[:horizontal]
81
+ elsif x == width - 1 || x.zero?
82
+ box[:vertical]
83
+ else
84
+ content
85
+ end)
86
+ end
87
+ lines.push(line.join(''))
88
+ end
89
+ lines
90
+ end
data/src/color.rb ADDED
@@ -0,0 +1,174 @@
1
+ # # # fast acceptable color comparision by myself
2
+ # # def color_distance1(c1, c2)
3
+ # # sum = 0
4
+ # # c1.each_index do |i|
5
+ # # sum += (c1[i].abs2 - c2[i].abs2).abs
6
+ # # end
7
+ # # sum
8
+ # # end
9
+
10
+ # def color_distance3(c1, c2)
11
+ # sum = 0
12
+ # coef = [1, 1, 1]
13
+ # c1.each_index do |i|
14
+ # sum += ((c1[i] - c2[i]) * coef[i]).abs2
15
+ # end
16
+ # sum
17
+ # end
18
+
19
+ # # // As it happens, comparing how similar two colors are is really hard. Here is
20
+ # # // one of the simplest solutions, which doesn't require conversion to another
21
+ # # // color space, posted on stackoverflow[1]. Maybe someone better at math can
22
+ # # // propose a superior solution.
23
+ # # // [1] http://stackoverflow.com/questions/1633828
24
+ # def color_distance2(c1, c2)
25
+ # # function colorDistance(r1, g1, b1, r2, g2, b2) {
26
+ # # return Math.pow(30 * (r1 - r2), 2)
27
+ # # + Math.pow(59 * (g1 - g2), 2)
28
+ # # + Math.pow(11 * (b1 - b2), 2);
29
+ # # }
30
+ # (30 * (c1[0] - c2[0])).abs2 + (59 * (c1[1] - c2[1])).abs2 + (11 * (c1[2] - c2[2])).abs2
31
+ # end
32
+
33
+ # # // This might work well enough for a terminal's colors: treat RGB as XYZ in a
34
+ # # // 3-dimensional space and go midway between the two points.
35
+ # # exports.mixColors = function(c1, c2, alpha) {
36
+ # # // if (c1 === 0x1ff) return c1;
37
+ # # // if (c2 === 0x1ff) return c1;
38
+ # # if (c1 === 0x1ff) c1 = 0;
39
+ # # if (c2 === 0x1ff) c2 = 0;
40
+ # # if (alpha == null) alpha = 0.5;
41
+
42
+ # # c1 = exports.vcolors[c1];
43
+ # # var r1 = c1[0];
44
+ # # var g1 = c1[1];
45
+ # # var b1 = c1[2];
46
+
47
+ # # c2 = exports.vcolors[c2];
48
+ # # var r2 = c2[0];
49
+ # # var g2 = c2[1];
50
+ # # var b2 = c2[2];
51
+
52
+ # # r1 += (r2 - r1) * alpha | 0;
53
+ # # g1 += (g2 - g1) * alpha | 0;
54
+ # # b1 += (b2 - b1) * alpha | 0;
55
+
56
+ # # return exports.match([r1, g1, b1]);
57
+ # # };
58
+
59
+ # # the rest is obsoleted by src/tco
60
+
61
+ # # color enumerations and utilities. See http://ascii-table.com/ansi-escape-sequences.phpsss
62
+ # require_relative 'key'
63
+
64
+ # # obsolete
65
+ # COLORS = {
66
+ # black: 0,
67
+ # red: 1,
68
+ # green: 2,
69
+ # yellow: 3,
70
+ # blue: 4,
71
+ # magenta: 5,
72
+ # cyan: 6,
73
+ # white: 7
74
+ # }.freeze
75
+
76
+ # # obsolete
77
+ # def color_names
78
+ # %w[black red green yellow blue magenta cyan white]
79
+ # end
80
+
81
+ # # obsolete
82
+ # def random_color
83
+ # color_names.sample
84
+ # end
85
+
86
+ # # obsolete
87
+ # def color_text(content, fg = nil, bg = nil)
88
+ # colored = color(fg, bg)
89
+ # colored << content
90
+ # colored << "#{CSI}0m"
91
+ # end
92
+
93
+ # # obsolete
94
+ # def color(fg = nil, bg = nil)
95
+ # colored = ''
96
+ # colored << color_to_escape(fg, 30) if fg
97
+ # colored << color_to_escape(bg, 40) if bg
98
+ # end
99
+
100
+ # # obsolete
101
+ # def color_to_escape(name, layer)
102
+ # short_name = name.to_s.sub(/\Abright_/, '')
103
+ # color = COLORS.fetch(short_name.to_sym)
104
+ # escape = "#{CSI}#{layer + color}"
105
+ # escape << ';1' if short_name.size < name.size
106
+ # escape << 'm'
107
+ # escape
108
+ # end
109
+
110
+ # # # https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_parameters
111
+ # # ATTRIBUTES = {
112
+ # # normal: 0,
113
+
114
+ # # bold: 1,
115
+ # # boldOff: 22,
116
+
117
+ # # faint: 2,
118
+ # # faintOff: 22,
119
+
120
+ # # italic: 3,
121
+ # # italicOff: 23,
122
+
123
+ # # underline: 4,
124
+ # # underlineOff: 24,
125
+
126
+ # # blink: 5,
127
+ # # slowBlink: 5,
128
+ # # rapidBlink: 6,
129
+ # # blinkOff: 25,
130
+ # # slowBlinkOff: 25,
131
+ # # rapidBlinkOff: 25,
132
+
133
+ # # inverse: 7,
134
+ # # inverseOff: 27,
135
+
136
+ # # defaultForeground: 39,
137
+ # # defaultBackground: 49,
138
+
139
+ # # invisible: 8,
140
+ # # invisibleOff: 28,
141
+
142
+ # # fraktur: 20,
143
+ # # frakturOff: 23,
144
+
145
+ # # framed: 51,
146
+ # # encircled: 52,
147
+ # # overlined: 53,
148
+
149
+ # # framedOff: 54,
150
+ # # encircledOff: 54
151
+ # # }.freeze
152
+
153
+ # # # Usage: screen.write attributes(blink: true).
154
+ # # # Attributes supported: bold, inverse, blink, slowBlink, rapidBlink, invisible, fraktur, framed, encircled, normal,
155
+ # # # italic, underline, faint
156
+ # # #
157
+ # # # Esc[Value;...;Valuem Set Graphics Mode:
158
+ # # # Calls the graphics functions specified by the following values.
159
+ # # # These specified functions remain active until the next occurrence of this escape sequence. Graphics mode changes the
160
+ # # # colors and attributes of text (such as bold and underline) displayed on the screen
161
+ # # def attributes(**args)
162
+ # # output = []
163
+ # # args.keys.each do |key|
164
+ # # if args[key] == true
165
+ # # output.push ATTRIBUTES[key].to_s
166
+ # # elsif args[key] == false
167
+ # # output.push ATTRIBUTES[:blinkOff].to_s if key == :blink
168
+ # # output.push "#{CSI}#{ATTRIBUTES[:boldOff]}" if key == :bold
169
+ # # # TODO: the rest
170
+ # # end
171
+ # # end
172
+ # # # p output
173
+ # # !output.empty? ? "#{CSI}#{output.join(';')}m" : ''
174
+ # # end