vedeu 0.1.10 → 0.1.12

Sign up to get free protection for your applications and to get access to all the features.
Files changed (47) hide show
  1. checksums.yaml +4 -4
  2. data/lib/vedeu.rb +2 -10
  3. data/lib/vedeu/api/api.rb +29 -1
  4. data/lib/vedeu/api/events.rb +1 -26
  5. data/lib/vedeu/api/{base.rb → helpers.rb} +1 -18
  6. data/lib/vedeu/api/interface.rb +16 -3
  7. data/lib/vedeu/api/line.rb +29 -4
  8. data/lib/vedeu/api/log.rb +13 -1
  9. data/lib/vedeu/api/store.rb +0 -4
  10. data/lib/vedeu/api/stream.rb +29 -2
  11. data/lib/vedeu/api/view.rb +4 -2
  12. data/lib/vedeu/models/attributes/attributes.rb +25 -0
  13. data/lib/vedeu/models/attributes/colour_translator.rb +0 -10
  14. data/lib/vedeu/models/colour.rb +23 -3
  15. data/lib/vedeu/models/composition.rb +32 -2
  16. data/lib/vedeu/models/geometry.rb +38 -22
  17. data/lib/vedeu/models/interface.rb +57 -12
  18. data/lib/vedeu/models/line.rb +28 -4
  19. data/lib/vedeu/models/stream.rb +36 -6
  20. data/lib/vedeu/output/buffers.rb +8 -4
  21. data/lib/vedeu/output/clear.rb +1 -3
  22. data/lib/vedeu/output/render.rb +12 -4
  23. data/lib/vedeu/support/esc.rb +12 -2
  24. data/lib/vedeu/support/menu.rb +10 -10
  25. data/test/lib/vedeu/api/events_test.rb +4 -4
  26. data/test/lib/vedeu/api/interface_test.rb +9 -9
  27. data/test/lib/vedeu/api/view_test.rb +1 -1
  28. data/test/lib/vedeu/models/attributes/{collection_test.rb → attributes_test.rb} +7 -7
  29. data/test/lib/vedeu/models/geometry_test.rb +0 -65
  30. data/test/lib/vedeu/models/line_test.rb +29 -4
  31. data/test/lib/vedeu/models/stream_test.rb +56 -11
  32. data/test/lib/vedeu/output/render_test.rb +1 -0
  33. data/vedeu.gemspec +1 -3
  34. metadata +6 -38
  35. data/lib/vedeu/models/attributes/background.rb +0 -7
  36. data/lib/vedeu/models/attributes/collection.rb +0 -11
  37. data/lib/vedeu/models/attributes/foreground.rb +0 -7
  38. data/lib/vedeu/models/attributes/interface_collection.rb +0 -13
  39. data/lib/vedeu/models/attributes/line_collection.rb +0 -9
  40. data/lib/vedeu/models/attributes/stream_collection.rb +0 -9
  41. data/lib/vedeu/models/style.rb +0 -15
  42. data/test/lib/vedeu/models/attributes/interface_collection_test.rb +0 -40
  43. data/test/lib/vedeu/models/attributes/line_collection_test.rb +0 -25
  44. data/test/lib/vedeu/models/attributes/stream_collection_test.rb +0 -23
  45. data/test/lib/vedeu/models/style_test.rb +0 -48
  46. data/test/support/bench.rb +0 -34
  47. data/test/support/colours.rb +0 -39
@@ -1,15 +1,35 @@
1
1
  module Vedeu
2
2
  class Geometry
3
- include Virtus.model
3
+ def initialize(attributes = {})
4
+ @attributes = attributes
5
+ end
6
+
7
+ def attributes
8
+ defaults.merge!(@attributes)
9
+ end
10
+
11
+ def y
12
+ @y ||= attributes[:y]
13
+ end
14
+
15
+ def x
16
+ @x ||= attributes[:x]
17
+ end
18
+
19
+ def width
20
+ @width ||= attributes[:width]
21
+ end
22
+
23
+ def height
24
+ @height ||= attributes[:height]
25
+ end
4
26
 
5
- attribute :y, Integer, default: 1
6
- attribute :x, Integer, default: 1
7
- attribute :width, Integer, default: Terminal.width
8
- attribute :height, Integer, default: Terminal.height
9
- attribute :centred, Boolean, default: false
27
+ def centred
28
+ @centred ||= attributes[:centred]
29
+ end
10
30
 
11
- def origin(index = 0)
12
- Esc.set_position(virtual_y[index], left)
31
+ def origin(index = 0, &block)
32
+ Esc.set_position(virtual_y[index], left, &block)
13
33
  end
14
34
 
15
35
  def top
@@ -52,20 +72,6 @@ module Vedeu
52
72
  right + value
53
73
  end
54
74
 
55
- def position
56
- {
57
- y: top,
58
- x: left,
59
- height: height,
60
- width: width,
61
- centred: centred,
62
- top: top,
63
- bottom: bottom,
64
- left: left,
65
- right: right,
66
- }
67
- end
68
-
69
75
  private
70
76
 
71
77
  def centre
@@ -83,5 +89,15 @@ module Vedeu
83
89
  def virtual_y
84
90
  @_virtual_y ||= (top..bottom).to_a
85
91
  end
92
+
93
+ def defaults
94
+ {
95
+ y: 1,
96
+ x: 1,
97
+ width: Terminal.width,
98
+ height: Terminal.height,
99
+ centred: false,
100
+ }
101
+ end
86
102
  end
87
103
  end
@@ -1,23 +1,68 @@
1
1
  module Vedeu
2
2
  class Interface
3
3
  extend Forwardable
4
- include Virtus.model
5
4
 
6
- attribute :name, String
7
- attribute :group, String
8
- attribute :lines, LineCollection
9
- attribute :colour, Colour, default: Colour.new
10
- attribute :style, Style, default: ''
11
- attribute :geometry, Geometry, default: Geometry.new
12
- attribute :cursor, Boolean, default: true
13
- attribute :delay, Float, default: 0
5
+ def_delegators :geometry, :north, :east, :south, :west,
6
+ :top, :right, :bottom, :left,
7
+ :width, :height, :origin
14
8
 
15
- def_delegators :@geometry, :north, :east, :south, :west,
16
- :top, :right, :bottom, :left,
17
- :width, :height, :origin
9
+ def initialize(attributes = {})
10
+ @attributes = attributes
11
+ end
12
+
13
+ def attributes
14
+ defaults.merge!(@attributes)
15
+ end
16
+
17
+ def name
18
+ @name ||= attributes[:name]
19
+ end
20
+
21
+ def group
22
+ @group ||= attributes[:group]
23
+ end
24
+
25
+ def lines
26
+ @lines ||= Attributes.coercer(attributes[:lines], Line, :streams)
27
+ end
28
+
29
+ def colour
30
+ @colour ||= Colour.new(attributes[:colour])
31
+ end
32
+
33
+ def style
34
+ @style ||= Attributes.coerce_styles(attributes[:style])
35
+ end
36
+
37
+ def geometry
38
+ @geometry ||= Geometry.new(attributes[:geometry])
39
+ end
40
+
41
+ def cursor
42
+ @cursor ||= attributes[:cursor]
43
+ end
44
+
45
+ def delay
46
+ @delay || attributes[:delay]
47
+ end
18
48
 
19
49
  def to_s
20
50
  Render.call(self)
21
51
  end
52
+
53
+ private
54
+
55
+ def defaults
56
+ {
57
+ name: '',
58
+ group: '',
59
+ lines: [],
60
+ colour: {},
61
+ style: '',
62
+ geometry: {},
63
+ cursor: true,
64
+ delay: 0.0
65
+ }
66
+ end
22
67
  end
23
68
  end
@@ -1,13 +1,37 @@
1
1
  module Vedeu
2
2
  class Line
3
- include Virtus.model
3
+ def initialize(attributes = {})
4
+ @attributes = attributes
5
+ end
6
+
7
+ def attributes
8
+ defaults.merge!(@attributes)
9
+ end
4
10
 
5
- attribute :colour, Colour, default: Colour.new
6
- attribute :streams, StreamCollection
7
- attribute :style, Style, default: ''
11
+ def colour
12
+ @colour ||= Colour.new(attributes[:colour])
13
+ end
14
+
15
+ def streams
16
+ @streams ||= Attributes.coercer(attributes[:streams], Stream, :text)
17
+ end
18
+
19
+ def style
20
+ @style ||= Attributes.coerce_styles(attributes[:style])
21
+ end
8
22
 
9
23
  def to_s
10
24
  [ colour, style, streams ].join
11
25
  end
26
+
27
+ private
28
+
29
+ def defaults
30
+ {
31
+ colour: {},
32
+ streams: [],
33
+ style: ''
34
+ }
35
+ end
12
36
  end
13
37
  end
@@ -1,12 +1,32 @@
1
1
  module Vedeu
2
2
  class Stream
3
- include Virtus.model
3
+ def initialize(attributes = {})
4
+ @attributes = attributes
5
+ end
6
+
7
+ def attributes
8
+ defaults.merge!(@attributes)
9
+ end
10
+
11
+ def colour
12
+ @colour ||= Colour.new(attributes[:colour])
13
+ end
14
+
15
+ def style
16
+ @style ||= Attributes.coerce_styles(attributes[:style])
17
+ end
4
18
 
5
- attribute :colour, Colour, default: Colour.new
6
- attribute :style, Style, default: ''
7
- attribute :text, String, default: ''
8
- attribute :width, Integer
9
- attribute :align, Symbol, default: :left
19
+ def text
20
+ @text ||= attributes[:text]
21
+ end
22
+
23
+ def width
24
+ @width ||= attributes[:width]
25
+ end
26
+
27
+ def align
28
+ @align ||= attributes[:align]
29
+ end
10
30
 
11
31
  def to_s
12
32
  [ colour, style, data ].join
@@ -29,5 +49,15 @@ module Vedeu
29
49
  def width?
30
50
  !!width
31
51
  end
52
+
53
+ def defaults
54
+ {
55
+ colour: {},
56
+ style: '',
57
+ text: '',
58
+ width: nil,
59
+ align: :left
60
+ }
61
+ end
32
62
  end
33
63
  end
@@ -7,13 +7,13 @@ module Vedeu
7
7
  def create(interface)
8
8
  buffers[interface.name][:clear] = Clear.call(interface)
9
9
 
10
- Vedeu.events.on("_refresh_#{interface.name}_".to_sym, interface.delay) do
10
+ Vedeu.events.event("_refresh_#{interface.name}_".to_sym, interface.delay) do
11
11
  refresh(interface.name)
12
12
  end
13
13
 
14
14
  # TODO: cannot refresh group since no logic to fetch group from buffer
15
15
  # unless interface.group.nil? || interface.group.empty?
16
- # Vedeu.events.on("_refresh_#{interface.group}_".to_sym, interface.delay) do
16
+ # Vedeu.events.event("_refresh_#{interface.group}_".to_sym, interface.delay) do
17
17
  # refresh_group(interface.group)
18
18
  # end
19
19
  # end
@@ -23,10 +23,14 @@ module Vedeu
23
23
  buffers[name][:next].unshift(sequence)
24
24
  end
25
25
 
26
- def refresh(name)
27
- data = buffers.fetch(name) do
26
+ def query(name)
27
+ buffers.fetch(name) do
28
28
  fail RefreshFailed, 'Cannot refresh non-existent interface.'
29
29
  end
30
+ end
31
+
32
+ def refresh(name)
33
+ data = query(name)
30
34
 
31
35
  sequence = if data[:next].any?
32
36
  data[:current] = data[:next].pop
@@ -10,9 +10,7 @@ module Vedeu
10
10
 
11
11
  def clear
12
12
  interface_lines.inject([colours]) do |line, index|
13
- line << interface.origin(index)
14
- line << ' ' * interface.width
15
- line << interface.origin(index)
13
+ line << interface.origin(index) { ' ' * interface.width }
16
14
  end.join
17
15
  end
18
16
 
@@ -39,9 +39,11 @@ module Vedeu
39
39
  if (line_length += stream.text.size) >= width
40
40
  remainder = width - line_length
41
41
 
42
- processed << Stream.new(text: truncate(stream.text, remainder),
43
- style: stream.style,
44
- colour: stream.colour)
42
+ processed << Stream.new({
43
+ colour: stream.colour.attributes,
44
+ style: stream.style,
45
+ text: truncate(stream.text, remainder),
46
+ })
45
47
 
46
48
  else
47
49
  processed << stream
@@ -49,9 +51,15 @@ module Vedeu
49
51
  end
50
52
  end
51
53
 
52
- Line.new(streams: processed, style: line.style, colour: line.colour)
54
+ Line.new({
55
+ colour: line.colour.attributes,
56
+ streams: processed,
57
+ style: line.style,
58
+ })
59
+
53
60
  else
54
61
  line
62
+
55
63
  end
56
64
  end
57
65
  end
@@ -2,11 +2,21 @@ module Vedeu
2
2
  module Esc
3
3
  extend self
4
4
 
5
- def set_position(y = 1, x = 1)
5
+ def set_position(y = 1, x = 1, &block)
6
6
  row = (y == 0 || y == nil) ? 1 : y
7
7
  column = (x == 0 || x == nil) ? 1 : x
8
8
 
9
- ["\e[", row, ';', column, 'H'].join
9
+ if block_given?
10
+ out = []
11
+ out << ["\e[", row, ';', column, 'H'].join
12
+ out << yield
13
+ out << ["\e[", row, ';', column, 'H'].join
14
+ out
15
+
16
+ else
17
+ ["\e[", row, ';', column, 'H'].join
18
+
19
+ end
10
20
  end
11
21
 
12
22
  def string(value = '')
@@ -9,16 +9,16 @@ module Vedeu
9
9
 
10
10
  def events
11
11
  @_events ||= Vedeu.events.add(self) do
12
- on(:menu_next) { next_item }
13
- on(:menu_prev) { prev_item }
14
- on(:menu_top) { top_item }
15
- on(:menu_bottom) { bottom_item }
16
- on(:menu_select) { select_item }
17
- on(:menu_deselect) { deselect_item }
18
-
19
- on(:menu_selected) { selected_item }
20
- on(:menu_current) { current_item }
21
- on(:menu_items) { items }
12
+ event(:menu_next) { next_item }
13
+ event(:menu_prev) { prev_item }
14
+ event(:menu_top) { top_item }
15
+ event(:menu_bottom) { bottom_item }
16
+ event(:menu_select) { select_item }
17
+ event(:menu_deselect) { deselect_item }
18
+
19
+ event(:menu_selected) { selected_item }
20
+ event(:menu_current) { current_item }
21
+ event(:menu_items) { items }
22
22
  end
23
23
  end
24
24
 
@@ -3,17 +3,17 @@ require 'test_helper'
3
3
  module Vedeu
4
4
  module API
5
5
  describe Events do
6
- describe '#on' do
6
+ describe '#event' do
7
7
  it 'adds the event block to the handlers' do
8
8
  skip
9
9
  events = Events.new
10
- events.on(:some_event) { proc { |x| x } }
10
+ events.event(:some_event) { proc { |x| x } }
11
11
  end
12
12
 
13
13
  it 'adds the specified throttle to the throttles' do
14
14
  skip
15
15
  events = Events.new
16
- events.on(:some_event, 250) { proc { |x| x } }
16
+ events.event(:some_event, 250) { proc { |x| x } }
17
17
  end
18
18
  end
19
19
 
@@ -21,7 +21,7 @@ module Vedeu
21
21
  it 'returns a collection containing the event when the event is ' \
22
22
  'pre-registered' do
23
23
  events = Events.new do
24
- on(:_exit_) { fail StopIteration }
24
+ event(:_exit_) { fail StopIteration }
25
25
  end
26
26
  proc { events.trigger(:_exit_) }.must_raise(StopIteration)
27
27
  end
@@ -8,7 +8,7 @@ module Vedeu
8
8
  interface = Interface.new('widget')
9
9
 
10
10
  it 'creates and stores a new interface' do
11
- interface.save.must_be_instance_of(Vedeu::Interface)
11
+ interface.create.must_be_instance_of(Vedeu::Interface)
12
12
  end
13
13
 
14
14
  it 'allows interfaces to share behaviour' do
@@ -38,35 +38,35 @@ module Vedeu
38
38
  end
39
39
 
40
40
  it 'raises an exception when the value is out of bounds' do
41
- proc { interface.save { x 0 } }.must_raise(XOutOfBounds)
41
+ proc { interface.create { x 0 } }.must_raise(XOutOfBounds)
42
42
  end
43
43
 
44
44
  it 'raises an exception when the value is out of bounds' do
45
- proc { interface.save { x 999 } }.must_raise(XOutOfBounds)
45
+ proc { interface.create { x 999 } }.must_raise(XOutOfBounds)
46
46
  end
47
47
 
48
48
  it 'raises an exception when the value is out of bounds' do
49
- proc { interface.save { y 0 } }.must_raise(YOutOfBounds)
49
+ proc { interface.create { y 0 } }.must_raise(YOutOfBounds)
50
50
  end
51
51
 
52
52
  it 'raises an exception when the value is out of bounds' do
53
- proc { interface.save { y 999 } }.must_raise(YOutOfBounds)
53
+ proc { interface.create { y 999 } }.must_raise(YOutOfBounds)
54
54
  end
55
55
 
56
56
  it 'raises an exception when the value is out of bounds' do
57
- proc { interface.save { width 0 } }.must_raise(InvalidWidth)
57
+ proc { interface.create { width 0 } }.must_raise(InvalidWidth)
58
58
  end
59
59
 
60
60
  it 'raises an exception when the value is out of bounds' do
61
- proc { interface.save { width 999 } }.must_raise(InvalidWidth)
61
+ proc { interface.create { width 999 } }.must_raise(InvalidWidth)
62
62
  end
63
63
 
64
64
  it 'raises an exception when the value is out of bounds' do
65
- proc { interface.save { height 0 } }.must_raise(InvalidHeight)
65
+ proc { interface.create { height 0 } }.must_raise(InvalidHeight)
66
66
  end
67
67
 
68
68
  it 'raises an exception when the value is out of bounds' do
69
- proc { interface.save { height 999 } }.must_raise(InvalidHeight)
69
+ proc { interface.create { height 999 } }.must_raise(InvalidHeight)
70
70
  end
71
71
  end
72
72
  end