vedeu 0.6.34 → 0.6.35

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 12cdb96715e44a6ed789be72c594f11a2fca50c4
4
- data.tar.gz: 5fb1b73c0e64a16bfaa3c227169fec1ad8d69475
3
+ metadata.gz: 19a9f0cb2cd4e5abc3c7df887e379390215d4a98
4
+ data.tar.gz: 42cd6518e55b29886c270ba7f41c7938087faf7a
5
5
  SHA512:
6
- metadata.gz: dcce80eac99efd8c58394bf7bc595ce26ec4534288357894afbbbb7ac7abe76dd4b4f1f80f5500b6208dbfe0be58882ad7c455a4e6f669d937a0b866ab2958df
7
- data.tar.gz: e55fbfa42ce9f608f0f28dffa9584f763a5f6ae8126e03de8c841fde5377612d67d7437a33b5962c63119a14eccce43b1cf4e0933982b65e1751ba7372080638
6
+ metadata.gz: 4aab96b5aca113d6e65b34c58d35a9cd7a277c81e1c12ad80a0cc5734df760e1e8737677464eeb8758d9b110eea806c34f22dc6205f39db0d9aa03ff6a6a3041
7
+ data.tar.gz: 56be459ebb0bbac1184447c6badc767624743dd7b224b5b86bf5480e41aef4dbae2fa6ad56b50f42987855c6f7e7066c852815c2f37800d8978d878fd2b28d8b
data/docs/api.md CHANGED
@@ -67,6 +67,11 @@ See {file:docs/events.md}
67
67
  - focus!
68
68
  - foreground
69
69
  - geometry
70
+ - alignment
71
+ - align_left
72
+ - align_center
73
+ - align_centre
74
+ - align_right
70
75
  - centred (or centred!)
71
76
  - height
72
77
  - width
data/docs/dsl.md CHANGED
@@ -109,6 +109,10 @@ end
109
109
 
110
110
  ### Setting the interface dimensions
111
111
 
112
+ {include:Vedeu::Geometry::DSL#alignment}
113
+ {include:Vedeu::Geometry::DSL#align_left}
114
+ {include:Vedeu::Geometry::DSL#align_centre}
115
+ {include:Vedeu::Geometry::DSL#align_right}
112
116
  {include:Vedeu::Geometry::DSL#centred}
113
117
  {include:Vedeu::Geometry::DSL#height}
114
118
  {include:Vedeu::Geometry::DSL#width}
data/docs/geometry.md CHANGED
@@ -24,5 +24,8 @@ of the terminal.
24
24
  - Geometry can be maximised- to use all the available terminal space.
25
25
  - It can also be unmaximised- to return to the pre-defined dimensions
26
26
  as mentioned above.
27
+ - Geometry can be aligned; either left, centre or right. When defined
28
+ in this way, the 'x', 'xn', 'y' and 'yn' parameters will be
29
+ calculated automatically.
27
30
  - Geometry like a {file:docs/cursors.md Cursor}, via events can be
28
31
  moved; left, right, up or down.
@@ -0,0 +1,183 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'vedeu'
5
+
6
+ class AlignmentApp
7
+
8
+ def self.geometry_stats(name)
9
+ Vedeu.geometries.by_name(name)
10
+ end
11
+ def self.gl
12
+ geometry_stats(:left_interface)
13
+ end
14
+ def self.gc
15
+ geometry_stats(:centre_interface)
16
+ end
17
+ def self.gr
18
+ geometry_stats(:right_interface)
19
+ end
20
+
21
+ def self.border_stats(name)
22
+ Vedeu.borders.by_name(name)
23
+ end
24
+ def self.bl
25
+ border_stats(:left_interface)
26
+ end
27
+ def self.bc
28
+ border_stats(:centre_interface)
29
+ end
30
+ def self.br
31
+ border_stats(:right_interface)
32
+ end
33
+
34
+ class CentreAlignmentView
35
+ def render
36
+ Vedeu.render do
37
+ view(:centre_interface) do
38
+ lines do
39
+ line "x:#{gc.x}, xn:#{gc.xn} (w:#{gc.width})"
40
+ line "y:#{gc.y}, yn:#{gc.yn} (h:#{gc.height})"
41
+ line ""
42
+ line "bx:#{bc.bx}, bxn:#{bc.bxn} (bw:#{bc.width})"
43
+ line "by:#{bc.by}, byn:#{bc.byn} (bh:#{bc.height})"
44
+ line ""
45
+ line "The 'h', 'j', 'k' and 'l' keys will"
46
+ line "move this view left, down, up, and"
47
+ line "right respectively."
48
+ end
49
+ end
50
+ end
51
+
52
+ Vedeu.trigger(:_refresh_view, :centre_interface)
53
+ end
54
+
55
+ def gc
56
+ Vedeu.geometries.by_name(:centre_interface)
57
+ end
58
+
59
+ def bc
60
+ Vedeu.borders.by_name(:centre_interface)
61
+ end
62
+ end
63
+
64
+ Vedeu.bind(:_initialize_) { Vedeu.trigger(:_refresh_) }
65
+
66
+ Vedeu.configure do
67
+ log './tmp/alignment.log'
68
+ renderers Vedeu::Renderers::File.new(filename: './tmp/alignment.out')
69
+ end
70
+
71
+ Vedeu.interface :left_interface do
72
+ border do
73
+ title 'Align: Left'
74
+ end
75
+ geometry do
76
+ alignment :left, 30
77
+
78
+ # or you can use:
79
+ # align_left 30
80
+
81
+ height 10
82
+ end
83
+ end
84
+
85
+ Vedeu.interface :centre_interface do
86
+ border do
87
+ title 'Align: Centre'
88
+ end
89
+ geometry do
90
+ alignment :centre, 40
91
+
92
+ # or you can use:
93
+ # align_centre 30
94
+
95
+ height 15
96
+ end
97
+ end
98
+
99
+ Vedeu.interface :right_interface do
100
+ border do
101
+ title 'Align: Right'
102
+ end
103
+ geometry do
104
+ alignment :right, 30
105
+
106
+ # or you can use:
107
+ # align_right 30
108
+
109
+ height 10
110
+ end
111
+ end
112
+
113
+ Vedeu.keymap '_global_' do
114
+ key('q') { Vedeu.exit }
115
+
116
+ key('h') {
117
+ Vedeu.trigger(:_view_left_, :centre_interface)
118
+
119
+ AlignmentApp::CentreAlignmentView.new.render
120
+ }
121
+ key('j') {
122
+ Vedeu.trigger(:_view_down_, :centre_interface)
123
+
124
+ AlignmentApp::CentreAlignmentView.new.render
125
+ }
126
+ key('k') {
127
+ Vedeu.trigger(:_view_up_, :centre_interface)
128
+
129
+ AlignmentApp::CentreAlignmentView.new.render
130
+ }
131
+ key('l') {
132
+ Vedeu.trigger(:_view_right_, :centre_interface)
133
+
134
+ AlignmentApp::CentreAlignmentView.new.render
135
+ }
136
+ end
137
+
138
+ Vedeu.render do
139
+ view(:left_interface) do
140
+ lines do
141
+ line "x:#{gl.x}, xn:#{gl.xn} (w:#{gl.width})"
142
+ line "y:#{gl.y}, yn:#{gl.yn} (h:#{gl.height})"
143
+ line ""
144
+ line "bx:#{bl.bx}, bxn:#{bl.bxn} (bw:#{bl.width})"
145
+ line "by:#{bl.by}, byn:#{bl.byn} (bh:#{bl.height})"
146
+ end
147
+ end
148
+
149
+ view(:centre_interface) do
150
+ lines do
151
+ line "x:#{gc.x}, xn:#{gc.xn} (w:#{gc.width})"
152
+ line "y:#{gc.y}, yn:#{gc.yn} (h:#{gc.height})"
153
+ line ""
154
+ line "bx:#{bc.bx}, bxn:#{bc.bxn} (bw:#{bc.width})"
155
+ line "by:#{bc.by}, byn:#{bc.byn} (bh:#{bc.height})"
156
+ line ""
157
+ line "The 'h', 'j', 'k' and 'l' keys will"
158
+ line "move this view left, down, up, and"
159
+ line "right respectively. The coordinates"
160
+ line "will not change in this example"
161
+ line "because they have not been"
162
+ line "instructed to do so."
163
+ end
164
+ end
165
+
166
+ view(:right_interface) do
167
+ lines do
168
+ line "x:#{gr.x}, xn:#{gr.xn} (w:#{gr.width})"
169
+ line "y:#{gr.y}, yn:#{gr.yn} (h:#{gr.height})"
170
+ line ""
171
+ line "bx:#{br.bx}, bxn:#{br.bxn} (bw:#{br.width})"
172
+ line "by:#{br.by}, byn:#{br.byn} (bh:#{br.height})"
173
+ end
174
+ end
175
+ end
176
+
177
+ def self.start(argv = ARGV)
178
+ Vedeu::Launcher.execute!(argv)
179
+ end
180
+
181
+ end # AlignmentApp
182
+
183
+ AlignmentApp.start
@@ -158,7 +158,7 @@ module Vedeu
158
158
  #
159
159
  # @return [Array<Vedeu::Models::Escape>]
160
160
  def render
161
- Vedeu.log(type: :info, message: "Refreshing cursor: '#{name}'".freeze)
161
+ Vedeu.log(message: "Refreshing cursor: '#{name}'".freeze)
162
162
 
163
163
  Vedeu.render_output(escape_sequence)
164
164
  end
@@ -0,0 +1,87 @@
1
+ module Vedeu
2
+
3
+ module Geometry
4
+
5
+ # Provides the mechanism to align an interface/view horizontally
6
+ # to the available terminal space.
7
+ #
8
+ class Alignment
9
+
10
+ # @param (see #initialize)
11
+ # @return (see #align)
12
+ def self.align(value = nil)
13
+ new(value).align
14
+ end
15
+
16
+ # @param value [NilClass|Symbol]
17
+ # @return [Vedeu::Geometry::Alignment]
18
+ def initialize(value = nil)
19
+ @value = value
20
+ end
21
+
22
+ # @raise [Vedeu::Error::InvalidSyntax] When the value is not one
23
+ # of :centre, :left, :right
24
+ # @return [Symbol]
25
+ def align
26
+ return value if valid?
27
+
28
+ fail Vedeu::Error::InvalidSyntax,
29
+ 'Cannot align as value is invalid or undefined.'.freeze
30
+ end
31
+
32
+ private
33
+
34
+ # @return [Boolean]
35
+ def align_value?
36
+ @value.to_s.start_with?('align_')
37
+ end
38
+
39
+ # @return [Symbol]
40
+ def coerced_value
41
+ coerced = @value.to_s.gsub!('align_', '').to_sym
42
+
43
+ coerced == :center ? :centre : coerced
44
+ end
45
+
46
+ # @return [Boolean]
47
+ def none?
48
+ @value.nil? || !(@value.is_a?(Symbol)) || @value == :alignment
49
+ end
50
+
51
+ # @return [Boolean]
52
+ def valid?
53
+ values.include?(value)
54
+ end
55
+
56
+ # @return [Symbol]
57
+ def value
58
+ @_value ||= if none?
59
+ :none
60
+
61
+ elsif @value == :center
62
+ :centre
63
+
64
+ elsif align_value?
65
+ coerced_value
66
+
67
+ else
68
+ @value.to_sym
69
+
70
+ end
71
+ end
72
+
73
+ # @return [Array<Symbol>]
74
+ def values
75
+ [
76
+ :centre,
77
+ :left,
78
+ :none,
79
+ :right,
80
+ ]
81
+ end
82
+
83
+ end # Alignment
84
+
85
+ end # Geometry
86
+
87
+ end # Vedeu
@@ -13,6 +13,7 @@ module Vedeu
13
13
 
14
14
  end # Vedeu
15
15
 
16
+ require 'vedeu/geometry/alignment'
16
17
  require 'vedeu/geometry/area'
17
18
  require 'vedeu/geometry/coordinate'
18
19
  require 'vedeu/geometry/dimension'
@@ -39,7 +39,9 @@ module Vedeu
39
39
  # @option attributes xn [Fixnum]
40
40
  # @option attributes x_xn [Fixnum]
41
41
  # @option attributes x_default [Fixnum]
42
- # @option attributes options [Hash<Symbol => Boolean>]
42
+ # @option attributes maximised [Boolean]
43
+ # @option attributes centred [Boolean]
44
+ # @option attributes alignment [Symbol]
43
45
  # @return [Vedeu::Geometry::Area]
44
46
  def self.from_attributes(attributes = {})
45
47
  y_attributes = {
@@ -49,6 +51,7 @@ module Vedeu
49
51
  default: Vedeu.height,
50
52
  maximised: attributes[:maximised],
51
53
  centred: attributes[:centred],
54
+ # alignment: attributes[:alignment],
52
55
  }
53
56
  x_attributes = {
54
57
  d: attributes[:x],
@@ -57,6 +60,7 @@ module Vedeu
57
60
  default: Vedeu.width,
58
61
  maximised: attributes[:maximised],
59
62
  centred: attributes[:centred],
63
+ alignment: attributes[:alignment],
60
64
  }
61
65
  y_yn = Vedeu::Geometry::Dimension.pair(y_attributes)
62
66
  x_xn = Vedeu::Geometry::Dimension.pair(x_attributes)
@@ -22,7 +22,9 @@ module Vedeu
22
22
  # @option attributes d_dn [Fixnum|NilClass] A width or a height.
23
23
  # @option attributes default [Fixnum|NilClass]
24
24
  # The terminal width or height.
25
- # @option attributes options [Hash]
25
+ # @option attributes maximised [Boolean]
26
+ # @option attributes centered [Boolean]
27
+ # @option attributes alignment [Symbol]
26
28
  # @return [Vedeu::Geometry::Dimension]
27
29
  def initialize(attributes = {})
28
30
  defaults.merge!(attributes).each do |key, value|
@@ -54,19 +56,19 @@ module Vedeu
54
56
  protected
55
57
 
56
58
  # @!attribute [r] d
57
- # @return [Fixnum|NilClass]
59
+ # @return [Fixnum|NilClass] The starting value (y or x).
58
60
  attr_reader :d
59
61
 
60
62
  # @!attribute [r] dn
61
- # @return [Fixnum|NilClass]
63
+ # @return [Fixnum|NilClass] The ending value (yn or xn).
62
64
  attr_reader :dn
63
65
 
64
66
  # @!attribute [r] d_dn
65
- # @return [Fixnum|NilClass]
67
+ # @return [Fixnum|NilClass] A width or a height.
66
68
  attr_reader :d_dn
67
69
 
68
70
  # @!attribute [r] default
69
- # @return [Fixnum|NilClass]
71
+ # @return [Fixnum|NilClass] The terminal width or height.
70
72
  attr_reader :default
71
73
 
72
74
  # @!attribute [r] maximised
@@ -79,13 +81,33 @@ module Vedeu
79
81
  attr_reader :centred
80
82
  alias_method :centred?, :centred
81
83
 
84
+ # @!attribute [r] alignment
85
+ # @return [Symbol]
86
+ attr_reader :alignment
87
+
82
88
  private
83
89
 
90
+ # Return the dimension.
91
+ #
92
+ # 1) If maximised, it will be from the first row/line or column/
93
+ # character to the last row/line or column/character of the
94
+ # terminal.
95
+ # 2) If centred,
96
+ #
84
97
  # @return [Array<Fixnum>]
85
98
  def dimension
86
99
  @dimension = if maximised?
87
100
  [1, default]
88
101
 
102
+ elsif centre_aligned?
103
+ [centred_d, centred_dn]
104
+
105
+ elsif left_aligned?
106
+ [1, left_dn]
107
+
108
+ elsif right_aligned?
109
+ [right_d, default]
110
+
89
111
  elsif centred? && length?
90
112
  [centred_d, centred_dn]
91
113
 
@@ -95,11 +117,22 @@ module Vedeu
95
117
  end
96
118
  end
97
119
 
120
+ # Return a boolean indicating we know the length if a we know
121
+ # either the terminal width or height, or we can determine a
122
+ # length from the values provided.
123
+ #
98
124
  # @return [Boolean]
125
+ # @todo GL 2015-10-16 Investigate: should this be && or ||.
99
126
  def length?
100
127
  default && length
101
128
  end
102
129
 
130
+ # Provide the number of rows/lines or columns/characters.
131
+ #
132
+ # 1) Use the starting (x or y) and ending (xn or yn) values.
133
+ # 2) Or use the width or height value.
134
+ # 3) Or use the default/current terminal width or height value.
135
+ #
103
136
  # @return [Fixnum|NilClass]
104
137
  def length
105
138
  if d && dn
@@ -108,7 +141,7 @@ module Vedeu
108
141
  elsif d_dn
109
142
  d_dn
110
143
 
111
- elsif default
144
+ else
112
145
  default
113
146
 
114
147
  end
@@ -116,16 +149,56 @@ module Vedeu
116
149
 
117
150
  # Ascertains the centred starting coordinate.
118
151
  #
152
+ # @example
153
+ # default = 78 # => 39
154
+ # length = 24 # => 12
155
+ # centred_d = 27 # (39 - 12 = 27)
156
+ #
119
157
  # @return [Fixnum]
120
158
  def centred_d
121
- (default / 2) - (length / 2)
159
+ d = (default / 2) - (length / 2)
160
+ d < 1 ? 1 : d
122
161
  end
123
162
 
124
163
  # Ascertains the centred ending coordinate.
125
164
  #
165
+ # @example
166
+ # default = 78 # => 39
167
+ # length = 24 # => 12
168
+ # centred_dn = 51 # (39 + 12 = 51)
169
+ #
126
170
  # @return [Fixnum]
127
171
  def centred_dn
128
- (default / 2) + (length / 2)
172
+ dn = (default / 2) + (length / 2)
173
+ dn > default ? default : dn
174
+ end
175
+
176
+ # @return [Fixnum]
177
+ def left_dn
178
+ if d_dn
179
+ (d_dn > default) ? default : d_dn
180
+
181
+ elsif dn
182
+ dn
183
+
184
+ else
185
+ default
186
+
187
+ end
188
+ end
189
+
190
+ # @return [Fixnum]
191
+ def right_d
192
+ if d_dn
193
+ (default - d_dn) < 1 ? 1 : (default - d_dn)
194
+
195
+ elsif d
196
+ d
197
+
198
+ else
199
+ 1
200
+
201
+ end
129
202
  end
130
203
 
131
204
  # Fetch the starting coordinate, or use 1 when not set.
@@ -139,10 +212,10 @@ module Vedeu
139
212
  #
140
213
  # 1) if an end value was given, use that.
141
214
  # 2) if a start value wasn't given, but a width or height was,
142
- # use the width or height
215
+ # use the width or height.
143
216
  # 3) if a start value was given and a width or height was given,
144
217
  # add the width or height to the start and minus 1.
145
- # 4) otherwise, use the terminal default width or height
218
+ # 4) otherwise, use the terminal default width or height.
146
219
  #
147
220
  # @return [Fixnum]
148
221
  def _dn
@@ -155,12 +228,36 @@ module Vedeu
155
228
  elsif d && d_dn
156
229
  (d + d_dn) - 1
157
230
 
158
- else
231
+ elsif default
159
232
  default
160
233
 
234
+ else
235
+ 1
236
+
161
237
  end
162
238
  end
163
239
 
240
+ # Return a boolean indicating alignment was set to :left.
241
+ #
242
+ # @return [Boolean]
243
+ def left_aligned?
244
+ alignment == :left
245
+ end
246
+
247
+ # Return a boolean indicating alignment was set to :centre.
248
+ #
249
+ # @return [Boolean]
250
+ def centre_aligned?
251
+ alignment == :centre
252
+ end
253
+
254
+ # Return a boolean indicating alignment was set to :right.
255
+ #
256
+ # @return [Boolean]
257
+ def right_aligned?
258
+ alignment == :right
259
+ end
260
+
164
261
  # Returns the default options/attributes for this class.
165
262
  #
166
263
  # @return [Hash<Symbol => NilClass,Boolean>]
@@ -172,6 +269,7 @@ module Vedeu
172
269
  default: nil,
173
270
  centred: false,
174
271
  maximised: false,
272
+ alignment: Vedeu::Geometry::Alignment.align(:none),
175
273
  }
176
274
  end
177
275