vedeu 0.0.31 → 0.0.32

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (39) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +6 -4
  3. data/lib/vedeu.rb +1 -0
  4. data/lib/vedeu/models/collection.rb +17 -0
  5. data/lib/vedeu/models/colour.rb +27 -5
  6. data/lib/vedeu/models/line_collection.rb +4 -9
  7. data/lib/vedeu/models/stream.rb +1 -1
  8. data/lib/vedeu/models/stream_collection.rb +4 -9
  9. data/lib/vedeu/support/coordinate.rb +149 -0
  10. data/lib/vedeu/support/esc.rb +4 -0
  11. data/lib/vedeu/support/menu.rb +103 -0
  12. data/lib/vedeu/support/terminal.rb +4 -0
  13. data/test/example_app/lib/app.rb +25 -6
  14. data/test/lib/vedeu/models/collection_test.rb +50 -0
  15. data/test/lib/vedeu/models/colour_test.rb +58 -1
  16. data/test/lib/vedeu/models/command_test.rb +2 -1
  17. data/test/lib/vedeu/models/interface_collection_test.rb +4 -3
  18. data/test/lib/vedeu/models/line_test.rb +3 -1
  19. data/test/lib/vedeu/models/stream_test.rb +6 -3
  20. data/test/lib/vedeu/output/text_adaptor_test.rb +1 -1
  21. data/test/lib/vedeu/repository/command_repository_test.rb +2 -1
  22. data/test/lib/vedeu/repository/interface_repository_test.rb +2 -1
  23. data/test/lib/vedeu/repository/repository_test.rb +2 -1
  24. data/test/lib/vedeu/repository/storage_test.rb +6 -3
  25. data/test/lib/vedeu/support/coordinate_test.rb +363 -0
  26. data/test/lib/vedeu/support/esc_test.rb +8 -0
  27. data/test/lib/vedeu/support/menu_test.rb +202 -0
  28. data/test/lib/vedeu/support/parser_test.rb +2 -1
  29. data/test/lib/vedeu/support/parsing/hash_parser_test.rb +6 -3
  30. data/test/lib/vedeu/support/parsing/json_parser_test.rb +2 -1
  31. data/test/lib/vedeu/support/terminal_test.rb +9 -0
  32. data/test/lib/vedeu/support/translator_test.rb +2 -1
  33. data/test/test_helper.rb +12 -0
  34. data/vedeu.gemspec +1 -1
  35. metadata +11 -8
  36. data/lib/vedeu/models/background.rb +0 -13
  37. data/lib/vedeu/models/foreground.rb +0 -13
  38. data/test/lib/vedeu/models/background_test.rb +0 -19
  39. data/test/lib/vedeu/models/foreground_test.rb +0 -18
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 29a2d43ab7e7d773c5ad3fcd3d223462808fe191
4
- data.tar.gz: 428e31c1b5fb377a92741f3cf7ccaeafd081fe24
3
+ metadata.gz: a0f9669e1076e596ff5730f44aa1e4563fc24fb6
4
+ data.tar.gz: 7316c60bdbf2067b1ee27726bd94524f8554af47
5
5
  SHA512:
6
- metadata.gz: b19757abb7a24b5e3c27db48950075536bd3b85b1d6b448f0041fa206c91af01405e76618f3d5b7644da86d08b0c7cd7a30de2d3438354d755c4d436f11342de
7
- data.tar.gz: 0feb617271ee83bf0f3caa26c7adfb294ac6651ddbd3aa014bf14ac2f660f746ae172e568e4cf0407320f63e9ac97c5a6b1eb890bc8cb3ea5d6dfc00ac08fc59
6
+ metadata.gz: 7233c00d83179e6d1374fca99bea3d477f83e5991e09a8a85211d83c590d47de83ab67adca91c39628f0050240e01921cc932c2cda64826f619fea618b8e0abc
7
+ data.tar.gz: 758b39394201c72a704d0fce2188272474f4c62ee73ec5589c3938f87fea664c8576b0fb69b67dc0c808a6298e7a7bb3676838694447326d31202182fbf7d33c
data/README.md CHANGED
@@ -54,8 +54,8 @@ To understand how Vedeu works, you need to familiarise yourself with some terms.
54
54
  y: 1,
55
55
  x: 1,
56
56
  z: 1,
57
- width: :auto, # will set to terminal width
58
- height: 10, # also accepts :auto
57
+ width: 10, # see notes below
58
+ height: 10,
59
59
  colour: {
60
60
  foreground: '#ffffff',
61
61
  background: '#000000'
@@ -69,8 +69,10 @@ Referring to the above example, interfaces have a name, and various default attr
69
69
  `:x` sets the starting column point.
70
70
  `:z` an integer specifying the z-index of the interface.
71
71
  (See Layers)
72
- `:width` sets how many characters wide the interface will be.
73
- `:height` sets how many characters tall the interface will be.
72
+
73
+ `:width` sets character width of the interface
74
+ `:height` sets character height of the interface
75
+ Note: not setting a width or height will set the values to the terminal's reported width and height.
74
76
 
75
77
  `:foreground` sets the default foreground colour. (See Colours)
76
78
  `:background` sets the default background colour.
data/lib/vedeu.rb CHANGED
@@ -4,6 +4,7 @@ require 'logger'
4
4
  require_relative 'vedeu/repository/command_repository'
5
5
  require_relative 'vedeu/repository/interface_repository'
6
6
  require_relative 'vedeu/repository/event_repository'
7
+ require_relative 'vedeu/support/coordinate'
7
8
  require_relative 'vedeu/support/exit'
8
9
  require_relative 'vedeu/launcher'
9
10
 
@@ -0,0 +1,17 @@
1
+ module Vedeu
2
+ module Collection
3
+ extend self
4
+
5
+ def coercer(value, model, key)
6
+ return [] if value.nil? || value.empty?
7
+
8
+ if value.is_a?(::String)
9
+ [model.new({ key => value })]
10
+
11
+ else
12
+ [value].flatten.map { |v| model.new(v) }
13
+
14
+ end
15
+ end
16
+ end
17
+ end
@@ -1,22 +1,44 @@
1
1
  require 'oj'
2
2
  require 'virtus'
3
3
 
4
- require_relative 'foreground'
5
- require_relative 'background'
4
+ require_relative '../support/esc'
6
5
 
7
6
  module Vedeu
8
7
  class Colour
9
8
  include Virtus.model
10
9
 
11
- attribute :foreground, Foreground
12
- attribute :background, Background
10
+ attribute :foreground, String
11
+ attribute :background, String
12
+
13
+ def foreground
14
+ @fg ||= Esc.foreground_colour(css_foreground)
15
+ end
16
+
17
+ def background
18
+ @bg ||= Esc.background_colour(css_background)
19
+ end
20
+
21
+ def css_foreground
22
+ @foreground || ''
23
+ end
24
+
25
+ def css_background
26
+ @background || ''
27
+ end
13
28
 
14
29
  def to_json
15
- Oj.dump(attributes, mode: :compat)
30
+ Oj.dump(as_hash, mode: :compat)
16
31
  end
17
32
 
18
33
  def to_s
19
34
  foreground + background
20
35
  end
36
+
37
+ def as_hash
38
+ {
39
+ foreground: css_foreground,
40
+ background: css_background,
41
+ }
42
+ end
21
43
  end
22
44
  end
@@ -1,19 +1,14 @@
1
1
  require 'virtus'
2
2
 
3
+ require_relative 'collection'
3
4
  require_relative 'line'
4
5
 
5
6
  module Vedeu
6
7
  class LineCollection < Virtus::Attribute
7
- def coerce(values)
8
- return [] if values.nil? || values.empty?
9
-
10
- if values.is_a?(::String)
11
- [Line.new({ streams: values })]
8
+ include Collection
12
9
 
13
- else
14
- [values].flatten.map { |value| Line.new(value) }
15
-
16
- end
10
+ def coerce(values)
11
+ coercer(values, Line, :streams)
17
12
  end
18
13
  end
19
14
  end
@@ -22,7 +22,7 @@ module Vedeu
22
22
 
23
23
  def json_attributes
24
24
  {
25
- colour: colour,
25
+ colour: colour.as_hash,
26
26
  style: style,
27
27
  text: text
28
28
  }
@@ -1,19 +1,14 @@
1
1
  require 'virtus'
2
2
 
3
+ require_relative 'collection'
3
4
  require_relative 'stream'
4
5
 
5
6
  module Vedeu
6
7
  class StreamCollection < Virtus::Attribute
7
- def coerce(values)
8
- return [] if values.nil? || values.empty?
9
-
10
- if values.is_a?(::String)
11
- [Stream.new({ text: values })]
8
+ include Collection
12
9
 
13
- else
14
- [values].flatten.map { |value| Stream.new(value) }
15
-
16
- end
10
+ def coerce(values)
11
+ coercer(values, Stream, :text)
17
12
  end
18
13
  end
19
14
  end
@@ -0,0 +1,149 @@
1
+ require_relative 'terminal'
2
+
3
+ module Vedeu
4
+ class OutOfBoundsError < StandardError; end
5
+
6
+ class Coordinate
7
+ def initialize(attrs = {})
8
+ @attrs = attrs
9
+ @height = attrs.fetch(:height)
10
+ @width = attrs.fetch(:width)
11
+ @terminal_height = attrs.fetch(:terminal_height, Terminal.height)
12
+ @terminal_width = attrs.fetch(:terminal_width, Terminal.width)
13
+ @y = attrs.fetch(:y, 1)
14
+ @x = attrs.fetch(:x, 1)
15
+ @centred = attrs.fetch(:centred, true)
16
+ end
17
+
18
+ def terminal_height
19
+ fail OutOfBoundsError,
20
+ 'Value must be greater than or equal to 1.' if @terminal_height < 1
21
+
22
+ if @terminal_height > Terminal.height
23
+ fail OutOfBoundsError, 'Cannot set terminal_height to be ' \
24
+ 'greater than the actual terminal ' \
25
+ 'height.'
26
+ else
27
+ @terminal_height
28
+ end
29
+ end
30
+
31
+ def height
32
+ fail OutOfBoundsError,
33
+ 'Value must be greater than or equal to 1.' if @height < 1
34
+
35
+ if @height > terminal_height
36
+ fail OutOfBoundsError, 'Cannot set height to be greater ' \
37
+ 'than the specified or actual ' \
38
+ 'terminal height.'
39
+ else
40
+ @height
41
+ end
42
+ end
43
+
44
+ def y
45
+ fail OutOfBoundsError,
46
+ 'Value must be greater than or equal to 1.' if @y < 1
47
+
48
+ if @y > height
49
+ fail OutOfBoundsError, 'Cannot set y position to be greater' \
50
+ ' that the specified height or' \
51
+ ' actual terminal height.'
52
+ else
53
+ @y
54
+ end
55
+ end
56
+
57
+ def terminal_width
58
+ fail OutOfBoundsError,
59
+ 'Value must be greater than or equal to 1.' if @terminal_width < 1
60
+
61
+ if @terminal_width > Terminal.width
62
+ fail OutOfBoundsError, 'Cannot set terminal_width to be ' \
63
+ 'greater than the actual terminal ' \
64
+ 'width.'
65
+ else
66
+ @terminal_width
67
+ end
68
+ end
69
+
70
+ def width
71
+ fail OutOfBoundsError,
72
+ 'Value must be greater than or equal to 1.' if @width < 1
73
+
74
+ if @width > terminal_width
75
+ fail OutOfBoundsError, 'Cannot set width to be greater ' \
76
+ 'than the specified or actual ' \
77
+ 'terminal width.'
78
+ else
79
+ @width
80
+ end
81
+ end
82
+
83
+ def x
84
+ fail OutOfBoundsError,
85
+ 'Value must be greater than or equal to 1.' if @x < 1
86
+
87
+ if @x > width
88
+ fail OutOfBoundsError, 'Cannot set x position to be greater' \
89
+ ' that the specified width or' \
90
+ ' actual terminal width.'
91
+ else
92
+ @x
93
+ end
94
+ end
95
+
96
+ def centred
97
+ !!(@centred)
98
+ end
99
+
100
+ def centre
101
+ [(terminal_height / 2), (terminal_width / 2)]
102
+ end
103
+
104
+ def centre_y
105
+ centre.first
106
+ end
107
+
108
+ def centre_x
109
+ centre.last
110
+ end
111
+
112
+ def top
113
+ if centred
114
+ centre_y - (height / 2)
115
+ else
116
+ y
117
+ end
118
+ end
119
+
120
+ def left
121
+ if centred
122
+ centre_x - (width / 2)
123
+ else
124
+ x
125
+ end
126
+ end
127
+
128
+ def bottom
129
+ top + height
130
+ end
131
+
132
+ def right
133
+ left + width
134
+ end
135
+
136
+ def position
137
+ {
138
+ y: top,
139
+ x: left,
140
+ height: height,
141
+ width: width,
142
+ centred: centred,
143
+ bottom: bottom,
144
+ right: right,
145
+ }
146
+ end
147
+
148
+ end
149
+ end
@@ -6,6 +6,8 @@ module Vedeu
6
6
  extend self
7
7
 
8
8
  def background_colour(value = '#000000')
9
+ return '' if value.empty?
10
+
9
11
  ["\e[48;5;", colour_translator(value), 'm'].join
10
12
  end
11
13
 
@@ -18,6 +20,8 @@ module Vedeu
18
20
  end
19
21
 
20
22
  def foreground_colour(value = '#ffffff')
23
+ return '' if value.empty?
24
+
21
25
  ["\e[38;5;", colour_translator(value), 'm'].join
22
26
  end
23
27
 
@@ -0,0 +1,103 @@
1
+ module Vedeu
2
+ class Menu
3
+ def initialize(collection)
4
+ @collection = collection
5
+ @current = 0
6
+ @selected = nil
7
+ end
8
+
9
+ def current
10
+ @current
11
+ end
12
+
13
+ def selected
14
+ @selected
15
+ end
16
+
17
+ def current_item
18
+ [@current, @collection[@current]]
19
+ end
20
+
21
+ def selected_item
22
+ return nil unless @selected
23
+
24
+ [@selected, @collection[@selected]]
25
+ end
26
+
27
+ def items
28
+ items = []
29
+ @collection.each_with_index do |item, index|
30
+ if index == @current && index == @selected
31
+ items << [true, true, item]
32
+
33
+ elsif index == @current
34
+ items << [false, true, item]
35
+
36
+ elsif index == @selected
37
+ items << [true, false, item]
38
+
39
+ else
40
+ items << [false, false, item]
41
+
42
+ end
43
+ end
44
+ items
45
+ end
46
+
47
+ def render
48
+ lines = []
49
+ items.each do |(sel, cur, item)|
50
+ if sel && cur
51
+ lines << "*> #{item}"
52
+
53
+ elsif cur
54
+ lines << " > #{item}"
55
+
56
+ elsif sel
57
+ lines << "* #{item}"
58
+
59
+ else
60
+ lines << " #{item}"
61
+
62
+ end
63
+ end
64
+ lines
65
+ end
66
+
67
+ def top
68
+ @current = 0
69
+ end
70
+
71
+ def bottom
72
+ @current = last
73
+ end
74
+
75
+ def next
76
+ @current += 1 if @current < last
77
+
78
+ self
79
+ end
80
+
81
+ def prev
82
+ @current -= 1 if @current > 0
83
+
84
+ self
85
+ end
86
+
87
+ def select
88
+ @selected = @current
89
+ end
90
+
91
+ def deselect
92
+ @selected = nil
93
+ end
94
+
95
+ def last
96
+ @collection.size - 1
97
+ end
98
+
99
+ def size
100
+ @collection.size
101
+ end
102
+ end
103
+ end