vedeu 0.4.22 → 0.4.23

Sign up to get free protection for your applications and to get access to all the features.
Files changed (45) hide show
  1. checksums.yaml +4 -4
  2. data/Rakefile +5 -5
  3. data/examples/geometry_app.rb +2 -2
  4. data/lib/vedeu/bindings.rb +2 -15
  5. data/lib/vedeu/buffers/buffer.rb +2 -6
  6. data/lib/vedeu/configuration/cli.rb +4 -4
  7. data/lib/vedeu/dsl/components/border.rb +24 -8
  8. data/lib/vedeu/dsl/components/geometry.rb +10 -0
  9. data/lib/vedeu/events/event.rb +1 -2
  10. data/lib/vedeu/geometry/area.rb +4 -1
  11. data/lib/vedeu/geometry/geometry.rb +51 -18
  12. data/lib/vedeu/geometry/grid.rb +4 -28
  13. data/lib/vedeu/launcher.rb +6 -2
  14. data/lib/vedeu/models/line.rb +1 -1
  15. data/lib/vedeu/output/clear.rb +8 -8
  16. data/lib/vedeu/output/compressor.rb +1 -0
  17. data/lib/vedeu/output/esc.rb +29 -101
  18. data/lib/vedeu/output/html_char.rb +9 -9
  19. data/lib/vedeu/output/refresh.rb +17 -5
  20. data/lib/vedeu/output/renderers/all.rb +1 -9
  21. data/lib/vedeu/output/translator.rb +1 -1
  22. data/lib/vedeu/output/viewport.rb +1 -1
  23. data/lib/vedeu/repositories/repositories/colours.rb +6 -3
  24. data/lib/vedeu/support/all.rb +1 -0
  25. data/lib/vedeu/support/template.rb +2 -1
  26. data/lib/vedeu/support/terminal.rb +2 -0
  27. data/lib/vedeu/support/timer.rb +44 -0
  28. data/test/lib/vedeu/buffers/buffer_test.rb +1 -7
  29. data/test/lib/vedeu/configuration/cli_test.rb +28 -0
  30. data/test/lib/vedeu/dsl/components/geometry_test.rb +12 -0
  31. data/test/lib/vedeu/geometry/geometry_test.rb +8 -0
  32. data/test/lib/vedeu/geometry/grid_test.rb +0 -20
  33. data/test/lib/vedeu/geometry/position_test.rb +6 -0
  34. data/test/lib/vedeu/null/border_test.rb +6 -0
  35. data/test/lib/vedeu/output/clear_test.rb +12 -6
  36. data/test/lib/vedeu/output/compressor_test.rb +29 -0
  37. data/test/lib/vedeu/output/esc_test.rb +26 -26
  38. data/test/lib/vedeu/output/presentation_test.rb +22 -0
  39. data/test/lib/vedeu/output/translator_test.rb +18 -16
  40. data/test/lib/vedeu/output/viewport_test.rb +163 -97
  41. data/test/lib/vedeu/output/virtual_terminal_test.rb +6 -0
  42. data/test/lib/vedeu/repositories/repositories/colours_test.rb +29 -4
  43. data/test/lib/vedeu/repositories/repository_test.rb +6 -0
  44. data/vedeu.gemspec +4 -4
  45. metadata +3 -44
@@ -51,7 +51,7 @@ module Vedeu
51
51
  def chars
52
52
  return [] if empty?
53
53
 
54
- streams.map(&:chars).flatten
54
+ streams.flat_map(&:chars)
55
55
  end
56
56
 
57
57
  # Returns a boolean indicating whether the line has content.
@@ -65,7 +65,7 @@ module Vedeu
65
65
  Vedeu::Char.new(value: ' ',
66
66
  colour: clear_colour,
67
67
  style: style,
68
- position: position(iy, ix))
68
+ position: Vedeu::IndexPosition[iy, ix, *position])
69
69
  end
70
70
  end
71
71
  else
@@ -105,9 +105,9 @@ module Vedeu
105
105
  # @return [Vedeu::Colour] The default background and foreground colours for
106
106
  # the terminal, or the colours of the interface.
107
107
  def clear_colour
108
- if use_terminal_colours?
109
- @colour ||= Vedeu::Colour.new(background: :default,
110
- foreground: :default)
108
+ @colour ||= if use_terminal_colours?
109
+ Vedeu::Colour.new(background: :default,
110
+ foreground: :default)
111
111
 
112
112
  else
113
113
  colour
@@ -149,12 +149,12 @@ module Vedeu
149
149
  # @param iy [Fixnum]
150
150
  # @param ix [Fixnum]
151
151
  # @return [Vedeu::IndexPosition]
152
- def position(iy, ix)
153
- if clear_border?
154
- Vedeu::IndexPosition[iy, ix, y, x]
152
+ def position
153
+ @position ||= if clear_border?
154
+ [y, x]
155
155
 
156
156
  else
157
- Vedeu::IndexPosition[iy, ix, by, bx]
157
+ [by, bx]
158
158
 
159
159
  end
160
160
  end
@@ -13,6 +13,7 @@ module Vedeu
13
13
  @style = ''
14
14
  end
15
15
 
16
+ # @note Takes approximately ~70ms for 2100 chars. (2015-05-24)
16
17
  # @return [String]
17
18
  def render
18
19
  Array(output).flatten.map do |char|
@@ -22,23 +22,9 @@ module Vedeu
22
22
  # Esc.on_blue { 'some text' } # => "\e[44msome text\e[49m"
23
23
  #
24
24
  # # Valid names:
25
- # :black
26
- # :red
27
- # :green
28
- # :yellow
29
- # :blue
30
- # :magenta
31
- # :cyan
32
- # :light_grey
33
- # :default
34
- # :dark_grey
35
- # :light_red
36
- # :light_green
37
- # :light_yellow
38
- # :light_blue
39
- # :light_magenta
40
- # :light_cyan
41
- # :white
25
+ # :black, :red, :green, :yellow, :blue, :magenta, :cyan, :light_grey,
26
+ # :default, :dark_grey, :light_red, :light_green, :light_yellow,
27
+ # :light_blue, :light_magenta, :light_cyan, :white
42
28
  #
43
29
  # @return [Hash<Symbol => Fixnum>]
44
30
  def codes
@@ -88,6 +74,32 @@ module Vedeu
88
74
  end
89
75
  end
90
76
 
77
+ # @return [Hash<Symbol => String>]
78
+ def actions
79
+ {
80
+ hide_cursor: "\e[?25l",
81
+ show_cursor: "\e[?25h",
82
+ bg_reset: "\e[49m",
83
+ blink: "\e[5m",
84
+ blink_off: "\e[25m",
85
+ bold: "\e[1m",
86
+ bold_off: "\e[22m",
87
+ border_on: "\e(0",
88
+ border_off: "\e(B",
89
+ dim: "\e[2m",
90
+ fg_reset: "\e[39m",
91
+ negative: "\e[7m",
92
+ positive: "\e[27m",
93
+ reset: "\e[0m",
94
+ underline: "\e[4m",
95
+ underline_off: "\e[24m",
96
+ }
97
+ end
98
+
99
+ actions.each do |key, code|
100
+ define_method(key) { code }
101
+ end
102
+
91
103
  # Return the stream with the escape sequences escaped so that they can be
92
104
  # printed to the terminal instead of being interpreted by the terminal which
93
105
  # will render them. This way we can see what escape sequences are being sent
@@ -101,16 +113,6 @@ module Vedeu
101
113
  stream.gsub(/\e/, '\\e')
102
114
  end
103
115
 
104
- # @return [String]
105
- def hide_cursor
106
- "\e[?25l"
107
- end
108
-
109
- # @return [String]
110
- def show_cursor
111
- "\e[?25h"
112
- end
113
-
114
116
  # Return the escape sequence string from the list of recognised sequence
115
117
  # 'commands', or an empty string if the 'command' cannot be found.
116
118
  #
@@ -140,45 +142,6 @@ module Vedeu
140
142
 
141
143
  private
142
144
 
143
- # @return [String]
144
- def bg_reset
145
- "\e[49m"
146
- end
147
-
148
- # @return [String]
149
- def blink
150
- "\e[5m"
151
- end
152
-
153
- # @return [String]
154
- def blink_off
155
- "\e[25m"
156
- end
157
-
158
- # @return [String]
159
- def bold
160
- "\e[1m"
161
- end
162
-
163
- # @return [String]
164
- def bold_off
165
- "\e[22m"
166
- end
167
-
168
- # Returns the escape sequence to start a border.
169
- #
170
- # @return [String]
171
- def border_on
172
- "\e(0"
173
- end
174
-
175
- # Returns the escape sequence to end a border.
176
- #
177
- # @return [String]
178
- def border_off
179
- "\e(B"
180
- end
181
-
182
145
  # @return [String]
183
146
  def clear
184
147
  [colour_reset, "\e[2J"].join
@@ -199,36 +162,11 @@ module Vedeu
199
162
  [fg_reset, bg_reset].join
200
163
  end
201
164
 
202
- # @return [String]
203
- def dim
204
- "\e[2m"
205
- end
206
-
207
- # @return [String]
208
- def fg_reset
209
- "\e[39m"
210
- end
211
-
212
- # @return [String]
213
- def negative
214
- "\e[7m"
215
- end
216
-
217
165
  # @return [String]
218
166
  def normal
219
167
  [underline_off, bold_off, positive].join
220
168
  end
221
169
 
222
- # @return [String]
223
- def positive
224
- "\e[27m"
225
- end
226
-
227
- # @return [String]
228
- def reset
229
- "\e[0m"
230
- end
231
-
232
170
  # @return [String]
233
171
  def screen_init
234
172
  [reset, clear, hide_cursor].join
@@ -239,16 +177,6 @@ module Vedeu
239
177
  [show_cursor, colour_reset, reset].join
240
178
  end
241
179
 
242
- # @return [String]
243
- def underline
244
- "\e[4m"
245
- end
246
-
247
- # @return [String]
248
- def underline_off
249
- "\e[24m"
250
- end
251
-
252
180
  end # Esc
253
181
 
254
182
  end # Vedeu
@@ -55,21 +55,21 @@ module Vedeu
55
55
  # @return [String]
56
56
  def border_style
57
57
  case border
58
- when :top_horizontal then border_css('top')
59
- when :left_vertical then border_css('left')
60
- when :right_vertical then border_css('right')
61
- when :bottom_horizontal then border_css('bottom')
62
- when :top_left then [border_css('top'), border_css('left')].join
63
- when :top_right then [border_css('top'), border_css('right')].join
64
- when :bottom_left then [border_css('bottom'), border_css('left')].join
65
- when :bottom_right then [border_css('bottom'), border_css('right')].join
58
+ when :top_horizontal then css('top')
59
+ when :left_vertical then css('left')
60
+ when :right_vertical then css('right')
61
+ when :bottom_horizontal then css('bottom')
62
+ when :top_left then [css('top'), css('left')].join
63
+ when :top_right then [css('top'), css('right')].join
64
+ when :bottom_left then [css('bottom'), css('left')].join
65
+ when :bottom_right then [css('bottom'), css('right')].join
66
66
  else
67
67
  ''
68
68
  end
69
69
  end
70
70
 
71
71
  # @return [String]
72
- def border_css(direction = '')
72
+ def css(direction = '')
73
73
  "border-#{direction}:1px #{fg} solid;"
74
74
  end
75
75
 
@@ -10,7 +10,13 @@ module Vedeu
10
10
  #
11
11
  # @return [Array]
12
12
  def all
13
- Vedeu.interfaces.registered.each { |name| by_name(name) }
13
+ message = "Refreshing all interfaces"
14
+
15
+ Vedeu.log(type: :info, message: message)
16
+
17
+ Vedeu::Timer.for(message) do
18
+ Vedeu.interfaces.registered.each { |name| by_name(name) }
19
+ end
14
20
  end
15
21
 
16
22
  # Refresh the interface which is currently focussed.
@@ -26,9 +32,13 @@ module Vedeu
26
32
  # @return [Array|ModelNotFound] A collection of the names of interfaces
27
33
  # refreshed, or an exception if the group was not found.
28
34
  def by_group(group_name)
29
- Vedeu.log(type: :info, message: "Refreshing group: '#{group_name}'")
35
+ message = "Refreshing group: '#{group_name}'"
30
36
 
31
- Vedeu.groups.find!(group_name).members.each { |name| by_name(name) }
37
+ Vedeu.log(type: :info, message: message)
38
+
39
+ Vedeu::Timer.for(message) do
40
+ Vedeu.groups.find!(group_name).members.each { |name| by_name(name) }
41
+ end
32
42
  end
33
43
 
34
44
  # Refresh an interface by name.
@@ -37,9 +47,11 @@ module Vedeu
37
47
  # named buffer.
38
48
  # @return [Array|ModelNotFound]
39
49
  def by_name(name)
40
- Vedeu.log(type: :info, message: "Refreshing interface: '#{name}'")
50
+ message = "Refreshing interface: '#{name}'"
51
+
52
+ Vedeu.log(type: :info, message: message)
41
53
 
42
- Vedeu.buffers.by_name(name).render
54
+ Vedeu::Timer.for(message) { Vedeu.buffers.by_name(name).render }
43
55
  end
44
56
 
45
57
  end # Refresh
@@ -23,24 +23,16 @@ module Vedeu
23
23
 
24
24
  # @return [Array<void>]
25
25
  def render(*args)
26
- # acc = []
27
26
  threads = storage.map do |renderer|
28
27
  Thread.new(renderer) do
29
28
  mutex.synchronize do
30
29
  Vedeu.log(type: :debug,
31
- message: "Sending to renderer: '#{renderer}'")
32
- # acc << renderer.render(*args)
30
+ message: "Rendering with: '#{renderer}' (#{storage.size} defined)")
33
31
  renderer.render(*args)
34
32
  end
35
33
  end
36
34
  end
37
35
  threads.each(&:join)
38
- # acc
39
-
40
- # storage.inject([]) do |acc, renderer|
41
- # acc << renderer.render(*args)
42
- # acc
43
- # end
44
36
  end
45
37
 
46
38
  # Adds the given renderer class(es) to the list of renderers.
@@ -33,7 +33,7 @@ module Vedeu
33
33
  # @return [Object]
34
34
  def self.coerce(value)
35
35
  if value.nil?
36
- new
36
+ ''
37
37
 
38
38
  elsif value.is_a?(self)
39
39
  value
@@ -57,7 +57,7 @@ module Vedeu
57
57
  out = []
58
58
  show[0...height].each_with_index do |line, iy|
59
59
  line[0...width].each_with_index do |column, ix|
60
- column.position = IndexPosition[iy, ix, by, bx]
60
+ column.position = Vedeu::IndexPosition[iy, ix, by, bx]
61
61
  out << column
62
62
  end
63
63
  end
@@ -24,8 +24,11 @@ module Vedeu
24
24
 
25
25
  # Returns a boolean indicating whether the colour has been registered.
26
26
  #
27
+ # @param colour [String]
27
28
  # @return [Boolean]
28
29
  def registered?(colour)
30
+ return false unless colour
31
+
29
32
  storage.key?(colour)
30
33
  end
31
34
 
@@ -36,15 +39,15 @@ module Vedeu
36
39
  storage.clear
37
40
  end
38
41
 
39
- # Retrieves a registered colour.
42
+ # Retrieves the escape sequence of a registered colour.
40
43
  #
41
44
  # @return [String]
42
45
  def retrieve(colour)
43
46
  storage.fetch(colour, '')
44
47
  end
45
48
 
46
- # Retrieves a registered colour, or registers the colour with its respective
47
- # escape sequence.
49
+ # Retrieves the escape sequence of a registered colour, or registers the
50
+ # colour with its respective escape sequence.
48
51
  #
49
52
  # @return [String]
50
53
  def retrieve_or_register(colour, escape_sequence)
@@ -2,5 +2,6 @@ require_relative 'common'
2
2
  require_relative 'options'
3
3
  require_relative 'template'
4
4
  require_relative 'terminal'
5
+ require_relative 'timer'
5
6
  require_relative 'trace'
6
7
  require_relative 'visibility'
@@ -16,7 +16,8 @@ module Vedeu
16
16
  # @param path [String]
17
17
  # @return [Template]
18
18
  def initialize(object, path)
19
- @object, @path = object, path.to_s
19
+ @object = object
20
+ @path = path.to_s
20
21
  end
21
22
 
22
23
  # @return [void]
@@ -37,6 +37,8 @@ module Vedeu
37
37
  #
38
38
  # @return [String]
39
39
  def input
40
+ Vedeu.log(type: :debug, message: "--- Waiting for user input")
41
+
40
42
  keys_or_cmd = if raw_mode?
41
43
  keys = console.getch
42
44
 
@@ -0,0 +1,44 @@
1
+ module Vedeu
2
+
3
+ # Measure the duration. Used for debugging.
4
+ class Timer
5
+
6
+ # @param message [String]
7
+ # @param block [Proc]
8
+ # @return [void]
9
+ def self.for(message = '', &block)
10
+ new(message).measure(&block)
11
+ end
12
+
13
+ # @param message [String]
14
+ # @return [Vedeu::Timer]
15
+ def initialize(message = '')
16
+ @message = message
17
+ @started = Time.now.to_f
18
+ end
19
+
20
+ # @param block [Proc]
21
+ # @return [void]
22
+ def measure(&block)
23
+ work = yield
24
+
25
+ elapsed = ((Time.now.to_f - started) * 1000).to_i
26
+
27
+ Vedeu.log(type: :debug, message: "#{message} took #{elapsed}ms.")
28
+
29
+ work
30
+ end
31
+
32
+ protected
33
+
34
+ # @!attribute [r] started
35
+ # @return [Time]
36
+ attr_reader :started
37
+
38
+ # @!attribute [r] message
39
+ # @return [String]
40
+ attr_reader :message
41
+
42
+ end # Timer
43
+
44
+ end # Vedeu