vedeu 0.7.4 → 0.8.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (277) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +3 -0
  3. data/docs/api.md +5 -1
  4. data/docs/borders.md +2 -26
  5. data/docs/dsl.md +4 -4
  6. data/docs/dsl_elements.md +100 -0
  7. data/docs/events/by_name/action.md +3 -0
  8. data/docs/events/by_name/cursor_hide.md +3 -0
  9. data/docs/events/by_name/cursor_reset.md +3 -0
  10. data/docs/events/by_name/cursor_show.md +3 -0
  11. data/docs/events/by_name/geometry_down.md +3 -0
  12. data/docs/events/by_name/geometry_left.md +3 -0
  13. data/docs/events/by_name/geometry_right.md +3 -0
  14. data/docs/events/by_name/geometry_up.md +3 -0
  15. data/docs/events/by_name/hide_view.md +3 -0
  16. data/docs/events/by_name/set_border_caption.md +13 -0
  17. data/docs/events/by_name/set_border_title.md +13 -0
  18. data/docs/events/by_name/show_view.md +3 -0
  19. data/docs/events/by_name/standalone.md +3 -0
  20. data/docs/events/by_name/toggle_view.md +3 -0
  21. data/docs/events/by_name/view_down.md +4 -0
  22. data/docs/events/by_name/view_left.md +4 -0
  23. data/docs/events/by_name/view_right.md +4 -0
  24. data/docs/events/by_name/view_up.md +4 -0
  25. data/examples/views/empty_template.rb +46 -0
  26. data/examples/views/expected/line.out +1 -0
  27. data/examples/views/expected/lines.out +1 -0
  28. data/examples/views/expected/lines_alignment.out +1 -0
  29. data/examples/views/line.rb +220 -0
  30. data/examples/views/lines.rb +281 -0
  31. data/examples/views/lines_alignment.rb +231 -0
  32. data/examples/views/lines_line_stream.rb +99 -0
  33. data/examples/views/lines_line_streams.rb +73 -0
  34. data/examples/views/lines_line_streams_stream.rb +83 -0
  35. data/examples/views/lines_line_streams_stream_stream.rb +53 -0
  36. data/examples/views/lines_streams.rb +50 -0
  37. data/examples/views/lines_streams_stream.rb +51 -0
  38. data/examples/views/runner.sh +27 -0
  39. data/lib/vedeu/all.rb +6 -2
  40. data/lib/vedeu/application/controller.rb +1 -1
  41. data/lib/vedeu/boolean.rb +35 -0
  42. data/lib/vedeu/borders/border.rb +3 -3
  43. data/lib/vedeu/borders/caption.rb +1 -1
  44. data/lib/vedeu/borders/dsl.rb +1 -20
  45. data/lib/vedeu/borders/title.rb +6 -8
  46. data/lib/vedeu/buffers/buffer.rb +2 -2
  47. data/lib/vedeu/buffers/refresh.rb +1 -1
  48. data/lib/vedeu/cells/border.rb +0 -11
  49. data/lib/vedeu/cells/empty.rb +4 -4
  50. data/lib/vedeu/{geometries/alignment → coercers}/alignment.rb +50 -53
  51. data/lib/vedeu/coercers/all.rb +16 -0
  52. data/lib/vedeu/coercers/colour.rb +78 -0
  53. data/lib/vedeu/coercers/colour_attributes.rb +122 -0
  54. data/lib/vedeu/coercers/horizontal_alignment.rb +28 -0
  55. data/lib/vedeu/coercers/vertical_alignment.rb +27 -0
  56. data/lib/vedeu/colours/all.rb +1 -0
  57. data/lib/vedeu/colours/background.rb +13 -2
  58. data/lib/vedeu/colours/colour.rb +16 -31
  59. data/lib/vedeu/colours/foreground.rb +13 -2
  60. data/lib/vedeu/colours/translator.rb +21 -59
  61. data/lib/vedeu/colours/validator.rb +84 -0
  62. data/lib/vedeu/common.rb +52 -0
  63. data/lib/vedeu/configuration/api.rb +4 -10
  64. data/lib/vedeu/configuration/configuration.rb +4 -4
  65. data/lib/vedeu/cursors/coordinate.rb +1 -1
  66. data/lib/vedeu/cursors/cursor.rb +1 -1
  67. data/lib/vedeu/cursors/dsl.rb +1 -28
  68. data/lib/vedeu/dsl/all.rb +7 -7
  69. data/lib/vedeu/dsl/attributes.rb +224 -0
  70. data/lib/vedeu/dsl/border.rb +127 -0
  71. data/lib/vedeu/dsl/cursors.rb +44 -0
  72. data/lib/vedeu/dsl/dsl.rb +9 -0
  73. data/lib/vedeu/dsl/elements.rb +335 -0
  74. data/lib/vedeu/dsl/geometry.rb +113 -0
  75. data/lib/vedeu/dsl/string/align.rb +127 -0
  76. data/lib/vedeu/dsl/string/all.rb +14 -0
  77. data/lib/vedeu/dsl/string/text.rb +107 -0
  78. data/lib/vedeu/dsl/string/truncate.rb +111 -0
  79. data/lib/vedeu/{output → dsl/string}/wordwrap.rb +10 -6
  80. data/lib/vedeu/dsl/view.rb +124 -248
  81. data/lib/vedeu/dsl/views.rb +244 -0
  82. data/lib/vedeu/editor/cursor.rb +1 -1
  83. data/lib/vedeu/editor/delete.rb +3 -1
  84. data/lib/vedeu/editor/insert.rb +22 -6
  85. data/lib/vedeu/editor/line.rb +15 -9
  86. data/lib/vedeu/editor/lines.rb +27 -18
  87. data/lib/vedeu/esc/actions.rb +73 -26
  88. data/lib/vedeu/esc/all.rb +2 -0
  89. data/lib/vedeu/esc/background.rb +219 -0
  90. data/lib/vedeu/esc/borders.rb +72 -26
  91. data/lib/vedeu/esc/colours.rb +26 -27
  92. data/lib/vedeu/esc/esc.rb +4 -4
  93. data/lib/vedeu/esc/foreground.rb +219 -0
  94. data/lib/vedeu/esc/mouse.rb +16 -13
  95. data/lib/vedeu/events/aliases.rb +2 -2
  96. data/lib/vedeu/events/event.rb +1 -1
  97. data/lib/vedeu/geometries/all.rb +0 -1
  98. data/lib/vedeu/geometries/area/dimension.rb +2 -2
  99. data/lib/vedeu/geometries/dsl/dsl.rb +3 -51
  100. data/lib/vedeu/geometries/dsl/validator.rb +4 -4
  101. data/lib/vedeu/geometries/geometry.rb +10 -10
  102. data/lib/vedeu/geometries/move.rb +4 -4
  103. data/lib/vedeu/groups/dsl.rb +1 -0
  104. data/lib/vedeu/groups/group.rb +1 -1
  105. data/lib/vedeu/input/all.rb +1 -0
  106. data/lib/vedeu/input/capture.rb +14 -10
  107. data/lib/vedeu/input/dsl.rb +20 -26
  108. data/lib/vedeu/input/key.rb +0 -6
  109. data/lib/vedeu/input/keymap.rb +4 -9
  110. data/lib/vedeu/input/keys.rb +13 -0
  111. data/lib/vedeu/input/mapper.rb +29 -14
  112. data/lib/vedeu/input/read.rb +4 -2
  113. data/lib/vedeu/interfaces/dsl.rb +23 -22
  114. data/lib/vedeu/interfaces/interface.rb +2 -2
  115. data/lib/vedeu/logging/debug.rb +33 -0
  116. data/lib/vedeu/logging/log.rb +42 -33
  117. data/lib/vedeu/menus/dsl.rb +7 -18
  118. data/lib/vedeu/menus/menu.rb +1 -1
  119. data/lib/vedeu/models/focus.rb +1 -1
  120. data/lib/vedeu/models/toggleable.rb +4 -4
  121. data/lib/vedeu/null/generic.rb +2 -2
  122. data/lib/vedeu/output/all.rb +0 -2
  123. data/lib/vedeu/output/compressor_cache.rb +1 -1
  124. data/lib/vedeu/output/output.rb +5 -20
  125. data/lib/vedeu/presentation/colour.rb +39 -7
  126. data/lib/vedeu/presentation/style.rb +25 -11
  127. data/lib/vedeu/renderers/html.rb +11 -11
  128. data/lib/vedeu/repositories/assemblage.rb +9 -0
  129. data/lib/vedeu/repositories/collection.rb +1 -0
  130. data/lib/vedeu/repositories/defaults.rb +4 -1
  131. data/lib/vedeu/repositories/model.rb +7 -37
  132. data/lib/vedeu/repositories/parent.rb +6 -6
  133. data/lib/vedeu/repositories/registerable.rb +16 -2
  134. data/lib/vedeu/repositories/repositories.rb +1 -1
  135. data/lib/vedeu/repositories/repository.rb +2 -12
  136. data/lib/vedeu/runtime/flags.rb +1 -1
  137. data/lib/vedeu/terminal/terminal.rb +7 -2
  138. data/lib/vedeu/version.rb +1 -1
  139. data/lib/vedeu/views/all.rb +4 -1
  140. data/lib/vedeu/views/char.rb +5 -4
  141. data/lib/vedeu/views/chars.rb +42 -0
  142. data/lib/vedeu/views/composition.rb +15 -30
  143. data/lib/vedeu/views/default_attributes.rb +74 -0
  144. data/lib/vedeu/views/html_char.rb +2 -2
  145. data/lib/vedeu/views/line.rb +27 -53
  146. data/lib/vedeu/views/lines.rb +31 -0
  147. data/lib/vedeu/views/stream.rb +31 -78
  148. data/lib/vedeu/views/streams.rb +42 -0
  149. data/lib/vedeu/views/value.rb +150 -0
  150. data/lib/vedeu/views/view.rb +32 -36
  151. data/lib/vedeu/views/{view_collection.rb → views.rb} +2 -2
  152. data/test/lib/vedeu/application/application_controller_test.rb +5 -0
  153. data/test/lib/vedeu/application/controller_test.rb +22 -28
  154. data/test/lib/vedeu/boolean_test.rb +57 -0
  155. data/test/lib/vedeu/borders/border_test.rb +130 -36
  156. data/test/lib/vedeu/borders/dsl_test.rb +63 -10
  157. data/test/lib/vedeu/borders/title_test.rb +16 -5
  158. data/test/lib/vedeu/buffers/buffer_test.rb +26 -10
  159. data/test/lib/vedeu/buffers/empty_test.rb +1 -1
  160. data/test/lib/vedeu/cells/border_test.rb +0 -6
  161. data/test/lib/vedeu/cells/clear_test.rb +2 -0
  162. data/test/lib/vedeu/cells/empty_test.rb +12 -2
  163. data/test/lib/vedeu/cells/escape_test.rb +7 -2
  164. data/test/lib/vedeu/{geometries/alignment → coercers}/alignment_test.rb +44 -19
  165. data/test/lib/vedeu/coercers/colour_attributes_test.rb +168 -0
  166. data/test/lib/vedeu/coercers/colour_test.rb +75 -0
  167. data/test/lib/vedeu/{geometries/alignment → coercers}/horizontal_alignment_test.rb +6 -6
  168. data/test/lib/vedeu/{geometries/alignment → coercers}/vertical_alignment_test.rb +6 -6
  169. data/test/lib/vedeu/colours/background_test.rb +17 -1
  170. data/test/lib/vedeu/colours/colour_test.rb +74 -2
  171. data/test/lib/vedeu/colours/foreground_test.rb +17 -1
  172. data/test/lib/vedeu/colours/repository_test.rb +1 -1
  173. data/test/lib/vedeu/colours/translator_test.rb +8 -1
  174. data/test/lib/vedeu/colours/validator_test.rb +60 -0
  175. data/test/lib/vedeu/common_test.rb +138 -0
  176. data/test/lib/vedeu/configuration/api_test.rb +96 -51
  177. data/test/lib/vedeu/cursors/coordinate_test.rb +58 -117
  178. data/test/lib/vedeu/cursors/cursor_test.rb +46 -17
  179. data/test/lib/vedeu/cursors/dsl_test.rb +19 -5
  180. data/test/lib/vedeu/distributed/client_test.rb +8 -3
  181. data/test/lib/vedeu/distributed/server_test.rb +8 -0
  182. data/test/lib/vedeu/distributed/test_application_test.rb +62 -19
  183. data/test/lib/vedeu/distributed/uri_test.rb +4 -2
  184. data/test/lib/vedeu/dsl/attributes_test.rb +212 -0
  185. data/test/lib/vedeu/dsl/border_test.rb +51 -0
  186. data/test/lib/vedeu/dsl/{lines_test.rb → cursors_test.rb} +2 -2
  187. data/test/lib/vedeu/dsl/dsl_test.rb +44 -0
  188. data/test/lib/vedeu/dsl/elements_test.rb +118 -0
  189. data/test/lib/vedeu/dsl/geometry_test.rb +35 -0
  190. data/test/lib/vedeu/dsl/presentation_test.rb +52 -11
  191. data/test/lib/vedeu/dsl/string/align_test.rb +147 -0
  192. data/test/lib/vedeu/dsl/string/text_test.rb +213 -0
  193. data/test/lib/vedeu/dsl/string/truncate_test.rb +97 -0
  194. data/test/lib/vedeu/{output → dsl/string}/wordwrap_test.rb +3 -3
  195. data/test/lib/vedeu/dsl/view_test.rb +30 -42
  196. data/test/lib/vedeu/dsl/views_test.rb +61 -0
  197. data/test/lib/vedeu/editor/cursor_test.rb +30 -12
  198. data/test/lib/vedeu/editor/document_test.rb +18 -10
  199. data/test/lib/vedeu/editor/line_test.rb +4 -1
  200. data/test/lib/vedeu/editor/lines_test.rb +11 -3
  201. data/test/lib/vedeu/esc/actions_test.rb +42 -4
  202. data/test/lib/vedeu/esc/background_test.rb +185 -0
  203. data/test/lib/vedeu/esc/borders_test.rb +37 -9
  204. data/test/lib/vedeu/esc/colours_test.rb +0 -59
  205. data/test/lib/vedeu/esc/foreground_test.rb +185 -0
  206. data/test/lib/vedeu/esc/mouse_test.rb +9 -4
  207. data/test/lib/vedeu/geometries/area/area_test.rb +34 -11
  208. data/test/lib/vedeu/geometries/dsl/dsl_test.rb +52 -61
  209. data/test/lib/vedeu/geometries/geometry_test.rb +62 -18
  210. data/test/lib/vedeu/geometries/position_test.rb +14 -7
  211. data/test/lib/vedeu/groups/dsl_test.rb +24 -15
  212. data/test/lib/vedeu/groups/group_test.rb +18 -8
  213. data/test/lib/vedeu/input/dsl_test.rb +33 -16
  214. data/test/lib/vedeu/input/key_test.rb +8 -1
  215. data/test/lib/vedeu/input/keymap_test.rb +4 -1
  216. data/test/lib/vedeu/input/keys_test.rb +16 -0
  217. data/test/lib/vedeu/interfaces/dsl_test.rb +24 -30
  218. data/test/lib/vedeu/interfaces/interface_test.rb +79 -24
  219. data/test/lib/vedeu/logging/ips_test.rb +10 -1
  220. data/test/lib/vedeu/menus/dsl_test.rb +24 -23
  221. data/test/lib/vedeu/menus/menu_test.rb +18 -8
  222. data/test/lib/vedeu/models/page_test.rb +5 -1
  223. data/test/lib/vedeu/models/row_test.rb +4 -0
  224. data/test/lib/vedeu/models/toggleable_test.rb +10 -6
  225. data/test/lib/vedeu/null/generic_test.rb +90 -1
  226. data/test/lib/vedeu/output/viewport_test.rb +11 -10
  227. data/test/lib/vedeu/plugins/plugin_test.rb +22 -9
  228. data/test/lib/vedeu/presentation/colour_test.rb +2 -1
  229. data/test/lib/vedeu/presentation/presentation_test.rb +13 -8
  230. data/test/lib/vedeu/presentation/style_test.rb +28 -13
  231. data/test/lib/vedeu/renderers/file_test.rb +4 -2
  232. data/test/lib/vedeu/renderers/options_test.rb +1 -1
  233. data/test/lib/vedeu/repositories/assemblage_test.rb +18 -0
  234. data/test/lib/vedeu/repositories/collection_test.rb +34 -10
  235. data/test/lib/vedeu/repositories/defaults_test.rb +6 -0
  236. data/test/lib/vedeu/repositories/model_test.rb +4 -13
  237. data/test/lib/vedeu/repositories/repository_test.rb +12 -5
  238. data/test/lib/vedeu/repositories/store_test.rb +9 -0
  239. data/test/lib/vedeu/runtime/flags_test.rb +6 -4
  240. data/test/lib/vedeu/runtime/launcher_test.rb +1 -1
  241. data/test/lib/vedeu/templating/helpers_test.rb +93 -65
  242. data/test/lib/vedeu/templating/view_template_test.rb +26 -26
  243. data/test/lib/vedeu/views/char_test.rb +36 -11
  244. data/test/lib/vedeu/views/chars_test.rb +71 -0
  245. data/test/lib/vedeu/views/composition_test.rb +17 -16
  246. data/test/lib/vedeu/views/default_attributes_test.rb +97 -0
  247. data/test/lib/vedeu/views/line_test.rb +20 -75
  248. data/test/lib/vedeu/views/lines_test.rb +47 -0
  249. data/test/lib/vedeu/views/stream_test.rb +38 -63
  250. data/test/lib/vedeu/views/streams_test.rb +9 -0
  251. data/test/lib/vedeu/views/value_test.rb +112 -0
  252. data/test/lib/vedeu/views/view_test.rb +67 -45
  253. data/test/lib/vedeu/views/views_test.rb +22 -0
  254. data/test/support/examples/material_colours_app.rb +2 -2
  255. data/test/support/helpers/model_test_class.rb +1 -0
  256. data/test/test_helper.rb +3 -1
  257. data/vedeu.gemspec +2 -2
  258. metadata +124 -58
  259. data/lib/vedeu/dsl/composition.rb +0 -150
  260. data/lib/vedeu/dsl/line.rb +0 -119
  261. data/lib/vedeu/dsl/lines.rb +0 -23
  262. data/lib/vedeu/dsl/shared.rb +0 -85
  263. data/lib/vedeu/dsl/stream.rb +0 -40
  264. data/lib/vedeu/dsl/streams.rb +0 -23
  265. data/lib/vedeu/dsl/text.rb +0 -98
  266. data/lib/vedeu/geometries/alignment/all.rb +0 -11
  267. data/lib/vedeu/geometries/alignment/horizontal_alignment.rb +0 -27
  268. data/lib/vedeu/geometries/alignment/vertical_alignment.rb +0 -27
  269. data/lib/vedeu/output/text.rb +0 -275
  270. data/test/lib/vedeu/dsl/composition_test.rb +0 -67
  271. data/test/lib/vedeu/dsl/line_test.rb +0 -85
  272. data/test/lib/vedeu/dsl/shared_test.rb +0 -66
  273. data/test/lib/vedeu/dsl/stream_test.rb +0 -30
  274. data/test/lib/vedeu/dsl/streams_test.rb +0 -13
  275. data/test/lib/vedeu/dsl/text_test.rb +0 -122
  276. data/test/lib/vedeu/output/text_test.rb +0 -156
  277. data/test/lib/vedeu/views/view_collection_test.rb +0 -13
@@ -0,0 +1,122 @@
1
+ module Vedeu
2
+
3
+ module Coercers
4
+
5
+ # Coerces a colour options hash into: an empty hash, or a hash
6
+ # containing either or both background and foreground keys.
7
+ #
8
+ # @api private
9
+ #
10
+ class ColourAttributes
11
+
12
+ include Vedeu::Common
13
+
14
+ # @param value [Hash]
15
+ # @return [Hash]
16
+ def self.coerce(value)
17
+ new(value).coerce
18
+ end
19
+
20
+ # @param value [Hash]
21
+ # @return [Hash]
22
+ def initialize(value)
23
+ @value = value
24
+ end
25
+
26
+ # @return [Hash]
27
+ def coerce
28
+ fail Vedeu::Error::InvalidSyntax unless hash?(value)
29
+
30
+ if colour? && hash?(colour)
31
+ Vedeu::Coercers::ColourAttributes.coerce(colour)
32
+
33
+ elsif colour? && already_coerced?(colour)
34
+ colour.attributes
35
+
36
+ else
37
+ coerced_background.merge(coerced_foreground)
38
+
39
+ end
40
+ end
41
+
42
+ protected
43
+
44
+ # @!attribute [r] value
45
+ # @return [Hash]
46
+ attr_reader :value
47
+
48
+ private
49
+
50
+ # @param colour [void]
51
+ # @return [Boolean]
52
+ def already_coerced?(colour)
53
+ colour.is_a?(Vedeu::Colours::Colour)
54
+ end
55
+
56
+ # @return [Hash]
57
+ def coerced_background
58
+ if background?
59
+ {
60
+ background: background
61
+ }
62
+
63
+ else
64
+ {}
65
+
66
+ end
67
+ end
68
+
69
+ # @return [NilClass|String]
70
+ def background
71
+ value[:background]
72
+ end
73
+
74
+ # @return [Boolean]
75
+ def background?
76
+ valid?(background)
77
+ end
78
+
79
+ # @return [Hash]
80
+ def colour
81
+ value[:colour]
82
+ end
83
+
84
+ # @return [Boolean]
85
+ def colour?
86
+ value.key?(:colour)
87
+ end
88
+
89
+ # @return [Hash]
90
+ def coerced_foreground
91
+ if foreground?
92
+ {
93
+ foreground: foreground
94
+ }
95
+
96
+ else
97
+ {}
98
+
99
+ end
100
+ end
101
+
102
+ # @return [NilClass|String]
103
+ def foreground
104
+ value[:foreground]
105
+ end
106
+
107
+ # @return [Boolean]
108
+ def foreground?
109
+ valid?(foreground)
110
+ end
111
+
112
+ # @param colour [void]
113
+ # @return [Boolean]
114
+ def valid?(colour)
115
+ Vedeu::Colours::Validator.valid?(colour)
116
+ end
117
+
118
+ end # ColourAttributes
119
+
120
+ end # Coercers
121
+
122
+ end # Vedeu
@@ -0,0 +1,28 @@
1
+ module Vedeu
2
+
3
+ module Coercers
4
+
5
+ # Provides the mechanism to validate a horizontal alignment value.
6
+ #
7
+ # @api private
8
+ #
9
+ class HorizontalAlignment < Vedeu::Coercers::Alignment
10
+
11
+ private
12
+
13
+ # @return [Array<Symbol>]
14
+ def values
15
+ [
16
+ :centre,
17
+ :center,
18
+ :left,
19
+ :none,
20
+ :right,
21
+ ]
22
+ end
23
+
24
+ end # HorizontalAlignment
25
+
26
+ end # Coercers
27
+
28
+ end # Vedeu
@@ -0,0 +1,27 @@
1
+ module Vedeu
2
+
3
+ module Coercers
4
+
5
+ # Provides the mechanism to validate a vertical alignment value.
6
+ #
7
+ # @api private
8
+ #
9
+ class VerticalAlignment < Vedeu::Coercers::Alignment
10
+
11
+ private
12
+
13
+ # @return [Array<Symbol>]
14
+ def values
15
+ [
16
+ :bottom,
17
+ :middle,
18
+ :none,
19
+ :top,
20
+ ]
21
+ end
22
+
23
+ end # VerticalAlignment
24
+
25
+ end # Coercers
26
+
27
+ end # Vedeu
@@ -8,6 +8,7 @@ module Vedeu
8
8
 
9
9
  end # Vedeu
10
10
 
11
+ require 'vedeu/colours/validator'
11
12
  require 'vedeu/colours/repository'
12
13
  require 'vedeu/colours/backgrounds'
13
14
  require 'vedeu/colours/foregrounds'
@@ -10,6 +10,11 @@ module Vedeu
10
10
  #
11
11
  class Background < Vedeu::Colours::Translator
12
12
 
13
+ # @return [Boolean]
14
+ def background?
15
+ present?(to_s)
16
+ end
17
+
13
18
  private
14
19
 
15
20
  # @return [String]
@@ -17,9 +22,15 @@ module Vedeu
17
22
  "\e[48;".freeze
18
23
  end
19
24
 
25
+ # Returns an escape sequence for a named colour.
26
+ #
27
+ # @note
28
+ # Valid names can be found at
29
+ # {Vedeu::EscapeSequences::Esc#valid_codes}
30
+ #
20
31
  # @return [String]
21
- def named_codes
22
- Vedeu::EscapeSequences::Esc.background_codes[colour]
32
+ def named_code
33
+ Vedeu::EscapeSequences::Esc.background_colour(colour)
23
34
  end
24
35
 
25
36
  # @return [Vedeu::Colours::Backgrounds]
@@ -37,44 +37,29 @@ module Vedeu
37
37
  #
38
38
  class Colour
39
39
 
40
- # @param value [Vedeu::Colours::Colour|Hash<Symbol => void>]
41
- # @return [Vedeu::Colours::Colour]
42
- def self.coerce(value)
43
- return value if value.is_a?(self)
44
- return new unless value.is_a?(Hash)
40
+ class << self
45
41
 
46
- if value[:colour] && value[:colour].is_a?(self)
47
- value[:colour]
42
+ extend Forwardable
48
43
 
49
- elsif value[:colour] && value[:colour].is_a?(Hash)
50
- new(value[:colour])
44
+ def_delegators Vedeu::Coercers::Colour,
45
+ :coerce
51
46
 
52
- elsif value[:background] || value[:foreground]
53
- new(value)
47
+ # @return [Vedeu::Colours::Colour]
48
+ def default
49
+ new(background: :default, foreground: :default)
50
+ end
54
51
 
55
- else
56
- new
52
+ end # Eigenclass
57
53
 
58
- end
59
- end
54
+ extend Forwardable
60
55
 
61
- # @return [Vedeu::Colours::Colour]
62
- def self.default
63
- new(background: :default, foreground: :default)
64
- end
56
+ def_delegators :background,
57
+ :background?
65
58
 
66
- # Returns a new instance of Vedeu::Colours::Colour.
67
- #
68
- # @param attributes [Hash<Symbol => String|
69
- # Vedeu::Colours::Background|Vedeu::Colours:Foreground>]
70
- # @option attributes background [String]
71
- # @option attributes foreground [String]
72
- # @return [Vedeu::Colours::Colour]
73
- def initialize(attributes = {})
74
- defaults.merge!(attributes).each do |key, value|
75
- instance_variable_set("@#{key}", value)
76
- end
77
- end
59
+ def_delegators :foreground,
60
+ :foreground?
61
+
62
+ include Vedeu::Repositories::Defaults
78
63
 
79
64
  # @return [Hash<Symbol => Vedeu::Colours::Background,
80
65
  # Vedeu::Colours::Foreground>]
@@ -10,6 +10,11 @@ module Vedeu
10
10
  #
11
11
  class Foreground < Vedeu::Colours::Translator
12
12
 
13
+ # @return [Boolean]
14
+ def foreground?
15
+ present?(to_s)
16
+ end
17
+
13
18
  private
14
19
 
15
20
  # @return [String]
@@ -17,9 +22,15 @@ module Vedeu
17
22
  "\e[38;".freeze
18
23
  end
19
24
 
25
+ # Returns an escape sequence for a named colour.
26
+ #
27
+ # @note
28
+ # Valid names can be found at
29
+ # {Vedeu::EscapeSequences::Esc#valid_codes}
30
+ #
20
31
  # @return [String]
21
- def named_codes
22
- Vedeu::EscapeSequences::Esc.foreground_codes[colour]
32
+ def named_code
33
+ Vedeu::EscapeSequences::Esc.foreground_colour(colour)
23
34
  end
24
35
 
25
36
  # @return [Vedeu::Colours::Foregrounds]
@@ -30,6 +30,14 @@ module Vedeu
30
30
  #
31
31
  class Translator
32
32
 
33
+ extend Forwardable
34
+ include Vedeu::Common
35
+
36
+ def_delegators :validator,
37
+ :named?,
38
+ :rgb?,
39
+ :within_range?
40
+
33
41
  # @!attribute [r] colour
34
42
  # @return [String]
35
43
  attr_reader :colour
@@ -56,7 +64,7 @@ module Vedeu
56
64
 
57
65
  # @return [Boolean]
58
66
  def empty?
59
- colour.nil? || colour.to_s.empty?
67
+ absent?(colour)
60
68
  end
61
69
 
62
70
  # An object is equal when its values are the same.
@@ -71,20 +79,19 @@ module Vedeu
71
79
  # @return [String]
72
80
  # @see Vedeu::Colours::Translator
73
81
  def escape_sequence
74
- if empty?
75
- ''
82
+ return '' if empty?
76
83
 
77
- elsif registered?(colour)
84
+ if registered?(colour)
78
85
  retrieve(colour)
79
86
 
80
87
  elsif rgb?
81
88
  rgb
82
89
 
83
- elsif numbered?
90
+ elsif within_range?
84
91
  numbered
85
92
 
86
93
  elsif named?
87
- named
94
+ named_code
88
95
 
89
96
  else
90
97
  ''
@@ -132,38 +139,6 @@ module Vedeu
132
139
  repository.registered?(colour)
133
140
  end
134
141
 
135
- # @return [Boolean]
136
- def named?
137
- colour.is_a?(Symbol) && valid_name?
138
- end
139
-
140
- # Returns an escape sequence for a named background colour.
141
- #
142
- # @note
143
- # Valid names can be found at
144
- # {Vedeu::EscapeSequences::Esc#codes}
145
- #
146
- # @return [String]
147
- def named
148
- "\e[#{named_codes}m".freeze
149
- end
150
-
151
- # Returns a boolean indicating whether the colour provided is a
152
- # valid named colour.
153
- #
154
- # @return [Boolean]
155
- def valid_name?
156
- Vedeu::EscapeSequences::Esc.codes.keys.include?(colour)
157
- end
158
-
159
- # Returns a boolean indicating whether the colour provided is a
160
- # terminal numbered colour.
161
- #
162
- # @return [Boolean]
163
- def numbered?
164
- colour.is_a?(Fixnum) && valid_range?
165
- end
166
-
167
142
  # Returns an escape sequence.
168
143
  #
169
144
  # @return [String]
@@ -171,16 +146,6 @@ module Vedeu
171
146
  "#{prefix}5;#{css_to_numbered}m".freeze
172
147
  end
173
148
 
174
- # Returns a boolean indicated whether the colour is a valid
175
- # HTML/CSS colour.
176
- #
177
- # @return [Boolean]
178
- def rgb?
179
- return true if colour =~ /^#([A-Fa-f0-9]{6})$/.freeze
180
-
181
- false
182
- end
183
-
184
149
  # Returns an escape sequence.
185
150
  #
186
151
  # @return [String]
@@ -207,14 +172,6 @@ module Vedeu
207
172
  "#{prefix}2;%s;%s;%sm".freeze
208
173
  end
209
174
 
210
- # Returns a boolean indicating whether the numbered colour is
211
- # within the range of valid terminal numbered colours.
212
- #
213
- # @return [Boolean]
214
- def valid_range?
215
- colour >= 0 && colour <= 255
216
- end
217
-
218
175
  # Returns a collection of converted HTML/CSS octets as their
219
176
  # decimal equivalents.
220
177
  #
@@ -236,7 +193,7 @@ module Vedeu
236
193
  if rgb?
237
194
  [16, red, green, blue].inject(:+)
238
195
 
239
- elsif numbered?
196
+ elsif within_range?
240
197
  colour
241
198
 
242
199
  end
@@ -272,8 +229,13 @@ module Vedeu
272
229
  def not_implemented
273
230
  fail Vedeu::Error::NotImplemented, 'Subclasses implement this.'.freeze
274
231
  end
275
- alias_method :named_codes, :not_implemented
276
- alias_method :repository, :not_implemented
232
+ alias_method :named_code, :not_implemented
233
+ alias_method :repository, :not_implemented
234
+
235
+ # @return [Vedeu::Colours::Validator]
236
+ def validator
237
+ @validator ||= Vedeu::Colours::Validator.new(colour)
238
+ end
277
239
 
278
240
  end # Translator
279
241