vedeu 0.6.1 → 0.6.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (138) hide show
  1. checksums.yaml +4 -4
  2. data/lib/vedeu/all.rb +2 -13
  3. data/lib/vedeu/api.rb +9 -9
  4. data/lib/vedeu/application/application_view.rb +3 -5
  5. data/lib/vedeu/bindings/bindings.rb +5 -1
  6. data/lib/vedeu/bindings/focus.rb +0 -5
  7. data/lib/vedeu/bindings/refresh.rb +0 -5
  8. data/lib/vedeu/bindings/system.rb +1 -6
  9. data/lib/vedeu/colours/all.rb +17 -0
  10. data/lib/vedeu/colours/background.rb +32 -28
  11. data/lib/vedeu/colours/backgrounds.rb +13 -9
  12. data/lib/vedeu/colours/colour.rb +106 -102
  13. data/lib/vedeu/colours/foreground.rb +32 -28
  14. data/lib/vedeu/colours/foregrounds.rb +13 -9
  15. data/lib/vedeu/colours/repository.rb +70 -0
  16. data/lib/vedeu/colours/translator.rb +267 -0
  17. data/lib/vedeu/configuration/api.rb +10 -7
  18. data/lib/vedeu/configuration/configuration.rb +2 -1
  19. data/lib/vedeu/dsl/border.rb +15 -2
  20. data/lib/vedeu/dsl/composition.rb +6 -4
  21. data/lib/vedeu/dsl/geometry.rb +14 -7
  22. data/lib/vedeu/dsl/group.rb +2 -2
  23. data/lib/vedeu/dsl/interface.rb +5 -3
  24. data/lib/vedeu/dsl/keymap.rb +9 -7
  25. data/lib/vedeu/dsl/line.rb +7 -4
  26. data/lib/vedeu/dsl/menu.rb +3 -0
  27. data/lib/vedeu/dsl/presentation.rb +20 -10
  28. data/lib/vedeu/dsl/shared.rb +4 -4
  29. data/lib/vedeu/dsl/stream.rb +2 -2
  30. data/lib/vedeu/dsl/text.rb +11 -5
  31. data/lib/vedeu/dsl/use.rb +2 -2
  32. data/lib/vedeu/dsl/view.rb +8 -6
  33. data/lib/vedeu/events/aliases.rb +91 -0
  34. data/lib/vedeu/events/all.rb +15 -0
  35. data/lib/vedeu/events/collection.rb +13 -0
  36. data/lib/vedeu/events/event.rb +244 -239
  37. data/lib/vedeu/events/repository.rb +30 -0
  38. data/lib/vedeu/events/trigger.rb +68 -64
  39. data/lib/vedeu/exceptions.rb +65 -54
  40. data/lib/vedeu/geometry/coordinate.rb +3 -1
  41. data/lib/vedeu/geometry/grid.rb +2 -4
  42. data/lib/vedeu/input/editor/all.rb +2 -1
  43. data/lib/vedeu/input/editor/{virtual_cursor.rb → cursor.rb} +10 -10
  44. data/lib/vedeu/input/editor/document.rb +7 -7
  45. data/lib/vedeu/input/editor/insert.rb +70 -0
  46. data/lib/vedeu/input/editor/line.rb +4 -16
  47. data/lib/vedeu/input/editor/lines.rb +2 -16
  48. data/lib/vedeu/input/input_translator.rb +2 -2
  49. data/lib/vedeu/input/key.rb +2 -2
  50. data/lib/vedeu/internal_api.rb +6 -6
  51. data/lib/vedeu/log/lockless_log_device.rb +0 -74
  52. data/lib/vedeu/models/focus.rb +3 -3
  53. data/lib/vedeu/models/interface.rb +3 -3
  54. data/lib/vedeu/models/menu.rb +2 -2
  55. data/lib/vedeu/models/views/char.rb +1 -1
  56. data/lib/vedeu/models/views/composition.rb +1 -1
  57. data/lib/vedeu/models/views/line.rb +1 -1
  58. data/lib/vedeu/models/views/stream.rb +1 -1
  59. data/lib/vedeu/models/views/view.rb +6 -7
  60. data/lib/vedeu/output/presentation/colour.rb +23 -21
  61. data/lib/vedeu/output/renderers/renderer_options.rb +2 -4
  62. data/lib/vedeu/output/text.rb +1 -1
  63. data/lib/vedeu/output/wordwrap.rb +18 -7
  64. data/lib/vedeu/plugins/plugin.rb +2 -2
  65. data/lib/vedeu/refresh/refresh_buffer.rb +3 -3
  66. data/lib/vedeu/refresh/refresh_group.rb +3 -3
  67. data/lib/vedeu/repositories/repository.rb +7 -6
  68. data/lib/vedeu/runtime/application.rb +1 -1
  69. data/lib/vedeu/runtime/main_loop.rb +3 -3
  70. data/lib/vedeu/runtime/router.rb +14 -11
  71. data/lib/vedeu/templating/helpers.rb +5 -5
  72. data/lib/vedeu/templating/template.rb +4 -4
  73. data/lib/vedeu/templating/view_template.rb +1 -1
  74. data/lib/vedeu/terminal/terminal.rb +2 -2
  75. data/lib/vedeu/version.rb +1 -1
  76. data/test/lib/vedeu/application/application_view_test.rb +1 -1
  77. data/test/lib/vedeu/colours/background_test.rb +93 -89
  78. data/test/lib/vedeu/colours/backgrounds_test.rb +11 -7
  79. data/test/lib/vedeu/colours/colour_test.rb +155 -151
  80. data/test/lib/vedeu/colours/foreground_test.rb +93 -89
  81. data/test/lib/vedeu/colours/foregrounds_test.rb +11 -7
  82. data/test/lib/vedeu/colours/repository_test.rb +97 -0
  83. data/test/lib/vedeu/colours/translator_test.rb +173 -0
  84. data/test/lib/vedeu/configuration/api_test.rb +4 -4
  85. data/test/lib/vedeu/dsl/border_test.rb +12 -0
  86. data/test/lib/vedeu/dsl/composition_test.rb +3 -3
  87. data/test/lib/vedeu/dsl/geometry_test.rb +11 -0
  88. data/test/lib/vedeu/dsl/group_test.rb +2 -2
  89. data/test/lib/vedeu/dsl/interface_test.rb +4 -4
  90. data/test/lib/vedeu/dsl/keymap_test.rb +8 -4
  91. data/test/lib/vedeu/dsl/line_test.rb +6 -3
  92. data/test/lib/vedeu/dsl/menu_test.rb +6 -0
  93. data/test/lib/vedeu/dsl/presentation_test.rb +16 -3
  94. data/test/lib/vedeu/dsl/stream_test.rb +1 -1
  95. data/test/lib/vedeu/dsl/text_test.rb +7 -0
  96. data/test/lib/vedeu/dsl/view_test.rb +3 -3
  97. data/test/lib/vedeu/events/aliases_test.rb +144 -0
  98. data/test/lib/vedeu/events/collection_test.rb +22 -0
  99. data/test/lib/vedeu/events/event_test.rb +84 -80
  100. data/test/lib/vedeu/events/repository_test.rb +28 -0
  101. data/test/lib/vedeu/events/trigger_test.rb +41 -37
  102. data/test/lib/vedeu/geometry/grid_test.rb +4 -4
  103. data/test/lib/vedeu/input/editor/{virtual_cursor_test.rb → cursor_test.rb} +3 -3
  104. data/test/lib/vedeu/input/editor/insert_test.rb +36 -0
  105. data/test/lib/vedeu/input/key_test.rb +1 -1
  106. data/test/lib/vedeu/models/cell_test.rb +1 -1
  107. data/test/lib/vedeu/models/focus_test.rb +4 -2
  108. data/test/lib/vedeu/models/menu_test.rb +1 -1
  109. data/test/lib/vedeu/models/views/char_test.rb +8 -4
  110. data/test/lib/vedeu/models/views/line_test.rb +4 -4
  111. data/test/lib/vedeu/models/views/stream_test.rb +3 -3
  112. data/test/lib/vedeu/models/views/view_test.rb +1 -1
  113. data/test/lib/vedeu/output/html_char_test.rb +4 -2
  114. data/test/lib/vedeu/output/presentation/colour_test.rb +5 -5
  115. data/test/lib/vedeu/output/presentation_test.rb +4 -4
  116. data/test/lib/vedeu/output/renderers/json_test.rb +4 -2
  117. data/test/lib/vedeu/plugins/plugin_test.rb +1 -1
  118. data/test/lib/vedeu/refresh/refresh_buffer_test.rb +1 -1
  119. data/test/lib/vedeu/refresh/refresh_group_test.rb +1 -1
  120. data/test/lib/vedeu/repositories/repository_test.rb +2 -2
  121. data/test/lib/vedeu/runtime/router_test.rb +6 -6
  122. data/test/lib/vedeu/templating/decoder_test.rb +2 -3
  123. data/test/lib/vedeu/templating/encoder_test.rb +2 -3
  124. data/test/lib/vedeu/templating/helpers_test.rb +6 -6
  125. data/test/lib/vedeu/templating/template_test.rb +2 -2
  126. data/test/lib/vedeu/templating/view_template_test.rb +13 -7
  127. data/test/lib/vedeu/terminal/terminal_test.rb +3 -3
  128. metadata +24 -19
  129. data/lib/vedeu/colours/colour_translator.rb +0 -260
  130. data/lib/vedeu/colours/colours.rb +0 -66
  131. data/lib/vedeu/events/event_aliases.rb +0 -87
  132. data/lib/vedeu/events/event_collection.rb +0 -9
  133. data/lib/vedeu/events/events.rb +0 -26
  134. data/test/lib/vedeu/colours/colour_translator_test.rb +0 -169
  135. data/test/lib/vedeu/colours/colours_test.rb +0 -93
  136. data/test/lib/vedeu/events/event_aliases_test.rb +0 -140
  137. data/test/lib/vedeu/events/event_collection_test.rb +0 -18
  138. data/test/lib/vedeu/events/events_test.rb +0 -24
@@ -1,32 +1,36 @@
1
1
  module Vedeu
2
2
 
3
- # The class represents one half (the other, can be found at
4
- # {Vedeu::Background}) of a terminal colour escape sequence.
5
- #
6
- class Foreground < Vedeu::ColourTranslator
7
-
8
- private
9
-
10
- # @return [String]
11
- def named_codes
12
- Vedeu::Esc.foreground_codes[colour]
13
- end
14
-
15
- # @return [String]
16
- def numbered_prefix
17
- "\e[38;5;"
18
- end
19
-
20
- # @return [Vedeu::Foregrounds]
21
- def repository
22
- Vedeu.foreground_colours
23
- end
24
-
25
- # @return [String]
26
- def rgb_prefix
27
- "\e[38;2;%s;%s;%sm"
28
- end
29
-
30
- end # Foreground
3
+ module Colours
4
+
5
+ # The class represents one half (the other, can be found at
6
+ # {Vedeu::Colours::Background}) of a terminal colour escape sequence.
7
+ #
8
+ class Foreground < Vedeu::Colours::Translator
9
+
10
+ private
11
+
12
+ # @return [String]
13
+ def named_codes
14
+ Vedeu::Esc.foreground_codes[colour]
15
+ end
16
+
17
+ # @return [String]
18
+ def numbered_prefix
19
+ "\e[38;5;"
20
+ end
21
+
22
+ # @return [Vedeu::Colours::Foregrounds]
23
+ def repository
24
+ Vedeu.foreground_colours
25
+ end
26
+
27
+ # @return [String]
28
+ def rgb_prefix
29
+ "\e[38;2;%s;%s;%sm"
30
+ end
31
+
32
+ end # Foreground
33
+
34
+ end # Colours
31
35
 
32
36
  end # Vedeu
@@ -1,15 +1,19 @@
1
1
  module Vedeu
2
2
 
3
- # Store foreground colour escape sequences by HTML/CSS code.
4
- #
5
- class Foregrounds < Colours
3
+ module Colours
6
4
 
7
- # @return [Vedeu::Foregrounds]
8
- # @see Vedeu::Repository
9
- def self.foreground_colours
10
- @foreground_colours ||= new
11
- end
5
+ # Store foreground colour escape sequences by HTML/CSS code.
6
+ #
7
+ class Foregrounds < Vedeu::Colours::Repository
12
8
 
13
- end # Foregrounds
9
+ # @return [Vedeu::Colours::Foregrounds]
10
+ # @see Vedeu::Repository
11
+ def self.foreground_colours
12
+ @foreground_colours ||= new
13
+ end
14
+
15
+ end # Foregrounds
16
+
17
+ end # Colours
14
18
 
15
19
  end # Vedeu
@@ -0,0 +1,70 @@
1
+ module Vedeu
2
+
3
+ module Colours
4
+
5
+ # Allows the storing of HTML/CSS colours and their respective escape
6
+ # sequences.
7
+ #
8
+ class Repository
9
+
10
+ # @!attribute [r] storage
11
+ # @return [Hash<String => String>]
12
+ attr_reader :storage
13
+
14
+ # Returns a new instance of Vedeu::Colours::Repository.
15
+ #
16
+ # @return [Vedeu::Colours::Repository]
17
+ def initialize
18
+ @storage = {}
19
+ end
20
+
21
+ # Registers a colour with respective escape sequence.
22
+ #
23
+ # @return [String]
24
+ def register(colour, escape_sequence)
25
+ storage.store(colour, escape_sequence)
26
+ end
27
+
28
+ # Returns a boolean indicating whether the colour has been registered.
29
+ #
30
+ # @param colour [String]
31
+ # @return [Boolean]
32
+ def registered?(colour)
33
+ return false unless colour
34
+
35
+ storage.key?(colour)
36
+ end
37
+
38
+ # Removes all stored colours.
39
+ #
40
+ # @return [Hash]
41
+ def reset!
42
+ storage.clear
43
+ end
44
+
45
+ # Retrieves the escape sequence of a registered colour.
46
+ #
47
+ # @return [String]
48
+ def retrieve(colour)
49
+ storage.fetch(colour, '')
50
+ end
51
+
52
+ # Retrieves the escape sequence of a registered colour, or registers the
53
+ # colour with its respective escape sequence.
54
+ #
55
+ # @return [String]
56
+ def retrieve_or_register(colour, escape_sequence)
57
+ if registered?(colour)
58
+ retrieve(colour)
59
+
60
+ else
61
+ register(colour, escape_sequence)
62
+
63
+ end
64
+ end
65
+
66
+ end # Repository
67
+
68
+ end # Colours
69
+
70
+ end # Vedeu
@@ -0,0 +1,267 @@
1
+ module Vedeu
2
+
3
+ module Colours
4
+
5
+ # Convert a CSS/HTML colour string into a terminal escape sequence.
6
+ #
7
+ # If provided with an empty value or a string it cannot convert, it will
8
+ # return an empty string.
9
+ #
10
+ # When provided with a named colour, uses the terminal's value for that
11
+ # colour. If a theme is being used with the terminal, which overrides the
12
+ # defaults, then the theme's colour will be used. The recognised names are:
13
+ # :black, :red, :green, :yellow, :blue, :magenta, :cyan, :white, :default.
14
+ #
15
+ # When a number between 0 and 255 is provided, Vedeu will use the terminal
16
+ # colour corresponding with that colour.
17
+ #
18
+ # Finally, when provided a CSS/HTML colour string e.g. '#ff0000', Vedeu will
19
+ # translate that to the 8-bit escape sequence or when you have a capable
20
+ # terminal and the `TERM=xterm-truecolor` environment variable set,
21
+ # a 24-bit representation.
22
+ #
23
+ # @todo More documentation required (create a fancy chart!)
24
+ #
25
+ class Translator
26
+
27
+ # @!attribute [r] colour
28
+ # @return [String]
29
+ attr_reader :colour
30
+ alias_method :value, :colour
31
+
32
+ # Produces new objects of the correct class from the value, ignores
33
+ # objects Colours::that have already been coerced.
34
+ #
35
+ # @param value [Object|NilClass]
36
+ # @return [Object]
37
+ def self.coerce(value)
38
+ if value.is_a?(self)
39
+ value
40
+
41
+ else
42
+ new(value)
43
+
44
+ end
45
+ end
46
+
47
+ # Return a new instance of Vedeu::Colours::Translator.
48
+ #
49
+ # @param colour [Fixnum|String|Symbol]
50
+ # @return [Vedeu::Colours::Translator]
51
+ def initialize(colour = '')
52
+ @colour = colour || ''
53
+ end
54
+
55
+ # @return [Boolean]
56
+ def empty?
57
+ colour.nil? || colour.to_s.empty?
58
+ end
59
+
60
+ # An object is equal when its values are the same.
61
+ #
62
+ # @param other [Vedeu::Views::Char]
63
+ # @return [Boolean]
64
+ def eql?(other)
65
+ self.class == other.class && colour == other.colour
66
+ end
67
+ alias_method :==, :eql?
68
+
69
+ # @return [String]
70
+ # @see Vedeu::Colours::Translator
71
+ def escape_sequence
72
+ if empty?
73
+ ''
74
+
75
+ elsif registered?(colour)
76
+ retrieve(colour)
77
+
78
+ elsif rgb?
79
+ rgb
80
+
81
+ elsif numbered?
82
+ numbered
83
+
84
+ elsif named?
85
+ named
86
+
87
+ else
88
+ ''
89
+
90
+ end
91
+ end
92
+ alias_method :to_s, :escape_sequence
93
+ alias_method :to_str, :escape_sequence
94
+
95
+ # @return [String]
96
+ def to_html
97
+ return colour if rgb?
98
+
99
+ ''
100
+ end
101
+
102
+ private
103
+
104
+ # Retrieves the escape sequence for the HTML/CSS colour code.
105
+ #
106
+ # @param colour [String]
107
+ # @return [String]
108
+ def retrieve(colour)
109
+ repository.retrieve(colour)
110
+ end
111
+
112
+ # Registers a HTML/CSS colour code and escape sequence to reduce
113
+ # processing.
114
+ #
115
+ # @param colour [String] A HTML/CSS colour code.
116
+ # @param escape_sequence [String] The HTML/CSS colour code as an escape
117
+ # sequence.
118
+ # @return [String]
119
+ def register(colour, escape_sequence)
120
+ repository.register(colour, escape_sequence)
121
+ end
122
+
123
+ # Returns a boolean indicating the HTML/CSS colour code has been
124
+ # registered.
125
+ #
126
+ # @param colour [String]
127
+ # @return [Boolean]
128
+ def registered?(colour)
129
+ repository.registered?(colour)
130
+ end
131
+
132
+ # @return [Boolean]
133
+ def named?
134
+ colour.is_a?(Symbol) && valid_name?
135
+ end
136
+
137
+ # Returns an escape sequence for a named background colour.
138
+ #
139
+ # @note
140
+ # Valid names can be found at {Vedeu::Esc#codes}
141
+ #
142
+ # @return [String]
143
+ def named
144
+ "\e[#{named_codes}m"
145
+ end
146
+
147
+ # Returns a boolean indicating whether the colour provided is a valid
148
+ # named colour.
149
+ #
150
+ # @return [Boolean]
151
+ def valid_name?
152
+ Vedeu::Esc.codes.keys.include?(colour)
153
+ end
154
+
155
+ # Returns a boolean indicating whether the colour provided is a terminal
156
+ # numbered colour.
157
+ #
158
+ # @return [Boolean]
159
+ def numbered?
160
+ colour.is_a?(Fixnum) && valid_range?
161
+ end
162
+
163
+ # Returns an escape sequence.
164
+ #
165
+ # @return [String]
166
+ def numbered
167
+ "#{numbered_prefix}#{css_to_numbered}m"
168
+ end
169
+
170
+ # Returns a boolean indicated whether the colour is a valid HTML/CSS
171
+ # colour.
172
+ #
173
+ # @return [Boolean]
174
+ def rgb?
175
+ return true if colour =~ /^#([A-Fa-f0-9]{6})$/
176
+
177
+ false
178
+ end
179
+
180
+ # Returns an escape sequence.
181
+ #
182
+ # @return [String]
183
+ def rgb
184
+ if Vedeu::Configuration.colour_mode == 16_777_216
185
+ register(colour, format(rgb_prefix, *css_to_rgb))
186
+
187
+ else
188
+ numbered
189
+
190
+ end
191
+ end
192
+
193
+ # Returns a boolean indicating whether the numbered colour is within the
194
+ # range of valid terminal numbered colours.
195
+ #
196
+ # @return [Boolean]
197
+ def valid_range?
198
+ colour >= 0 && colour <= 255
199
+ end
200
+
201
+ # Returns a collection of converted HTML/CSS octets as their decimal
202
+ # equivalents.
203
+ #
204
+ # @example
205
+ # colour = '#aadd55'
206
+ # css_to_rgb # => [170, 221, 85]
207
+ #
208
+ # @return [Array]
209
+ def css_to_rgb
210
+ [
211
+ colour[1..2].to_i(16),
212
+ colour[3..4].to_i(16),
213
+ colour[5..6].to_i(16),
214
+ ]
215
+ end
216
+
217
+ # @return [Fixnum]
218
+ def css_to_numbered
219
+ if rgb?
220
+ [16, red, green, blue].inject(:+)
221
+
222
+ elsif numbered?
223
+ colour
224
+
225
+ end
226
+ end
227
+
228
+ # Takes the red component of {#css_to_rgb} and converts to the correct
229
+ # value for setting the terminal red value.
230
+ #
231
+ # @return [Fixnum]
232
+ def red
233
+ (css_to_rgb[0] / 51) * 36
234
+ end
235
+
236
+ # Takes the green component of {#css_to_rgb} and converts to the correct
237
+ # value for setting the terminal green value.
238
+ #
239
+ # @return [Fixnum]
240
+ def green
241
+ (css_to_rgb[1] / 51) * 6
242
+ end
243
+
244
+ # Takes the blue component of {#css_to_rgb} and converts to the correct
245
+ # value for setting the terminal blue value.
246
+ #
247
+ # @return [Fixnum]
248
+ def blue
249
+ (css_to_rgb[2] / 51) * 1
250
+ end
251
+
252
+ # @raise [Vedeu::Error::NotImplemented] Subclasses of this class must
253
+ # implement this method.
254
+ # @return [Vedeu::Error::NotImplemented]
255
+ def not_implemented
256
+ fail Vedeu::Error::NotImplemented, 'Subclasses implement this.'
257
+ end
258
+ alias_method :named_codes, :not_implemented
259
+ alias_method :numbered_prefix, :not_implemented
260
+ alias_method :repository, :not_implemented
261
+ alias_method :rgb_prefix, :not_implemented
262
+
263
+ end # Translator
264
+
265
+ end # Colours
266
+
267
+ end # Vedeu
@@ -250,12 +250,15 @@ module Vedeu
250
250
  # the colour mode at all and allow it to be detected, or use 256 here.
251
251
  #
252
252
  # @param value [Fixnum]
253
- # @raise [Vedeu::InvalidSyntax] When the value parameter is not one of
254
- # +8+, +16+, +256+ or +16777216+.
253
+ # @raise [Vedeu::Error::InvalidSyntax] When the value parameter is not one
254
+ # of +8+, +16+, +256+ or +16777216+.
255
255
  # @return [Boolean]
256
256
  def colour_mode(value = nil)
257
- fail Vedeu::InvalidSyntax, '`colour_mode` must be `8`, `16`, `256`, ' \
258
- '`16777216`.' unless valid_colour_mode?(value)
257
+ unless valid_colour_mode?(value)
258
+ fail Vedeu::Error::InvalidSyntax,
259
+ '`colour_mode` must be `8`, `16`, `256`, `16777216`.'
260
+ end
261
+
259
262
  options[:colour_mode] = value
260
263
  end
261
264
 
@@ -422,8 +425,8 @@ module Vedeu
422
425
  # end
423
426
  #
424
427
  # @param mode [Symbol] Either ':cooked', ':fake' or ':raw'.
425
- # @raise [Vedeu::InvalidSyntax] When the mode is not ':cooked', ':fake' or
426
- # ':raw'.
428
+ # @raise [Vedeu::Error::InvalidSyntax] When the mode is not ':cooked',
429
+ # ':fake' or ':raw'.
427
430
  # @return [Symbol]
428
431
  # @see Vedeu::Config::API#cooked!
429
432
  # @see Vedeu::Config::API#fake!
@@ -433,7 +436,7 @@ module Vedeu
433
436
  options[:terminal_mode] = mode
434
437
 
435
438
  else
436
- fail Vedeu::InvalidSyntax,
439
+ fail Vedeu::Error::InvalidSyntax,
437
440
  'Terminal mode can be set to either :cooked, :fake or :raw'
438
441
 
439
442
  end