Class: Vedeu::Border
Overview
Provides the mechanism to decorate an interface with a border on all edges, or specific edges. The characters which are used for the border parts (e.g. the corners, verticals and horizontals) can be customised as can the colours and styles.
Instance Attribute Summary collapse
Attributes included from Model
#repository
Instance Method Summary
collapse
-
#border(value, type = :border, iy = 0, ix = 0) ⇒ Vedeu::Char
private
-
#bottom ⇒ String
Renders the bottom border for the interface.
-
#bx ⇒ Fixnum
-
#bxn ⇒ Fixnum
-
#by ⇒ Fixnum
-
#byn ⇒ Fixnum
-
#defaults ⇒ Hash
private
The default values for a new instance of this class.
-
#geometry ⇒ Vedeu::Geometry
private
-
#height ⇒ Fixnum
Returns the height of the interface determined by whether a top, bottom, both or neither borders are shown.
-
#horizontal_border(position) ⇒ Array<Vedeu::Char>
private
-
#initialize(attributes = {}) ⇒ Border
constructor
Returns a new instance of Vedeu::Border.
-
#interface ⇒ Vedeu::Interface
private
-
#left(iy = 0) ⇒ String
Renders the left border for the interface.
-
#padded_title ⇒ String
private
Pads the title with a single whitespace either side.
-
#parent ⇒ Vedeu::Interface
The parent of a border is always an interface.
-
#position(name, iy = 0, ix = 0) ⇒ Vedeu::Position
private
-
#render ⇒ Array<Array<Vedeu::Char>>
-
#right(iy = 0) ⇒ String
Renders the right border for the interface.
-
#title? ⇒ Boolean
private
Return boolean indicating whether this border has a non-empty title.
-
#title_characters ⇒ Array<String>
private
-
#titlebar ⇒ Array<Vedeu::Char>
private
-
#to_s ⇒ String
-
#top ⇒ String
Renders the top border for the interface.
-
#truncated_title ⇒ String
private
Truncates the title to the width of the interface, minus characters needed to ensure there is at least a single character of horizontal border and a whitespace on either side of the title.
-
#width ⇒ Fixnum
Returns the width of the interface determined by whether a left, right, both or neither borders are shown.
#_colour, #_style, #background, #background=, #colour, #colour=, #foreground, #foreground=, #parent_background, #parent_colour, #parent_foreground, #parent_style, #render_colour, #render_position, #render_style, #style, #style=
Methods included from Model
#demodulize, #deputy, #dsl_class, included, #store
Methods included from Common
#defined_value?
Constructor Details
#initialize(attributes = {}) ⇒ Border
Returns a new instance of Vedeu::Border.
113
114
115
116
117
118
119
|
# File 'lib/vedeu/output/border.rb', line 113
def initialize(attributes = {})
@attributes = defaults.merge!(attributes)
@attributes.each do |key, value|
instance_variable_set("@#{key}", value)
end
end
|
Instance Attribute Details
#attributes ⇒ Hash
27
28
29
|
# File 'lib/vedeu/output/border.rb', line 27
def attributes
@attributes
end
|
#bottom_left ⇒ String
31
32
33
|
# File 'lib/vedeu/output/border.rb', line 31
def bottom_left
@bottom_left
end
|
#bottom_right ⇒ String
35
36
37
|
# File 'lib/vedeu/output/border.rb', line 35
def bottom_right
@bottom_right
end
|
#enabled ⇒ Boolean
Also known as:
enabled?
83
84
85
|
# File 'lib/vedeu/output/border.rb', line 83
def enabled
@enabled
end
|
#horizontal ⇒ String
39
40
41
|
# File 'lib/vedeu/output/border.rb', line 39
def horizontal
@horizontal
end
|
#name ⇒ String
79
80
81
|
# File 'lib/vedeu/output/border.rb', line 79
def name
@name
end
|
#show_bottom ⇒ Boolean
Also known as:
bottom?
43
44
45
|
# File 'lib/vedeu/output/border.rb', line 43
def show_bottom
@show_bottom
end
|
#show_left ⇒ Boolean
Also known as:
left?
48
49
50
|
# File 'lib/vedeu/output/border.rb', line 48
def show_left
@show_left
end
|
#show_right ⇒ Boolean
Also known as:
right?
53
54
55
|
# File 'lib/vedeu/output/border.rb', line 53
def show_right
@show_right
end
|
#show_top ⇒ Boolean
Also known as:
top?
58
59
60
|
# File 'lib/vedeu/output/border.rb', line 58
def show_top
@show_top
end
|
#title ⇒ String
63
64
65
|
# File 'lib/vedeu/output/border.rb', line 63
def title
@title
end
|
#top_left ⇒ String
67
68
69
|
# File 'lib/vedeu/output/border.rb', line 67
def top_left
@top_left
end
|
#top_right ⇒ String
71
72
73
|
# File 'lib/vedeu/output/border.rb', line 71
def top_right
@top_right
end
|
#vertical ⇒ String
75
76
77
|
# File 'lib/vedeu/output/border.rb', line 75
def vertical
@vertical
end
|
Instance Method Details
#border(value, type = :border, iy = 0, ix = 0) ⇒ Vedeu::Char
308
309
310
311
312
313
314
315
|
# File 'lib/vedeu/output/border.rb', line 308
def border(value, type = :border, iy = 0, ix = 0)
Vedeu::Char.new(value: value,
parent: interface,
colour: colour,
style: style,
position: position(type, iy, ix),
border: type)
end
|
#bottom ⇒ String
Renders the bottom border for the interface.
179
180
181
182
183
184
185
186
187
|
# File 'lib/vedeu/output/border.rb', line 179
def bottom
return [] unless bottom?
out = []
out << border(bottom_left, :bottom_left) if left?
out << horizontal_border(:bottom_horizontal)
out << border(bottom_right, :bottom_right) if right?
out
end
|
#bx ⇒ Fixnum
122
123
124
|
# File 'lib/vedeu/output/border.rb', line 122
def bx
(enabled? && left?) ? x + 1 : x
end
|
#bxn ⇒ Fixnum
127
128
129
|
# File 'lib/vedeu/output/border.rb', line 127
def bxn
(enabled? && right?) ? xn - 1 : xn
end
|
#by ⇒ Fixnum
132
133
134
|
# File 'lib/vedeu/output/border.rb', line 132
def by
(enabled? && top?) ? y + 1 : y
end
|
#byn ⇒ Fixnum
137
138
139
|
# File 'lib/vedeu/output/border.rb', line 137
def byn
(enabled? && bottom?) ? yn - 1 : yn
end
|
#defaults ⇒ Hash
Note:
Using the ‘uXXXX’ variant produces gaps in the border, whilst the ‘xXX’ renders ‘nicely’.
The default values for a new instance of this class.
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
|
# File 'lib/vedeu/output/border.rb', line 334
def defaults
{
bottom_left: "\x6D", bottom_right: "\x6A", client: nil,
colour: nil,
enabled: false,
horizontal: "\x71", name: '',
repository: Vedeu.borders,
show_bottom: true,
show_left: true,
show_right: true,
show_top: true,
style: nil,
title: '',
top_left: "\x6C", top_right: "\x6B", vertical: "\x78", }
end
|
318
319
320
|
# File 'lib/vedeu/output/border.rb', line 318
def geometry
@geometry ||= Vedeu.geometries.by_name(name)
end
|
#height ⇒ Fixnum
Returns the height of the interface determined by whether a top, bottom, both or neither borders are shown.
153
154
155
|
# File 'lib/vedeu/output/border.rb', line 153
def height
(by..byn).size
end
|
#horizontal_border(position) ⇒ Array<Vedeu::Char>
239
240
241
242
243
|
# File 'lib/vedeu/output/border.rb', line 239
def horizontal_border(position)
width.times.each_with_object([]) do |ix, a|
a << border(horizontal, position, nil, ix)
end
end
|
323
324
325
|
# File 'lib/vedeu/output/border.rb', line 323
def interface
@interface ||= Vedeu.interfaces.find(name)
end
|
#left(iy = 0) ⇒ String
Renders the left border for the interface.
193
194
195
196
197
|
# File 'lib/vedeu/output/border.rb', line 193
def left(iy = 0)
return [] unless left?
border(vertical, :left_vertical, iy)
end
|
#padded_title ⇒ String
Pads the title with a single whitespace either side.
275
276
277
|
# File 'lib/vedeu/output/border.rb', line 275
def padded_title
truncated_title.center(truncated_title.size + 2)
end
|
The parent of a border is always an interface.
231
232
233
|
# File 'lib/vedeu/output/border.rb', line 231
def parent
interface
end
|
#position(name, iy = 0, ix = 0) ⇒ Vedeu::Position
360
361
362
363
364
365
366
367
368
369
370
371
|
# File 'lib/vedeu/output/border.rb', line 360
def position(name, iy = 0, ix = 0)
case name
when :top_horizontal then [y, (bx + ix)]
when :bottom_horizontal then [yn, (bx + ix)]
when :left_vertical then [(by + iy), x]
when :right_vertical then [(by + iy), xn]
when :bottom_left then [yn, x]
when :bottom_right then [yn, xn]
when :top_left then [y, x]
when :top_right then [y, xn]
end
end
|
#render ⇒ Array<Array<Vedeu::Char>>
158
159
160
161
162
163
164
165
166
167
168
169
|
# File 'lib/vedeu/output/border.rb', line 158
def render
return [] unless visible?
return [] unless enabled?
out = [top, bottom]
height.times do |y|
out << [left(y), right(y)]
end
out.flatten
end
|
#right(iy = 0) ⇒ String
Renders the right border for the interface.
203
204
205
206
207
|
# File 'lib/vedeu/output/border.rb', line 203
def right(iy = 0)
return [] unless right?
border(vertical, :right_vertical, iy)
end
|
#title? ⇒ Boolean
Return boolean indicating whether this border has a non-empty title.
299
300
301
|
# File 'lib/vedeu/output/border.rb', line 299
def title?
defined_value?(title)
end
|
#title_characters ⇒ Array<String>
259
260
261
|
# File 'lib/vedeu/output/border.rb', line 259
def title_characters
@title_characters ||= padded_title.chars
end
|
249
250
251
252
253
254
255
256
|
# File 'lib/vedeu/output/border.rb', line 249
def titlebar
horizontal_border(:top_horizontal).each_with_index do |char, index|
if index >= 1 && index <= title_characters.size
char.border = nil
char.value = title_characters[(index - 1)]
end
end
end
|
#top ⇒ String
Renders the top border for the interface.
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
|
# File 'lib/vedeu/output/border.rb', line 212
def top
return [] unless top?
out = []
out << border(top_left, :top_left) if left?
if title?
out << titlebar
else
out << horizontal_border(:top_horizontal)
end
out << border(top_right, :top_right) if right?
out
end
|
#truncated_title ⇒ String
Truncates the title to the width of the interface, minus characters needed to ensure there is at least a single character of horizontal border and a whitespace on either side of the title.
292
293
294
|
# File 'lib/vedeu/output/border.rb', line 292
def truncated_title
title.chomp.slice(0..(width - 5))
end
|
#width ⇒ Fixnum
Returns the width of the interface determined by whether a left, right, both or neither borders are shown.
145
146
147
|
# File 'lib/vedeu/output/border.rb', line 145
def width
(bx..bxn).size
end
|