vedeu 0.4.2 → 0.4.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (95) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE.txt +3 -0
  3. data/Rakefile +16 -7
  4. data/examples/borders_app.rb +58 -55
  5. data/lib/vedeu/api.rb +1 -1
  6. data/lib/vedeu/application.rb +25 -25
  7. data/lib/vedeu/bindings.rb +5 -7
  8. data/lib/vedeu/configuration/api.rb +0 -2
  9. data/lib/vedeu/configuration/cli.rb +0 -2
  10. data/lib/vedeu/configuration/configuration.rb +0 -2
  11. data/lib/vedeu/cursor/cursor.rb +0 -2
  12. data/lib/vedeu/debug.rb +2 -2
  13. data/lib/vedeu/distributed/server.rb +0 -2
  14. data/lib/vedeu/dsl/components/border.rb +15 -19
  15. data/lib/vedeu/dsl/components/geometry.rb +14 -18
  16. data/lib/vedeu/dsl/components/keymap.rb +28 -32
  17. data/lib/vedeu/dsl/composition.rb +27 -9
  18. data/lib/vedeu/events/all.rb +1 -0
  19. data/lib/vedeu/events/event.rb +6 -8
  20. data/lib/vedeu/events/events.rb +10 -0
  21. data/lib/vedeu/events/trigger.rb +6 -12
  22. data/lib/vedeu/geometry/grid.rb +0 -2
  23. data/lib/vedeu/geometry/position.rb +0 -2
  24. data/lib/vedeu/geometry/position_validator.rb +0 -14
  25. data/lib/vedeu/input/input.rb +0 -2
  26. data/lib/vedeu/models/collection.rb +0 -2
  27. data/lib/vedeu/models/focus.rb +0 -1
  28. data/lib/vedeu/models/geometry.rb +7 -3
  29. data/lib/vedeu/models/group.rb +0 -2
  30. data/lib/vedeu/models/menu.rb +23 -29
  31. data/lib/vedeu/models/model.rb +0 -2
  32. data/lib/vedeu/models/view/char.rb +14 -10
  33. data/lib/vedeu/models/view/composition.rb +7 -9
  34. data/lib/vedeu/models/view/interface.rb +0 -1
  35. data/lib/vedeu/models/view/line.rb +2 -3
  36. data/lib/vedeu/models/view/stream.rb +0 -3
  37. data/lib/vedeu/output/all.rb +1 -0
  38. data/lib/vedeu/output/background.rb +0 -2
  39. data/lib/vedeu/output/colour.rb +0 -2
  40. data/lib/vedeu/output/compositor.rb +0 -2
  41. data/lib/vedeu/output/foreground.rb +0 -2
  42. data/lib/vedeu/output/html_renderer.rb +6 -0
  43. data/lib/vedeu/output/index_position.rb +35 -0
  44. data/lib/vedeu/output/output.rb +3 -1
  45. data/lib/vedeu/output/position_index.rb +2 -2
  46. data/lib/vedeu/output/presentation.rb +0 -2
  47. data/lib/vedeu/output/style.rb +0 -1
  48. data/lib/vedeu/output/translator.rb +18 -20
  49. data/lib/vedeu/output/viewport.rb +76 -51
  50. data/lib/vedeu/output/virtual_terminal.rb +2 -1
  51. data/lib/vedeu/repositories/all.rb +4 -4
  52. data/lib/vedeu/repositories/repository.rb +0 -2
  53. data/lib/vedeu/support/coercions.rb +0 -2
  54. data/lib/vedeu/support/common.rb +0 -2
  55. data/lib/vedeu/support/esc.rb +0 -2
  56. data/lib/vedeu/support/log.rb +86 -3
  57. data/lib/vedeu/support/refresh.rb +0 -2
  58. data/lib/vedeu/support/sentence.rb +5 -9
  59. data/lib/vedeu/support/terminal.rb +0 -2
  60. data/lib/vedeu/support/trace.rb +0 -2
  61. data/lib/vedeu/support/visible.rb +1 -1
  62. data/test/lib/vedeu/configuration/api_test.rb +14 -0
  63. data/test/lib/vedeu/configuration/configuration_test.rb +10 -0
  64. data/test/lib/vedeu/distributed/server_test.rb +5 -0
  65. data/test/lib/vedeu/dsl/components/border_test.rb +7 -0
  66. data/test/lib/vedeu/dsl/components/geometry_test.rb +8 -3
  67. data/test/lib/vedeu/dsl/interface_test.rb +5 -0
  68. data/test/lib/vedeu/dsl/line_test.rb +4 -0
  69. data/test/lib/vedeu/dsl/shared/colour_test.rb +7 -7
  70. data/test/lib/vedeu/dsl/shared/style_test.rb +5 -2
  71. data/test/lib/vedeu/dsl/stream_test.rb +4 -1
  72. data/test/lib/vedeu/events/event_test.rb +6 -0
  73. data/test/lib/vedeu/geometry/bounding_area_test.rb +30 -30
  74. data/test/lib/vedeu/geometry/position_test.rb +5 -0
  75. data/test/lib/vedeu/models/view/char_test.rb +1 -14
  76. data/test/lib/vedeu/output/index_position_test.rb +64 -0
  77. data/test/lib/vedeu/output/style_test.rb +4 -0
  78. data/test/lib/vedeu/output/translator_test.rb +4 -0
  79. data/test/lib/vedeu/output/virtual_terminal_test.rb +2 -0
  80. data/test/lib/vedeu/output/wordwrap_test.rb +5 -0
  81. data/test/lib/vedeu/repositories/all_test.rb +3 -3
  82. data/test/lib/vedeu/repositories/repository_test.rb +8 -0
  83. data/test/lib/vedeu/support/console_test.rb +14 -0
  84. data/test/lib/vedeu/support/esc_test.rb +6 -0
  85. data/test/lib/vedeu/support/terminal_test.rb +13 -0
  86. data/vedeu.gemspec +4 -4
  87. metadata +12 -24
  88. data/test/integration/distributed_test.rb +0 -16
  89. data/test/integration/dsl/compositions_test.rb +0 -19
  90. data/test/integration/dsl/interfaces_test.rb +0 -193
  91. data/test/integration/dsl/keymaps_test.rb +0 -30
  92. data/test/integration/dsl/lines_test.rb +0 -104
  93. data/test/integration/dsl/menus_test.rb +0 -59
  94. data/test/integration/dsl/streams_test.rb +0 -103
  95. data/test/integration/dsl/views_test.rb +0 -45
@@ -78,40 +78,41 @@ module Vedeu
78
78
  def show
79
79
  return [] unless lines?
80
80
 
81
- line_pad.map { |line| column_pad(line) }.compact
81
+ padded_lines.map { |line| padded_columns(line) }.compact
82
82
  end
83
83
 
84
- # Pad the number of rows so that we always return an Array of the same
85
- # length for each viewport.
84
+ # Returns the lines, padded where necessary for the viewport.
86
85
  #
87
86
  # @return [Array<Array<String>>]
88
- def line_pad
89
- visible_lines = lines[rows] || []
87
+ def padded_lines
88
+ visible = lines[rows] || []
90
89
 
91
- if visible_lines.size < height
92
- visible_lines + [" "] * (height - visible_lines.size)
93
-
94
- else
95
- visible_lines
96
-
97
- end
90
+ pad(visible, :height)
98
91
  end
99
92
 
100
- # Pads the number of columns so that we always return an Array of the same
101
- # length for each line.
93
+ # Returns the columns, padded where necessary for the given line.
102
94
  #
103
95
  # @param line [Array<String>]
104
96
  # @return [Array<String>]
105
- def column_pad(line)
106
- visible_columns = line.chars[columns] || []
97
+ def padded_columns(line)
98
+ visible = line.chars[columns] || []
107
99
 
108
- if visible_columns.size < width
109
- visible_columns + [" "] * (width - visible_columns.size)
100
+ pad(visible, :width)
101
+ end
110
102
 
111
- else
112
- visible_columns
103
+ # Pads the number of rows or columns to always return an Array of the same
104
+ # length for each viewport or line respectively.
105
+ #
106
+ # @param visible [Array<Array<String>>|Array<String>]
107
+ # @param dimension [Symbol] The dimension to pad (:height or :width).
108
+ # @return [Array<Array<String>>|Array<String>]
109
+ def pad(visible, dimension)
110
+ dim = send(dimension)
111
+ size = visible.size
113
112
 
114
- end
113
+ return visible unless size < dim
114
+
115
+ visible + [" "] * (dim - size)
115
116
  end
116
117
 
117
118
  # Using the current cursor's y position, return a range of visible lines.
@@ -119,24 +120,8 @@ module Vedeu
119
120
  # Scrolls the content vertically when the stored cursor's y position for the
120
121
  # interface is outside of the visible area.
121
122
  #
122
- # @note
123
- # [value, 0].max # this allows us to set a top that is greater than
124
- # # the content height.
125
- #
126
- # [[value, (content_height - height)].min, 0].max
127
- # # this does not allow us to have a position
128
- # # greater than the content height.
129
- #
130
123
  # @return [Range]
131
124
  def rows
132
- top = if oy >= bordered_height
133
- [(oy - bordered_height), 0].max
134
-
135
- else
136
- 0
137
-
138
- end
139
-
140
125
  top..(top + (height - 1))
141
126
  end
142
127
 
@@ -145,27 +130,60 @@ module Vedeu
145
130
  # Scrolls the content horizontally when the stored cursor's x position for
146
131
  # the interface is outside of the visible area.
147
132
  #
148
- # @note
149
- # [value, 0].max # this allows us to set a left that is greater
150
- # # than the content width.
151
- #
152
- # [[value, (content_width - width)].min, 0].max
153
- # # this does not allow us to have a position
154
- # # greater than the content width.
155
- #
156
133
  # @return [Range]
157
134
  def columns
158
- left = if ox >= bordered_width
159
- [(ox - bordered_width), 0].max
135
+ left..(left + (width - 1))
136
+ end
160
137
 
161
- else
162
- 0
138
+ # Returns the offset for the content based on the offset.
139
+ #
140
+ # @return [Fixnum]
141
+ def left
142
+ @left ||= reposition_x? ? reposition_x : 0
143
+ end
163
144
 
164
- end
145
+ # Returns the offset for the content based on the offset.
146
+ #
147
+ # @return [Fixnum]
148
+ def top
149
+ @top ||= reposition_y? ? reposition_y : 0
150
+ end
165
151
 
166
- left..(left + (width - 1))
152
+ # Returns a boolean indicating whether the x offset is greater than or equal
153
+ # to the bordered width.
154
+ #
155
+ # @return [Boolean]
156
+ def reposition_x?
157
+ ox >= bordered_width
158
+ end
159
+
160
+ # Returns a boolean indicating whether the y offset is greater than or equal
161
+ # to the bordered height.
162
+ #
163
+ # @return [Boolean]
164
+ def reposition_y?
165
+ oy >= bordered_height
167
166
  end
168
167
 
168
+ # Returns the number of columns to change the viewport by on the x axis,
169
+ # determined by the position of the x offset.
170
+ #
171
+ # @return [Fixnum]
172
+ def reposition_x
173
+ ((ox - bordered_width) <= 0) ? 0 : (ox - bordered_width)
174
+ end
175
+
176
+ # Returns the number of rows to change the viewport by on the y axis,
177
+ # determined by the position of the y offset.
178
+ #
179
+ # @return [Fixnum]
180
+ def reposition_y
181
+ ((oy - bordered_height) <= 0) ? 0 : (oy - bordered_height)
182
+ end
183
+
184
+ # When the viewport has a border, we need to account for that in our
185
+ # redrawing.
186
+ #
169
187
  # @return [Fixnum]
170
188
  def bordered_width
171
189
  return border.width if border?
@@ -173,6 +191,9 @@ module Vedeu
173
191
  width
174
192
  end
175
193
 
194
+ # When the viewport has a border, we need to account for that in our
195
+ # redrawing.
196
+ #
176
197
  # @return [Fixnum]
177
198
  def bordered_height
178
199
  return border.height if border?
@@ -180,11 +201,15 @@ module Vedeu
180
201
  height
181
202
  end
182
203
 
204
+ # Return the border associated with the interface we are drawing.
205
+ #
183
206
  # @return [Vedeu::Border]
184
207
  def border
185
208
  @border ||= Vedeu.borders.find(interface.name)
186
209
  end
187
210
 
211
+ # Returns a boolean indicating the interface we are drawing has a border.
212
+ #
188
213
  # @return [Boolean]
189
214
  def border?
190
215
  Vedeu.borders.registered?(interface.name)
@@ -69,9 +69,10 @@ module Vedeu
69
69
  def write(y, x, data)
70
70
  cy, cx = Vedeu::PositionIndex[y, x]
71
71
 
72
- return false unless read(y, x).is_a?(Vedeu::Char)
72
+ return false unless read(cy, cx).is_a?(Vedeu::Char)
73
73
 
74
74
  cells[cy][cx] = data
75
+
75
76
  true
76
77
  end
77
78
 
@@ -43,14 +43,14 @@ module Vedeu
43
43
 
44
44
  # Allows the storing of events.
45
45
  #
46
- class Events < Repository
46
+ class EventsRepository < Repository
47
47
 
48
- # @return [Vedeu::Events]
48
+ # @return [Vedeu::EventsRepository]
49
49
  def self.events
50
- @events ||= Vedeu::Events.new(Vedeu::Model::Collection)
50
+ @events ||= Vedeu::EventsRepository.new(Vedeu::Events)
51
51
  end
52
52
 
53
- end # Events
53
+ end # EventsRepository
54
54
 
55
55
  # Allows the storing of interface/view geometry independent of the interface
56
56
  # instance.
@@ -11,8 +11,6 @@ module Vedeu
11
11
  #
12
12
  # { 'models' => [Model] }
13
13
  #
14
- # @api private
15
- #
16
14
  class Repository
17
15
 
18
16
  include Vedeu::Common
@@ -4,8 +4,6 @@ module Vedeu
4
4
 
5
5
  # Provides means to convert attributes into the correct model.
6
6
  #
7
- # @api private
8
- #
9
7
  module Coercions
10
8
 
11
9
  include Vedeu::Common
@@ -2,8 +2,6 @@ module Vedeu
2
2
 
3
3
  # A module for common methods used throughout Vedeu.
4
4
  #
5
- # @api private
6
- #
7
5
  module Common
8
6
 
9
7
  # Returns a boolean indicating whether a variable has a useful value.
@@ -3,8 +3,6 @@ module Vedeu
3
3
  # Provides escape sequence strings for setting the cursor position and various
4
4
  # display related functions.
5
5
  #
6
- # @api private
7
- #
8
6
  module Esc
9
7
 
10
8
  extend self
@@ -2,9 +2,92 @@ require 'vedeu/configuration/configuration'
2
2
 
3
3
  module Vedeu
4
4
 
5
- # Provides the ability to log anything to the Vedeu log file.
5
+ # Allows the creation of a lock-less log device.
6
6
  #
7
- # @api private
7
+ class MonoLogger < Logger
8
+
9
+ # Create a trappable Logger instance.
10
+ #
11
+ # @param logdev [String|IO] The filename (String) or IO object (typically
12
+ # STDOUT, STDERR or an open file).
13
+ # @param shift_age [] Number of old log files to keep, or frequency of
14
+ # rotation (daily, weekly, monthly).
15
+ # @param shift_size [] Maximum log file size (only applies when shift_age
16
+ # is a number).
17
+ #
18
+ # @example
19
+ # Logger.new(name, shift_age = 7, shift_size = 1048576)
20
+ # Logger.new(name, shift_age = 'weekly')
21
+ #
22
+ def initialize(logdev, shift_age=nil, shift_size=nil)
23
+ @level = DEBUG
24
+ @default_formatter = Formatter.new
25
+ @formatter = nil
26
+ @logdev = nil
27
+ if logdev
28
+ @logdev = LocklessLogDevice.new(logdev)
29
+ end
30
+ end
31
+
32
+ # Ensures we can always write to the log file by creating a lock-less
33
+ # log device.
34
+ class LocklessLogDevice < LogDevice
35
+
36
+ # @param log []
37
+ # @return []
38
+ def initialize(log = nil)
39
+ @dev = nil
40
+ @filename = nil
41
+ @shift_age = nil
42
+ @shift_size = nil
43
+
44
+ if log.respond_to?(:write) && log.respond_to?(:close)
45
+ @dev = log
46
+
47
+ else
48
+ @dev = open_logfile(log)
49
+ @dev.sync = true
50
+ @filename = log
51
+
52
+ end
53
+ end
54
+
55
+ # @param message []
56
+ # @return []
57
+ def write(message)
58
+ @dev.write(message)
59
+
60
+ rescue Exception => ignored
61
+ warn("log writing failed. #{ignored}")
62
+
63
+ end
64
+
65
+ # @return []
66
+ def close
67
+ @dev.close rescue nil
68
+ end
69
+
70
+ private
71
+
72
+ # @param filename []
73
+ # @return []
74
+ def open_logfile(filename)
75
+ if (FileTest.exist?(filename))
76
+ open(filename, (File::WRONLY | File::APPEND))
77
+
78
+ else
79
+ logdev = open(filename, (File::WRONLY | File::APPEND | File::CREAT))
80
+ logdev.sync = true
81
+ logdev
82
+
83
+ end
84
+ end
85
+
86
+ end # LocklessLogDevice
87
+
88
+ end # MonoLogger
89
+
90
+ # Provides the ability to log anything to the Vedeu log file.
8
91
  #
9
92
  class Log
10
93
 
@@ -29,7 +112,7 @@ module Vedeu
29
112
 
30
113
  # @return [TrueClass]
31
114
  def logger
32
- Logger.new(log_file).tap do |log|
115
+ MonoLogger.new(log_file).tap do |log|
33
116
  log.formatter = proc do |_, time, _, message|
34
117
  formatted_message(message, time)
35
118
  end
@@ -2,8 +2,6 @@ module Vedeu
2
2
 
3
3
  # Refreshes the terminal.
4
4
  #
5
- # @api private
6
- #
7
5
  module Refresh
8
6
 
9
7
  extend self
@@ -8,15 +8,11 @@ module Vedeu
8
8
  #
9
9
  class Sentence
10
10
 
11
- class << self
12
-
13
- # @param elements [Array]
14
- # @param label [String]
15
- # @return [String]
16
- def construct(elements, label = 'elements')
17
- new(elements, label).construct
18
- end
19
-
11
+ # @param elements [Array]
12
+ # @param label [String]
13
+ # @return [String]
14
+ def self.construct(elements, label = 'elements')
15
+ new(elements, label).construct
20
16
  end
21
17
 
22
18
  # @param elements [Array]
@@ -7,8 +7,6 @@ module Vedeu
7
7
  # This module is the direct interface between Vedeu and your terminal/
8
8
  # console, via Ruby's IO core library.
9
9
  #
10
- # @api private
11
- #
12
10
  module Terminal
13
11
 
14
12
  extend self
@@ -9,8 +9,6 @@ module Vedeu
9
9
  # this will make your application less responsive, and the tests
10
10
  # excruciatingly slow to run.
11
11
  #
12
- # @api private
13
- #
14
12
  class Trace
15
13
 
16
14
  # @todo
@@ -6,7 +6,7 @@ module Vedeu
6
6
  #
7
7
  class Visible
8
8
 
9
- # @param visible [Boolean|Symbol]
9
+ # @param value [Boolean|Symbol]
10
10
  # @return [Visible]
11
11
  def self.coerce(value)
12
12
  if value.is_a?(self)
@@ -6,9 +6,23 @@ module Vedeu
6
6
 
7
7
  describe API do
8
8
 
9
+ let(:described) { Vedeu::Config::API }
10
+ let(:instance) { described.new }
11
+
9
12
  before { Configuration.reset! }
10
13
  after { test_configuration }
11
14
 
15
+ describe 'alias_methods' do
16
+ it { instance.must_respond_to(:interactive) }
17
+ it { instance.must_respond_to(:standalone) }
18
+ it { instance.must_respond_to(:run_once) }
19
+ it { instance.must_respond_to(:drb) }
20
+ it { instance.must_respond_to(:cooked) }
21
+ it { instance.must_respond_to(:raw) }
22
+ it { instance.must_respond_to(:debug) }
23
+ it { instance.must_respond_to(:trace) }
24
+ end
25
+
12
26
  describe '.configure' do
13
27
  it 'returns the configuration singleton' do
14
28
  Vedeu.configure do