vedeu 0.1.15 → 0.1.16

Sign up to get free protection for your applications and to get access to all the features.
Files changed (81) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE.txt +2 -0
  3. data/README.md +116 -98
  4. data/lib/vedeu.rb +24 -11
  5. data/lib/vedeu/api/api.rb +174 -33
  6. data/lib/vedeu/api/composition.rb +2 -13
  7. data/lib/vedeu/api/helpers.rb +41 -0
  8. data/lib/vedeu/api/interface.rb +147 -34
  9. data/lib/vedeu/api/line.rb +47 -0
  10. data/lib/vedeu/api/stream.rb +41 -0
  11. data/lib/vedeu/application.rb +9 -2
  12. data/lib/vedeu/configuration.rb +66 -8
  13. data/lib/vedeu/launcher.rb +11 -0
  14. data/lib/vedeu/models/attributes/background.rb +29 -0
  15. data/lib/vedeu/models/attributes/coercions.rb +30 -0
  16. data/lib/vedeu/models/attributes/colour_translator.rb +106 -16
  17. data/lib/vedeu/models/attributes/foreground.rb +29 -0
  18. data/lib/vedeu/models/attributes/presentation.rb +15 -0
  19. data/lib/vedeu/models/colour.rb +12 -7
  20. data/lib/vedeu/models/composition.rb +23 -5
  21. data/lib/vedeu/models/geometry.rb +101 -16
  22. data/lib/vedeu/models/interface.rb +36 -28
  23. data/lib/vedeu/models/line.rb +16 -14
  24. data/lib/vedeu/models/stream.rb +17 -25
  25. data/lib/vedeu/models/style.rb +29 -0
  26. data/lib/vedeu/support/buffer.rb +36 -21
  27. data/lib/vedeu/support/buffers.rb +88 -0
  28. data/lib/vedeu/{output → support}/clear.rb +9 -2
  29. data/lib/vedeu/support/esc.rb +16 -2
  30. data/lib/vedeu/support/event.rb +104 -0
  31. data/lib/vedeu/support/events.rb +61 -0
  32. data/lib/vedeu/support/focus.rb +69 -0
  33. data/lib/vedeu/{api → support}/grid.rb +29 -25
  34. data/lib/vedeu/support/groups.rb +51 -0
  35. data/lib/vedeu/support/input.rb +5 -0
  36. data/lib/vedeu/support/log.rb +105 -0
  37. data/lib/vedeu/support/menu.rb +19 -0
  38. data/lib/vedeu/support/position.rb +7 -0
  39. data/lib/vedeu/{output → support}/render.rb +12 -6
  40. data/lib/vedeu/support/terminal.rb +21 -0
  41. data/lib/vedeu/support/trace.rb +54 -0
  42. data/lib/vedeu/{output → support}/view.rb +15 -10
  43. data/test/lib/vedeu/api/api_test.rb +55 -29
  44. data/test/lib/vedeu/api/interface_test.rb +3 -49
  45. data/test/lib/vedeu/api/line_test.rb +71 -0
  46. data/test/lib/vedeu/api/stream_test.rb +25 -0
  47. data/test/lib/vedeu/models/attributes/background_test.rb +83 -0
  48. data/test/lib/vedeu/models/attributes/coercer_test.rb +52 -0
  49. data/test/lib/vedeu/models/attributes/foreground_test.rb +83 -0
  50. data/test/lib/vedeu/models/attributes/presentation_test.rb +30 -0
  51. data/test/lib/vedeu/models/composition_test.rb +6 -6
  52. data/test/lib/vedeu/models/geometry_test.rb +34 -0
  53. data/test/lib/vedeu/models/interface_test.rb +0 -12
  54. data/test/lib/vedeu/models/line_test.rb +1 -13
  55. data/test/lib/vedeu/models/stream_test.rb +4 -50
  56. data/test/lib/vedeu/models/style_test.rb +39 -0
  57. data/test/lib/vedeu/support/buffer_test.rb +7 -3
  58. data/test/lib/vedeu/support/buffers_test.rb +15 -0
  59. data/test/lib/vedeu/{output → support}/clear_test.rb +8 -1
  60. data/test/lib/vedeu/support/esc_test.rb +2 -2
  61. data/test/lib/vedeu/support/event_test.rb +41 -0
  62. data/test/lib/vedeu/support/events_test.rb +70 -0
  63. data/test/lib/vedeu/support/focus_test.rb +114 -0
  64. data/test/lib/vedeu/support/grid_test.rb +31 -0
  65. data/test/lib/vedeu/support/groups_test.rb +65 -0
  66. data/test/lib/vedeu/{output → support}/render_test.rb +8 -1
  67. data/test/lib/vedeu/{output → support}/view_test.rb +1 -1
  68. data/test/test_helper.rb +3 -1
  69. data/vedeu.gemspec +3 -2
  70. metadata +67 -31
  71. data/lib/vedeu/api/events.rb +0 -46
  72. data/lib/vedeu/api/log.rb +0 -84
  73. data/lib/vedeu/models/attributes/attributes.rb +0 -25
  74. data/lib/vedeu/models/store.rb +0 -29
  75. data/lib/vedeu/output/buffers.rb +0 -65
  76. data/test/lib/vedeu/api/events_test.rb +0 -36
  77. data/test/lib/vedeu/api/grid_test.rb +0 -33
  78. data/test/lib/vedeu/models/attributes/attributes_test.rb +0 -39
  79. data/test/lib/vedeu/models/attributes/colour_translator_test.rb +0 -78
  80. data/test/lib/vedeu/models/store_test.rb +0 -57
  81. data/test/lib/vedeu/output/buffers_test.rb +0 -35
@@ -1,20 +1,8 @@
1
1
  module Vedeu
2
2
  module API
3
3
  class Composition < Vedeu::Composition
4
- def self.build(&block)
5
- new(&block).attributes
6
- end
7
-
8
- def initialize(&block)
9
- super
10
-
11
- if block_given?
12
- @self_before_instance_eval = eval('self', block.binding)
13
-
14
- instance_eval(&block)
15
- end
16
- end
17
4
 
5
+ # @see Vedeu::API#view
18
6
  def view(name, &block)
19
7
  attributes[:interfaces] << Interface.build({ name: name }, &block)
20
8
  end
@@ -24,6 +12,7 @@ module Vedeu
24
12
  def method_missing(method, *args, &block)
25
13
  @self_before_instance_eval.send(method, *args, &block)
26
14
  end
15
+
27
16
  end
28
17
  end
29
18
  end
@@ -1,6 +1,26 @@
1
1
  module Vedeu
2
2
  module API
3
3
  module Helpers
4
+
5
+ # Define either or both foreground and background colours for an
6
+ # interface, line or a stream.
7
+ #
8
+ # @param values [Hash]
9
+ #
10
+ # @example
11
+ # interface 'my_interface' do
12
+ # colour background: '#000000', foreground: '#ffffff'
13
+ # ... some interface attributes ...
14
+ #
15
+ # line do
16
+ # colour background: '#000000', foreground: '#ffffff'
17
+ # ... some line attributes ...
18
+ #
19
+ # stream do
20
+ # colour background: '#000000', foreground: '#ffffff'
21
+ # ... some stream attributes ...
22
+ #
23
+ # @return []
4
24
  def colour(values = {})
5
25
  fail InvalidArgument, '#colour expects a Hash containing :foreground,' \
6
26
  ' :background or both.' unless values.is_a?(Hash)
@@ -8,6 +28,26 @@ module Vedeu
8
28
  attributes[:colour] = values
9
29
  end
10
30
 
31
+ # Define a style for an interface, line or a stream.
32
+ #
33
+ # @param values [Array|String]
34
+ # @param block [Proc]
35
+ #
36
+ # @example
37
+ # interface 'my_interface' do
38
+ # style 'normal'
39
+ # ... some interface attributes ...
40
+ # end
41
+ #
42
+ # line do
43
+ # style ['bold', 'underline']
44
+ # ... some line attributes ...
45
+ #
46
+ # stream do
47
+ # style 'blink'
48
+ # ... some stream attributes ...
49
+ #
50
+ # @return []
11
51
  def style(values = [], &block)
12
52
  if block_given?
13
53
  attributes[:streams] << API::Stream.build({ style: [values] }, &block)
@@ -17,6 +57,7 @@ module Vedeu
17
57
 
18
58
  end
19
59
  end
60
+
20
61
  end
21
62
  end
22
63
  end
@@ -1,97 +1,209 @@
1
1
  module Vedeu
2
- InvalidHeight = Class.new(StandardError)
3
- InvalidWidth = Class.new(StandardError)
4
- XOutOfBounds = Class.new(StandardError)
5
- YOutOfBounds = Class.new(StandardError)
6
-
7
2
  module API
8
3
  class Interface < Vedeu::Interface
9
- def self.build(attributes = {}, &block)
10
- new(attributes, &block).attributes
11
- end
4
+ include Helpers
12
5
 
6
+ # @param attributes [Hash]
7
+ # @param block [Proc]
8
+ # @return []
13
9
  def self.define(attributes = {}, &block)
14
10
  new(attributes).define(&block)
15
11
  end
16
12
 
17
- def initialize(attributes = {}, &block)
18
- @attributes = attributes
19
-
20
- if block_given?
21
- @self_before_instance_eval = eval('self', block.binding)
22
-
23
- instance_eval(&block)
24
- end
25
- end
26
-
13
+ # @param block [Proc]
14
+ #
15
+ # @example
16
+ # TODO
17
+ #
18
+ # @return []
27
19
  def define(&block)
28
20
  instance_eval(&block) if block_given?
29
21
 
30
- Vedeu::Store.create(attributes)
31
- end
22
+ Vedeu::Buffers.create(attributes)
32
23
 
24
+ Vedeu.event("_refresh_#{attributes[:name]}_".to_sym,
25
+ { delay: attributes[:delay] }) do
26
+ Vedeu::Buffers.refresh(attributes[:name])
27
+ end
28
+
29
+ true
30
+ end
31
+
32
+ # Define a single line in a view.
33
+ #
34
+ # @param block [Proc]
35
+ #
36
+ # @example
37
+ # view 'my_interface' do
38
+ # line do
39
+ # ... some line attributes ...
40
+ # end
41
+ # end
42
+ #
43
+ # @return []
33
44
  def line(&block)
34
45
  attributes[:lines] << Line.build(&block)
35
46
  end
36
47
 
48
+ # @see Vedeu::API#use
37
49
  def use(value)
38
50
  Vedeu.use(value)
39
51
  end
40
52
 
41
- def colour(value)
42
- attributes[:colour] = value
43
- end
44
-
53
+ # Define the cursor visibility for an interface. A `true` value will show
54
+ # the cursor, whilst `false` will hide it.
55
+ #
56
+ # @param value [Boolean]
57
+ #
58
+ # @example
59
+ # interface 'my_interface' do
60
+ # cursor true
61
+ # ... some interface attributes ...
62
+ # end
63
+ #
64
+ # @return []
45
65
  def cursor(value)
46
66
  attributes[:cursor] = value
47
67
  end
48
68
 
69
+ # @param value [Fixnum|Float]
70
+ #
71
+ # @return []
49
72
  def delay(value)
50
73
  attributes[:delay] = value
51
74
  end
52
75
 
76
+ # Define a group for an interface. Interfaces of the same group can be
77
+ # targetted together; for example you may want to refresh multiple
78
+ # interfaces at once.
79
+ #
80
+ # @param value [String]
81
+ #
82
+ # @example
83
+ # interface 'my_interface' do
84
+ # group 'main_screen' do
85
+ # ... some interface attributes ...
86
+ # end
87
+ #
88
+ # @return []
53
89
  def group(value)
54
90
  attributes[:group] = value
55
91
  end
56
92
 
93
+ # The name of the interface. Used to reference the interface throughout
94
+ # your application's execution lifetime.
95
+ #
96
+ # @param value [String]
97
+ #
98
+ # @example
99
+ # TODO
100
+ #
101
+ # @return []
57
102
  def name(value)
58
103
  attributes[:name] = value
59
104
  end
60
105
 
61
- def x(value)
62
- fail XOutOfBounds if x_out_of_bounds?(value)
106
+ # Define the starting x position (column) of the interface.
107
+ #
108
+ # @param value [Fixnum]
109
+ # @param block [Proc]
110
+ #
111
+ # @example
112
+ # interface 'my_interface' do
113
+ # x 7 # start on column 7.
114
+ #
115
+ # interface 'other_interface' do
116
+ # x { use('my_interface').east } # start on column 8, if
117
+ # # `my_interface` changes position,
118
+ # # `other_interface` will too.
119
+ #
120
+ # @return []
121
+ def x(value = 0, &block)
122
+ return attributes[:geometry][:x] = block if block_given?
123
+
124
+ Vedeu.log(out_of_bounds('x')) if x_out_of_bounds?(value)
63
125
 
64
126
  attributes[:geometry][:x] = value
65
127
  end
66
128
 
67
- def y(value)
68
- fail YOutOfBounds if y_out_of_bounds?(value)
129
+ # Define the starting y position (row/line) of the interface.
130
+ #
131
+ # @param value [Fixnum]
132
+ # @param block [Proc]
133
+ #
134
+ # @example
135
+ # interface 'my_interface' do
136
+ # y 4
137
+ # ...
138
+ #
139
+ # interface 'other_interface' do
140
+ # y { use('my_interface').north } # start on row/line 3, if
141
+ # ... # `my_interface` changes position,
142
+ # # `other_interface` will too.
143
+ #
144
+ # @return []
145
+ def y(value = 0, &block)
146
+ return attributes[:geometry][:y] = block if block_given?
147
+
148
+ Vedeu.log(out_of_bounds('y')) if y_out_of_bounds?(value)
69
149
 
70
150
  attributes[:geometry][:y] = value
71
151
  end
72
152
 
153
+ # Define the number of characters/columns wide the interface will be.
154
+ #
155
+ # @param value [Fixnum]
156
+ #
157
+ # @example
158
+ # interface 'my_interface' do
159
+ # width 25
160
+ # ...
161
+ #
162
+ # @return []
73
163
  def width(value)
74
- fail InvalidWidth if x_out_of_bounds?(value)
164
+ Vedeu.log(out_of_bounds('width')) if x_out_of_bounds?(value)
75
165
 
76
166
  attributes[:geometry][:width] = value
77
167
  end
78
168
 
169
+ # Define the number of characters/rows/lines tall the interface will be.
170
+ #
171
+ # @param value [Fixnum]
172
+ #
173
+ # @example
174
+ # interface 'my_interface' do
175
+ # height 8
176
+ # ...
177
+ #
178
+ # @return []
79
179
  def height(value)
80
- fail InvalidHeight if y_out_of_bounds?(value)
180
+ Vedeu.log(out_of_bounds('height')) if y_out_of_bounds?(value)
81
181
 
82
182
  attributes[:geometry][:height] = value
83
183
  end
84
184
 
185
+ # Instructs Vedeu to calculate x and y geometry automatically based on the
186
+ # centre character of the terminal, the width and the height.
187
+ #
188
+ # @param value [Boolean]
189
+ #
190
+ # @example
191
+ # interface 'my_interface' do
192
+ # centred true
193
+ # ...
194
+ #
195
+ # @return []
85
196
  def centred(value)
86
197
  attributes[:geometry][:centred] = value
87
198
  end
88
199
 
89
- def style(value)
90
- attributes[:style] = value
91
- end
92
-
93
200
  private
94
201
 
202
+ def out_of_bounds(name)
203
+ "Note: For this terminal, the value of '#{name}' may lead to content " \
204
+ "that is outside the viewable area."
205
+ end
206
+
95
207
  def y_out_of_bounds?(value)
96
208
  value < 1 || value > Terminal.height
97
209
  end
@@ -103,6 +215,7 @@ module Vedeu
103
215
  def method_missing(method, *args, &block)
104
216
  @self_before_instance_eval.send(method, *args, &block)
105
217
  end
218
+
106
219
  end
107
220
  end
108
221
  end
@@ -3,25 +3,72 @@ module Vedeu
3
3
  class Line < Vedeu::Line
4
4
  include Helpers
5
5
 
6
+ # Define a stream (a subset of a line).
7
+ #
8
+ # @param block [Proc] Block contains directives relating to API::Stream.
9
+ #
10
+ # @example
11
+ # ...
12
+ # line do
13
+ # stream do
14
+ # ... other stream directives ...
15
+ # end
16
+ # end
17
+ # ...
18
+ #
19
+ # @return [Array]
6
20
  def stream(&block)
7
21
  attributes[:streams] << API::Stream.build(&block)
8
22
  end
9
23
 
24
+ # Define text for a line. Using this directive will not allow stream
25
+ # attributes for this line but is useful for adding lines straight into
26
+ # the interface.
27
+ #
28
+ # @param value [String]
29
+ #
30
+ # @example
31
+ # ...
32
+ # line do
33
+ # text 'Some text to display...'
34
+ #
35
+ # @return [Array]
10
36
  def text(value)
11
37
  attributes[:streams] << { text: value }
12
38
  end
13
39
 
40
+ # @param value [String]
41
+ # @param block [Proc]
42
+ #
43
+ # @example
44
+ # ...
45
+ # line do
46
+ # foreground '#00ff00' do
47
+ # ... other stream directives ...
48
+ #
49
+ # @return [Array]
14
50
  def foreground(value = '', &block)
15
51
  attributes[:streams] << API::Stream.build({
16
52
  colour: { foreground: value }
17
53
  }, &block)
18
54
  end
19
55
 
56
+ # @param value [String]
57
+ # @param block [Proc]
58
+ #
59
+ # @example
60
+ # ...
61
+ # line do
62
+ # background '#0022ff' do
63
+ # ... other stream directives ...
64
+ #
65
+ # @return [Array]
20
66
  def background(value = '', &block)
21
67
  attributes[:streams] << API::Stream.build({
22
68
  colour: { background: value }
23
69
  }, &block)
24
70
  end
71
+
25
72
  end
26
73
  end
27
74
  end
@@ -3,17 +3,58 @@ module Vedeu
3
3
  class Stream < Vedeu::Stream
4
4
  include Helpers
5
5
 
6
+ # Specify the alignment of the stream within the line. Useful in
7
+ # combination with #width to provide simple formatting effects.
8
+ #
9
+ # @param value [Symbol] `:left`, `:centre` and `right` are valid values
10
+ # and will align accordingly. If not value is specified, the stream will
11
+ # left align.
12
+ #
13
+ # @example
14
+ # ...
15
+ # stream do
16
+ # align :right
17
+ # ...
18
+ #
19
+ # @return [Symbol]
6
20
  def align(value)
7
21
  attributes[:align] = value
8
22
  end
9
23
 
24
+ # Add textual data to the stream via this method.
25
+ #
26
+ # @param value [String] The text to be added to the stream. If the length
27
+ # of the text is greater than the interface's width, it will be
28
+ # truncated and ellipsized.
29
+ #
30
+ # @example
31
+ # ...
32
+ # stream do
33
+ # text 'Some text to display...'
34
+ # ...
35
+ #
36
+ # @return [String]
10
37
  def text(value)
11
38
  attributes[:text] = value
12
39
  end
13
40
 
41
+ # Provides the ability to arbitrarily set the width of content for a
42
+ # stream. Useful in combination with #align to provide simple formatting
43
+ # effects.
44
+ #
45
+ # @param value [Fixnum] The width in characters.
46
+ #
47
+ # @example
48
+ # ...
49
+ # stream do
50
+ # width 20
51
+ # ...
52
+ #
53
+ # @return [Fixnum]
14
54
  def width(value)
15
55
  attributes[:width] = value
16
56
  end
57
+
17
58
  end
18
59
  end
19
60
  end