vedeu 0.6.34 → 0.6.35

Sign up to get free protection for your applications and to get access to all the features.
@@ -73,6 +73,7 @@ module Vedeu
73
73
  #
74
74
  class DSL
75
75
 
76
+ include Vedeu::Common
76
77
  include Vedeu::DSL
77
78
  include Vedeu::DSL::Use
78
79
 
@@ -94,6 +95,100 @@ module Vedeu
94
95
  Vedeu::Geometry::Geometry.build(name: name, &block).store
95
96
  end
96
97
 
98
+ # Instructs Vedeu to align the interface/view either left,
99
+ # centre or right. A width is also required so that required
100
+ # coordinates are calculated correctly.
101
+ #
102
+ # Vedeu.geometry :some_interface do
103
+ # # :some_value must be one of: :left, :none, :center,
104
+ # # :centre, or :right
105
+ # # `width` is a positive integer, e.g. 30
106
+ # alignment(:some_value, width)
107
+ #
108
+ # # ... some code
109
+ # end
110
+ #
111
+ # @param value [Symbol] One of :center, :centre, :left, :none,
112
+ # :right.
113
+ # @param width [Fixnum] The width of the aligned interface/view.
114
+ # @raise [Vedeu::Error::InvalidSyntax] When the value or width
115
+ # is not given.
116
+ # @return [Vedeu::Geometry::Geometry]
117
+ def alignment(value, width)
118
+ fail Vedeu::Error::InvalidSyntax,
119
+ 'No alignment given. Valid values are :center, :centre, :left, ' \
120
+ ':none, :right.'.freeze unless present?(value)
121
+ fail Vedeu::Error::InvalidSyntax,
122
+ 'No width given.'.freeze unless present?(width)
123
+
124
+ model.alignment = Vedeu::Geometry::Alignment.align(value)
125
+ model.width = width
126
+ model
127
+ end
128
+
129
+ # Align the interface/view centrally.
130
+ #
131
+ # Vedeu.geometry :some_interface do
132
+ # # `width` is a positive integer, e.g. 30
133
+ # align_centre 30
134
+ #
135
+ # # this is the same as:
136
+ # # alignment(:centre, 30)
137
+ #
138
+ # # ... some code
139
+ # end
140
+ #
141
+ # # Also allows `align_center` if preferred.
142
+ #
143
+ # @param width [Fixnum] The number of characters/columns.
144
+ # @raise [Vedeu::Error::InvalidSyntax] When the width is not
145
+ # given.
146
+ # @return [Vedeu::Geometry::Geometry]
147
+ def align_centre(width)
148
+ alignment(:centre, width)
149
+ end
150
+ alias_method :align_center, :align_centre
151
+
152
+ # Align the interface/view to the left.
153
+ #
154
+ # Vedeu.geometry :some_interface do
155
+ # # `width` is a positive integer, e.g. 30
156
+ # align_left 30
157
+ #
158
+ # # this is the same as:
159
+ # # alignment(:left, 30)
160
+ #
161
+ # # ... some code
162
+ # end
163
+ #
164
+ # @param width [Fixnum] The number of characters/columns.
165
+ # @raise [Vedeu::Error::InvalidSyntax] When the width is not
166
+ # given.
167
+ # @return [Vedeu::Geometry::Geometry]
168
+ def align_left(width)
169
+ alignment(:left, width)
170
+ end
171
+
172
+ # Align the interface/view to the right.
173
+ #
174
+ # Vedeu.geometry :some_interface do
175
+ # # `width` is a positive integer, e.g. 30
176
+ # align_right 30
177
+ #
178
+ # # this is the same as:
179
+ # # alignment(:right, 30)
180
+ #
181
+ # # ... some code
182
+ # end
183
+ #
184
+ # @param width [Fixnum] The number of characters/columns.
185
+ # @raise [Vedeu::Error::InvalidSyntax] When the width is not
186
+ # given.
187
+ # @return [Vedeu::Geometry::Geometry]
188
+ def align_right(width)
189
+ alignment(:right, width)
190
+ end
191
+
97
192
  # Instructs Vedeu to calculate x and y geometry automatically
98
193
  # based on the centre character of the terminal, the width and
99
194
  # the height.
@@ -178,7 +273,7 @@ module Vedeu
178
273
  # # ... some code
179
274
  # end
180
275
  #
181
- # @param value [Fixnum]
276
+ # @param value [Fixnum] The number of characters/columns.
182
277
  # @return [Fixnum]
183
278
  def width(value)
184
279
  model.width = proc { value }
@@ -31,6 +31,10 @@ module Vedeu
31
31
  :height,
32
32
  :width
33
33
 
34
+ # @!attribute [rw] alignment
35
+ # @return [Symbol]
36
+ attr_accessor :alignment
37
+
34
38
  # @!attribute [rw] centred
35
39
  # @return [Boolean]
36
40
  attr_accessor :centred
@@ -101,6 +105,7 @@ module Vedeu
101
105
  # @return [Hash]
102
106
  def attributes
103
107
  {
108
+ alignment: @alignment,
104
109
  client: @client,
105
110
  centred: @centred,
106
111
  height: height,
@@ -222,6 +227,7 @@ module Vedeu
222
227
  # @return [Hash<Symbol => Boolean, Fixnum>]
223
228
  def area_attributes
224
229
  {
230
+ alignment: @alignment,
225
231
  centred: @centred,
226
232
  maximised: @maximised,
227
233
  x: @x.is_a?(Proc) ? @x.call : @x,
@@ -234,8 +240,9 @@ module Vedeu
234
240
  end
235
241
 
236
242
  # When moving an interface;
237
- # 1) Reset the centred and maximised states to false; it wont be
238
- # centred if moved, and cannot be moved if maximised.
243
+ # 1) Reset the alignment, centred and maximised states to false;
244
+ # it wont be aligned to a side if moved, nor centred if
245
+ # moved, and cannot be moved if maximised.
239
246
  # 2) Get the current coordinates of the interface, then:
240
247
  # 3) Override the attributes with the new coordinates for
241
248
  # desired movement; these are usually +/- 1 of the current
@@ -250,7 +257,9 @@ module Vedeu
250
257
  # @option coordinates yn [Fixnum] The ending row/line position.
251
258
  # @return [Hash<Symbol => Boolean, Fixnum>]
252
259
  def move(coordinates = {})
253
- attrs = attributes.merge!(centred: false, maximised: false)
260
+ attrs = attributes.merge!(alignment: :none,
261
+ centred: false,
262
+ maximised: false)
254
263
  .merge!(coordinates)
255
264
 
256
265
  Vedeu::Geometry::Geometry.store(attrs)
@@ -261,6 +270,7 @@ module Vedeu
261
270
  # @return [Hash]
262
271
  def defaults
263
272
  {
273
+ alignment: Vedeu::Geometry::Alignment.align(:none),
264
274
  client: nil,
265
275
  centred: false,
266
276
  height: nil,
@@ -23,7 +23,7 @@ module Vedeu
23
23
 
24
24
  # @return [Symbol]
25
25
  def translate
26
- symbols.fetch(code, code)
26
+ Vedeu::KEY_TABLE[code] || code
27
27
  end
28
28
 
29
29
  protected
@@ -32,147 +32,98 @@ module Vedeu
32
32
  # @return [String]
33
33
  attr_reader :code
34
34
 
35
- private
36
-
37
- # @return [Hash<String => Symbol>]
38
- def symbols
39
- @symbols ||= ctrl_letters.merge!(f_keys)
40
- .merge!(shift_f_keys)
41
- .merge!(ctrl_f_keys)
42
- .merge!(direction_keys)
43
- .merge!(specials)
44
- end
45
-
46
- # @return [Hash<String => Symbol>]
47
- def ctrl_letters
48
- {
49
- "\u0001" => :ctrl_a,
50
- "\u0002" => :ctrl_b,
51
-
52
- "\u0003" => :ctrl_c,
53
- "\u2404" => :ctrl_c,
54
-
55
- "\u0004" => :ctrl_d,
56
- "\u2403" => :ctrl_d,
57
-
58
- "\u0005" => :ctrl_e,
59
- "\u0006" => :ctrl_f,
60
- "\u0007" => :ctrl_g,
61
- "\u0008" => :ctrl_h,
62
- # "\u0009" => :ctrl_i, # duplicates tab
63
- "\u0010" => :ctrl_j, # produces "\n"
64
- "\u0011" => :ctrl_k,
65
- "\u0012" => :ctrl_l,
66
- "\u0013" => :ctrl_m,
67
- "\u0014" => :ctrl_n,
68
- "\u0015" => :ctrl_o,
69
- "\u0016" => :ctrl_p,
70
- "\u0017" => :ctrl_q,
71
-
72
- "\u0018" => :ctrl_r,
73
- "\u2412" => :ctrl_r,
74
-
75
- "\u0019" => :ctrl_s,
76
- # "\u0020" => :ctrl_t, # duplicates spacebar
77
- "\u0021" => :ctrl_u,
78
- "\u0022" => :ctrl_v,
79
- "\u0023" => :ctrl_w,
80
- "\u0024" => :ctrl_x,
81
- "\u0025" => :ctrl_y,
82
- "\u0026" => :ctrl_z,
83
- }
84
- end
85
-
86
- # @return [Hash<String => Symbol>]
87
- def f_keys
88
- {
89
- "\eOP" => :f1,
90
- "\eOQ" => :f2,
91
- "\eOR" => :f3,
92
- "\eOS" => :f4,
93
- "\e[15~" => :f5,
94
- "\e[17~" => :f6,
95
- "\e[18~" => :f7,
96
- "\e[19~" => :f8,
97
- "\e[20~" => :f9,
98
- "\e[21~" => :f10,
99
- "\e[23~" => :f11,
100
- "\e[24~" => :f12,
101
- }
102
- end
103
-
104
- # @return [Hash<String => Symbol>]
105
- def shift_f_keys
106
- {
107
- "\e[15;2~" => :shift_f5,
108
- "\e[17;2~" => :shift_f6,
109
- "\e[18;2~" => :shift_f7,
110
- "\e[19;2~" => :shift_f8,
111
- "\e[20;2~" => :shift_f9,
112
- "\e[21;2~" => :shift_f10,
113
- "\e[23;2~" => :shift_f11,
114
- "\e[24;2~" => :shift_f12,
115
- }
116
- end
117
-
118
- # @return [Hash<String => Symbol>]
119
- def ctrl_f_keys
120
- {
121
- "\e[15;5~" => :ctrl_f5,
122
- "\e[17;5~" => :ctrl_f6,
123
- "\e[18;5~" => :ctrl_f7,
124
- "\e[19;5~" => :ctrl_f8,
125
- "\e[20;5~" => :ctrl_f9,
126
- "\e[21;5~" => :ctrl_f10,
127
- "\e[23;5~" => :ctrl_f11,
128
- "\e[24;5~" => :ctrl_f12,
129
- }
130
- end
131
-
132
- # @return [Hash<String => Symbol>]
133
- def direction_keys
134
- {
135
- "\e[B" => :down,
136
- "\u2193" => :down,
137
- "\e[D" => :left,
138
- "\u2190" => :left,
139
- "\e[C" => :right,
140
- "\u2192" => :right,
141
- "\e[A" => :up,
142
- "\u2191" => :up,
143
- }
144
- end
145
-
146
- # @return [Hash<String => Symbol>]
147
- def specials
148
- {
149
- "\u007F" => :backspace,
150
- "\u2408" => :backspace,
151
- "\u23CE" => :carriage_return,
152
- "\e[3~" => :delete,
153
- "\u232B" => :delete,
154
- "\e[F" => :end,
155
- "\r" => :enter,
156
- "\n" => :enter,
157
- "\e" => :escape,
158
- "\u238B" => :escape,
159
- "\e[H" => :home,
160
- "\eOH" => :home,
161
- "\e[2~" => :insert,
162
- "\u240A" => :line_feed,
163
- "\e[5~" => :page_up,
164
- "\e[6~" => :page_down,
165
- "\e[1;2R" => :pause_break,
166
- "\e[1;2P" => :print_screen,
167
- "\e[1;2Q" => :scroll_lock,
168
- "\e[Z" => :shift_tab,
169
- "\t" => :tab,
170
- "\u21B9" => :tab,
171
- }
172
- end
173
-
174
35
  end # Translator
175
36
 
176
37
  end # Input
177
38
 
39
+ KEY_TABLE = {
40
+ "\u0001" => :ctrl_a,
41
+ "\u0002" => :ctrl_b,
42
+ "\u0003" => :ctrl_c,
43
+ "\u2404" => :ctrl_c,
44
+ "\u0004" => :ctrl_d,
45
+ "\u2403" => :ctrl_d,
46
+ "\u0005" => :ctrl_e,
47
+ "\u0006" => :ctrl_f,
48
+ "\u0007" => :ctrl_g,
49
+ "\u0008" => :ctrl_h,
50
+ # "\u0009" => :ctrl_i, # duplicates tab
51
+ "\u0010" => :ctrl_j, # produces "\n"
52
+ "\u0011" => :ctrl_k,
53
+ "\u0012" => :ctrl_l,
54
+ "\u0013" => :ctrl_m,
55
+ "\u0014" => :ctrl_n,
56
+ "\u0015" => :ctrl_o,
57
+ "\u0016" => :ctrl_p,
58
+ "\u0017" => :ctrl_q,
59
+ "\u0018" => :ctrl_r,
60
+ "\u2412" => :ctrl_r,
61
+ "\u0019" => :ctrl_s,
62
+ # "\u0020" => :ctrl_t, # duplicates spacebar
63
+ "\u0021" => :ctrl_u,
64
+ "\u0022" => :ctrl_v,
65
+ "\u0023" => :ctrl_w,
66
+ "\u0024" => :ctrl_x,
67
+ "\u0025" => :ctrl_y,
68
+ "\u0026" => :ctrl_z,
69
+ "\eOP" => :f1,
70
+ "\eOQ" => :f2,
71
+ "\eOR" => :f3,
72
+ "\eOS" => :f4,
73
+ "\e[15~" => :f5,
74
+ "\e[17~" => :f6,
75
+ "\e[18~" => :f7,
76
+ "\e[19~" => :f8,
77
+ "\e[20~" => :f9,
78
+ "\e[21~" => :f10,
79
+ "\e[23~" => :f11,
80
+ "\e[24~" => :f12,
81
+ "\e[15;2~" => :shift_f5,
82
+ "\e[17;2~" => :shift_f6,
83
+ "\e[18;2~" => :shift_f7,
84
+ "\e[19;2~" => :shift_f8,
85
+ "\e[20;2~" => :shift_f9,
86
+ "\e[21;2~" => :shift_f10,
87
+ "\e[23;2~" => :shift_f11,
88
+ "\e[24;2~" => :shift_f12,
89
+ "\e[15;5~" => :ctrl_f5,
90
+ "\e[17;5~" => :ctrl_f6,
91
+ "\e[18;5~" => :ctrl_f7,
92
+ "\e[19;5~" => :ctrl_f8,
93
+ "\e[20;5~" => :ctrl_f9,
94
+ "\e[21;5~" => :ctrl_f10,
95
+ "\e[23;5~" => :ctrl_f11,
96
+ "\e[24;5~" => :ctrl_f12,
97
+ "\e[B" => :down,
98
+ "\u2193" => :down,
99
+ "\e[D" => :left,
100
+ "\u2190" => :left,
101
+ "\e[C" => :right,
102
+ "\u2192" => :right,
103
+ "\e[A" => :up,
104
+ "\u2191" => :up,
105
+ "\u007F" => :backspace,
106
+ "\u2408" => :backspace,
107
+ "\u23CE" => :carriage_return,
108
+ "\e[3~" => :delete,
109
+ "\u232B" => :delete,
110
+ "\e[F" => :end,
111
+ "\r" => :enter,
112
+ "\n" => :enter,
113
+ "\e" => :escape,
114
+ "\u238B" => :escape,
115
+ "\e[H" => :home,
116
+ "\eOH" => :home,
117
+ "\e[2~" => :insert,
118
+ "\u240A" => :line_feed,
119
+ "\e[5~" => :page_up,
120
+ "\e[6~" => :page_down,
121
+ "\e[1;2R" => :pause_break,
122
+ "\e[1;2P" => :print_screen,
123
+ "\e[1;2Q" => :scroll_lock,
124
+ "\e[Z" => :shift_tab,
125
+ "\t" => :tab,
126
+ "\u21B9" => :tab,
127
+ }.freeze
128
+
178
129
  end # Vedeu
@@ -33,19 +33,12 @@ module Vedeu
33
33
  return storage unless focus
34
34
 
35
35
  by_name(name)
36
- storage
37
36
 
38
37
  else
39
38
  Vedeu.log(type: :store,
40
39
  message: "Storing focus entry: '#{name}'".freeze)
41
40
 
42
- if focus
43
- storage.unshift(name)
44
-
45
- else
46
- storage.push(name)
47
-
48
- end
41
+ focus ? storage.unshift(name) : storage.push(name)
49
42
  end
50
43
  end
51
44
 
@@ -63,11 +56,7 @@ module Vedeu
63
56
  # @return [String|Symbol] The name of the interface now in
64
57
  # focus.
65
58
  def by_name(name)
66
- unless registered?(name)
67
- fail Vedeu::Error::ModelNotFound,
68
- "Cannot focus '#{name}' as this interface has not been " \
69
- 'registered.'.freeze
70
- end
59
+ not_registered! unless registered?(name)
71
60
 
72
61
  storage.rotate!(storage.index(name))
73
62
 
@@ -84,9 +73,7 @@ module Vedeu
84
73
  def current
85
74
  return storage[0] unless empty?
86
75
 
87
- fail Vedeu::Error::Fatal,
88
- 'No interfaces or views have been registered, therefore the ' \
89
- 'focus table is empty.'.freeze
76
+ no_interfaces_registered!
90
77
  end
91
78
  alias_method :focus, :current
92
79
 
@@ -212,6 +199,20 @@ module Vedeu
212
199
 
213
200
  private
214
201
 
202
+ # @raise [Vedeu::Error::Fatal]
203
+ def no_interfaces_registered!
204
+ fail Vedeu::Error::Fatal,
205
+ 'No interfaces or views have been registered, therefore the ' \
206
+ 'focus table is empty.'.freeze
207
+ end
208
+
209
+ # @raise [Vedeu::Error::ModelNotFound]
210
+ def not_registered!
211
+ fail Vedeu::Error::ModelNotFound,
212
+ "Cannot focus '#{name}' as this interface has not been " \
213
+ 'registered.'.freeze
214
+ end
215
+
215
216
  # Return the name of the interface in focus after triggering the
216
217
  # refresh event for that interface. Returns false when the
217
218
  # storage is empty.
@@ -220,8 +221,7 @@ module Vedeu
220
221
  def update
221
222
  return false if empty?
222
223
 
223
- Vedeu.log(type: :info,
224
- message: "Interface in focus: '#{current}'".freeze)
224
+ Vedeu.log(message: "Interface in focus: '#{current}'".freeze)
225
225
 
226
226
  refresh
227
227