vedeu 0.4.8 → 0.4.9

Sign up to get free protection for your applications and to get access to all the features.
@@ -32,13 +32,13 @@ module Vedeu
32
32
 
33
33
  # @return [Fixnum]
34
34
  def cy
35
- ((yn - y) / 2) + y
35
+ (height / 2) + y
36
36
  end
37
37
  alias_method :centre_y, :cy
38
38
 
39
39
  # @return [Fixnum]
40
40
  def cx
41
- ((xn - x) / 2) + x
41
+ (width / 2) + x
42
42
  end
43
43
  alias_method :centre_x, :cx
44
44
 
@@ -48,6 +48,16 @@ module Vedeu
48
48
  end
49
49
  alias_method :origin, :o
50
50
 
51
+ # @return [Fixnum]
52
+ def height
53
+ (y..yn).size
54
+ end
55
+
56
+ # @return [Fixnum]
57
+ def width
58
+ (x..xn).size
59
+ end
60
+
51
61
  # @return [Fixnum]
52
62
  def y
53
63
  1
@@ -15,17 +15,19 @@ module Vedeu
15
15
  # @option attributes dn [Fixnum|NilClass] The ending value (yn or xn).
16
16
  # @option attributes d_dn [Fixnum|NilClass] A width or a height.
17
17
  # @option attributes default [Fixnum|NilClass] The terminal width or height.
18
+ # @option attributes options [Hash]
18
19
  # @return [Vedeu::Dimension]
19
20
  def initialize(attributes = {})
20
21
  @d = attributes[:d]
21
22
  @dn = attributes[:dn]
22
23
  @d_dn = attributes[:d_dn]
23
24
  @default = attributes[:default]
25
+ @options = attributes.fetch(:options, {})
24
26
  end
25
27
 
26
28
  # @return [Fixnum]
27
29
  def d1
28
- dimension.first
30
+ dimension.first < 1 ? 1 : dimension.first
29
31
  end
30
32
 
31
33
  # @return [Fixnum]
@@ -40,26 +42,6 @@ module Vedeu
40
42
 
41
43
  private
42
44
 
43
- # @return [Array<Fixnum>]
44
- def dimension
45
- @dimension ||= if @d && @dn
46
- [@d, @dn]
47
-
48
- elsif @d && @d_dn
49
- [@d, (@d + @d_dn)]
50
-
51
- elsif @d_dn
52
- [1, @d_dn]
53
-
54
- elsif @d
55
- [@d, @default]
56
-
57
- else
58
- [1, @default]
59
-
60
- end
61
- end
62
-
63
45
  # @!attribute [r] d
64
46
  # @return [Fixnum]
65
47
  attr_reader :d
@@ -76,6 +58,71 @@ module Vedeu
76
58
  # @return [Fixnum]
77
59
  attr_reader :default
78
60
 
61
+ # @return [Array<Fixnum>]
62
+ def dimension
63
+ @dimension ||= if centred? && length?
64
+ [(default / 2) - (length / 2), (default / 2) + (length / 2)]
65
+
66
+ elsif d && dn
67
+ [d, dn]
68
+
69
+ elsif d && d_dn
70
+ [d, (d + d_dn)]
71
+
72
+ elsif d_dn
73
+ [1, d_dn]
74
+
75
+ elsif d
76
+ [d, default]
77
+
78
+ elsif dn
79
+ [1, dn]
80
+
81
+ else
82
+ [1, default]
83
+
84
+ end
85
+ end
86
+
87
+ # @return [Boolean]
88
+ def length?
89
+ !!(default) && !!(length)
90
+ end
91
+
92
+ # @return [Fixnum|NilClass]
93
+ def length
94
+ if d && dn
95
+ (d..dn).size
96
+
97
+ elsif d_dn
98
+ d_dn
99
+
100
+ elsif default
101
+ default
102
+
103
+ else
104
+ nil
105
+
106
+ end
107
+ end
108
+
109
+ # @return [Boolean]
110
+ def centred?
111
+ options[:centred]
112
+ end
113
+
114
+ # @return [Hash<Symbol => Boolean>]
115
+ def options
116
+ defaults.merge!(@options)
117
+ end
118
+
119
+ # @return [Hash<Symbol => Boolean>]
120
+ def defaults
121
+ {
122
+ centred: false,
123
+ }
124
+ end
125
+
79
126
  end # Dimension
80
127
 
81
128
  end # Vedeu
@@ -1,8 +1,8 @@
1
1
  require 'vedeu/dsl/components/all'
2
2
  require 'vedeu/repositories/model'
3
3
 
4
- require 'vedeu/geometry/limit'
5
- require 'vedeu/geometry/position'
4
+ require 'vedeu/geometry/area'
5
+ require 'vedeu/geometry/dimension'
6
6
  require 'vedeu/support/terminal'
7
7
 
8
8
  module Vedeu
@@ -32,38 +32,53 @@ module Vedeu
32
32
  extend Forwardable
33
33
  include Vedeu::Model
34
34
 
35
+ def_delegators :area, :north,
36
+ :east,
37
+ :south,
38
+ :west,
39
+ :top,
40
+ :right,
41
+ :bottom,
42
+ :left,
43
+ :y,
44
+ :xn,
45
+ :yn,
46
+ :x,
47
+ :height,
48
+ :width
49
+
35
50
  # @!attribute [rw] centred
36
51
  # @return [Boolean]
37
52
  attr_accessor :centred
38
53
 
39
- # @!attribute [rw] height
40
- # @return [Fixnum]
41
- attr_accessor :height
42
-
43
54
  # @!attribute [rw] name
44
55
  # @return [String]
45
56
  attr_accessor :name
46
57
 
47
- # @!attribute [rw] width
48
- # @return [Fixnum]
49
- attr_accessor :width
50
-
51
58
  # @!attribute [r] attributes
52
59
  # @return [Hash]
53
60
  attr_reader :attributes
54
61
 
55
- # @!attribute [w] x
62
+ # @!attribute [w] height
56
63
  # @return [Fixnum]
57
- attr_writer :x
64
+ attr_writer :height
58
65
 
59
- # @!attribute [w] y
66
+ # @!attribute [w] width
60
67
  # @return [Fixnum]
61
- attr_writer :y
68
+ attr_writer :width
69
+
70
+ # @!attribute [w] x
71
+ # @return [Fixnum]
72
+ attr_writer :x
62
73
 
63
74
  # @!attribute [w] xn
64
75
  # @return [Fixnum]
65
76
  attr_writer :xn
66
77
 
78
+ # @!attribute [w] y
79
+ # @return [Fixnum]
80
+ attr_writer :y
81
+
67
82
  # @!attribute [w] yn
68
83
  # @return [Fixnum]
69
84
  attr_writer :yn
@@ -81,240 +96,71 @@ module Vedeu
81
96
  # @option attributes yn [Fixnum]
82
97
  # @return [Geometry]
83
98
  def initialize(attributes = {})
84
- @attributes = defaults.merge!(attributes)
85
-
86
- @centred = @attributes[:centred]
87
- @height = @attributes[:height]
88
- @name = @attributes[:name]
89
- @width = @attributes[:width]
90
- @x = @attributes[:x]
91
- @xn = @attributes[:xn]
92
- @y = @attributes[:y]
93
- @yn = @attributes[:yn]
99
+ @attributes = attributes
100
+ @centred = @attributes[:centred]
101
+ @height = @attributes[:height]
102
+ @name = @attributes[:name]
103
+ @width = @attributes[:width]
104
+ @x = @attributes[:x]
105
+ @xn = @attributes[:xn]
106
+ @y = @attributes[:y]
107
+ @yn = @attributes[:yn]
94
108
  @repository = Vedeu.geometries
95
109
  end
96
110
 
97
- # Returns the row/line start position for the interface.
98
- #
99
- # @return [Fixnum]
100
- def y
101
- if @y.is_a?(Proc)
102
- @y.call
103
-
104
- else
105
- @y
106
-
107
- end
108
- end
109
-
110
- # Returns the row/line end position for the interface.
111
- #
112
- # @return [Fixnum]
113
- def yn
114
- if @yn.is_a?(Proc)
115
- @yn.call
116
-
117
- else
118
- @yn
119
-
120
- end
121
- end
122
-
123
- # Returns the column/character start position for the interface.
124
- #
125
- # @return [Fixnum]
126
- def x
127
- if @x.is_a?(Proc)
128
- @x.call
129
-
130
- else
131
- @x
132
-
133
- end
134
- end
135
-
136
- # Returns the column/character end position for the interface.
137
- #
138
- # @return [Fixnum]
139
- def xn
140
- if @xn.is_a?(Proc)
141
- @xn.call
142
-
143
- else
144
- @xn
145
-
146
- end
147
- end
148
-
149
- # Returns a dynamic value calculated from the current terminal dimensions,
150
- # combined with the desired start point.
151
- #
152
- # If the interface is `centred` then if the terminal resizes, this value
153
- # should attempt to accommodate that.
154
- #
155
- # For uncentred interfaces, when the terminal resizes, then this will help
156
- # Vedeu render the view to ensure the content is not off-screen.
157
- #
158
- # @return [Fixnum]
159
- def width
160
- Vedeu::Limit.apply(x, @width, Vedeu::Terminal.width, Vedeu::Terminal.origin)
161
- end
162
-
163
- # @see Vedeu::Geometry#width
164
- def height
165
- Vedeu::Limit.apply(y, @height, Vedeu::Terminal.height, Vedeu::Terminal.origin)
166
- end
167
-
168
- # Returns the top coordinate of the interface, a fixed or dynamic value
169
- # depending on whether the interface is centred or not.
170
- #
171
- # @note
172
- # Division which results in a non-integer will be rounded down.
173
- #
174
- # @return [Fixnum]
175
- def top
176
- if centred
177
- Vedeu::Terminal.centre_y - (height / 2)
178
-
179
- else
180
- y
181
-
182
- end
183
- end
111
+ private
184
112
 
185
- # Returns the row above the top by default.
186
- #
187
- # @example
188
- # `top` is 4.
189
- #
190
- # north # => 3
191
- # north(2) # => 2
192
- # north(-4) # => 8
193
- #
194
- # @param value [Fixnum]
195
- # @return [Fixnum]
196
- def north(value = 1)
197
- top - value
113
+ # @return [Vedeu::Area]
114
+ def area
115
+ @area ||= Vedeu::Area.from_dimensions(y_yn: y_yn, x_xn: x_xn)
198
116
  end
199
117
 
200
- # Returns the left coordinate of the interface, a fixed or dynamic value
201
- # depending on whether the interface is centred or not.
202
- #
203
- # @note
204
- # Division which results in a non-integer will be rounded down.
205
- #
206
- # @return [Fixnum]
207
- def left
208
- if centred
209
- Vedeu::Terminal.centre_x - (width / 2)
210
-
211
- else
212
- x
213
-
214
- end
118
+ # @return [Array<Fixnum>]
119
+ def x_xn
120
+ @x_xn ||= Vedeu::Dimension.pair({ d: _x,
121
+ dn: _xn,
122
+ d_dn: @width,
123
+ default: Vedeu::Terminal.width,
124
+ options: { centred: centred }
125
+ })
215
126
  end
216
127
 
217
- # Returns the column before left by default.
218
- #
219
- # @example
220
- # `left` is 8.
221
- #
222
- # west # => 7
223
- # west(2) # => 6
224
- # west(-4) # => 12
225
- #
226
- # @param value [Fixnum]
227
- # @return [Fixnum]
228
- def west(value = 1)
229
- left - value
128
+ # @return [Array<Fixnum>]
129
+ def y_yn
130
+ @y_yn ||= Vedeu::Dimension.pair({ d: _y,
131
+ dn: _yn,
132
+ d_dn: @height,
133
+ default: Vedeu::Terminal.height,
134
+ options: { centred: centred }
135
+ })
230
136
  end
231
137
 
232
- # Returns the bottom coordinate of the interface, a fixed or dynamic value
233
- # depending on the value of {#top}.
138
+ # Returns the row/line start position for the interface.
234
139
  #
235
140
  # @return [Fixnum]
236
- def bottom
237
- top + height
141
+ def _y
142
+ @y.is_a?(Proc) ? @y.call : @y
238
143
  end
239
144
 
240
- # Returns the row below the bottom by default.
241
- #
242
- # @example
243
- # `bottom` is 12.
244
- #
245
- # south # => 13
246
- # south(2) # => 14
247
- # south(-4) # => 8
145
+ # Returns the row/line end position for the interface.
248
146
  #
249
- # @param value [Fixnum]
250
147
  # @return [Fixnum]
251
- def south(value = 1)
252
- bottom + value
148
+ def _yn
149
+ @yn.is_a?(Proc) ? @yn.call : @yn
253
150
  end
254
151
 
255
- # Returns the right coordinate of the interface, a fixed or dynamic value
256
- # depending on the value of {#left}.
152
+ # Returns the column/character start position for the interface.
257
153
  #
258
154
  # @return [Fixnum]
259
- def right
260
- left + width
155
+ def _x
156
+ @x.is_a?(Proc) ? @x.call : @x
261
157
  end
262
158
 
263
- # Returns the column after right by default.
264
- #
265
- # @example
266
- # `right` is 19.
267
- #
268
- # east # => 20
269
- # east(2) # => 21
270
- # east(-4) # => 15
159
+ # Returns the column/character end position for the interface.
271
160
  #
272
- # @param value [Fixnum]
273
161
  # @return [Fixnum]
274
- def east(value = 1)
275
- right + value
276
- end
277
-
278
- # def_delegators :area, :north, :east, :south, :west
279
-
280
- private
281
-
282
- # @return [Vedeu::Area]
283
- def area
284
- @area ||= Vedeu::Area.from_dimensions(y_yn, x_xn)
285
- end
286
-
287
- # @return [Array<Fixnum>]
288
- def x_xn
289
- @x_xn ||= Vedeu::Dimension.pair({ v: @x,
290
- vn: @xn,
291
- v_vn: @width,
292
- default: Vedeu::Terminal.width })
293
- end
294
-
295
- # @return [Array<Fixnum>]
296
- def y_yn
297
- @y_yn ||= Vedeu::Dimension.pair({ v: @y,
298
- vn: @yn,
299
- v_vn: @height,
300
- default: Vedeu::Terminal.height })
301
- end
302
-
303
- # The default values for a new instance of this class.
304
- #
305
- # @return [Hash]
306
- def defaults
307
- {
308
- centred: false,
309
- client: nil,
310
- height: Vedeu::Terminal.height,
311
- name: '',
312
- width: Vedeu::Terminal.width,
313
- x: 1,
314
- xn: Vedeu::Terminal.width,
315
- y: 1,
316
- yn: Vedeu::Terminal.height,
317
- }
162
+ def _xn
163
+ @xn.is_a?(Proc) ? @xn.call : @xn
318
164
  end
319
165
 
320
166
  end # Geometry