Class: Vedeu::Border

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Common, Model, Presentation
Defined in:
lib/vedeu/output/border.rb

Overview

Note:

Refer to UTF-8 U+2500 to U+257F for border characters. More details can be found at: en.wikipedia.org/wiki/Box-drawing_character

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

Methods included from Presentation

#_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.

Parameters:

  • attributes (Hash) (defaults to: {})

Options Hash (attributes):

  • bottom_left (String)

    The bottom left border character.

  • bottom_right (String)

    The bottom right border character.

  • colour (Hash)
  • enabled (Boolean)

    Indicate whether the border is to be shown for this interface.

  • horizontal (String)

    The horizontal border character.

  • name (String)

    The name of the interface to which this border relates.

  • style (Object)
  • show_bottom (Boolean)

    Indicate whether the bottom border is to be shown.

  • show_left (Boolean)

    Indicate whether the left border is to be shown.

  • show_right (Boolean)

    Indicate whether the right border is to be shown.

  • show_top (Boolean)

    Indicate whether the top border is to be shown.

  • title (String)

    A title bar for when the top border is to be shown.

  • top_left (String)

    The top left border character.

  • top_right (String)

    The top right border character.

  • vertical (String)

    The vertical border character.



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

#attributesHash

Returns:

  • (Hash)


27
28
29
# File 'lib/vedeu/output/border.rb', line 27

def attributes
  @attributes
end

#bottom_leftString

Returns:

  • (String)


31
32
33
# File 'lib/vedeu/output/border.rb', line 31

def bottom_left
  @bottom_left
end

#bottom_rightString

Returns:

  • (String)


35
36
37
# File 'lib/vedeu/output/border.rb', line 35

def bottom_right
  @bottom_right
end

#enabledBoolean (readonly) Also known as: enabled?

Returns:

  • (Boolean)


83
84
85
# File 'lib/vedeu/output/border.rb', line 83

def enabled
  @enabled
end

#horizontalString

Returns:

  • (String)


39
40
41
# File 'lib/vedeu/output/border.rb', line 39

def horizontal
  @horizontal
end

#nameString (readonly)

Returns:

  • (String)


79
80
81
# File 'lib/vedeu/output/border.rb', line 79

def name
  @name
end

#show_bottomBoolean Also known as: bottom?

Returns:

  • (Boolean)


43
44
45
# File 'lib/vedeu/output/border.rb', line 43

def show_bottom
  @show_bottom
end

#show_leftBoolean Also known as: left?

Returns:

  • (Boolean)


48
49
50
# File 'lib/vedeu/output/border.rb', line 48

def show_left
  @show_left
end

#show_rightBoolean Also known as: right?

Returns:

  • (Boolean)


53
54
55
# File 'lib/vedeu/output/border.rb', line 53

def show_right
  @show_right
end

#show_topBoolean Also known as: top?

Returns:

  • (Boolean)


58
59
60
# File 'lib/vedeu/output/border.rb', line 58

def show_top
  @show_top
end

#titleString

Returns:

  • (String)


63
64
65
# File 'lib/vedeu/output/border.rb', line 63

def title
  @title
end

#top_leftString

Returns:

  • (String)


67
68
69
# File 'lib/vedeu/output/border.rb', line 67

def top_left
  @top_left
end

#top_rightString

Returns:

  • (String)


71
72
73
# File 'lib/vedeu/output/border.rb', line 71

def top_right
  @top_right
end

#verticalString

Returns:

  • (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 (private)

Parameters:

  • value (String)
  • type (Symbol|NilClass) (defaults to: :border)
  • iy (Fixnum) (defaults to: 0)
  • ix (Fixnum) (defaults to: 0)

Returns:



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

#bottomString

Renders the bottom border for the interface.

Returns:

  • (String)


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

#bxFixnum

Returns:

  • (Fixnum)


122
123
124
# File 'lib/vedeu/output/border.rb', line 122

def bx
  (enabled? && left?) ? x + 1 : x
end

#bxnFixnum

Returns:

  • (Fixnum)


127
128
129
# File 'lib/vedeu/output/border.rb', line 127

def bxn
  (enabled? && right?) ? xn - 1 : xn
end

#byFixnum

Returns:

  • (Fixnum)


132
133
134
# File 'lib/vedeu/output/border.rb', line 132

def by
  (enabled? && top?) ? y + 1 : y
end

#bynFixnum

Returns:

  • (Fixnum)


137
138
139
# File 'lib/vedeu/output/border.rb', line 137

def byn
  (enabled? && bottom?) ? yn - 1 : yn
end

#defaultsHash (private)

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.

Returns:

  • (Hash)


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", # └ # \u2514
    bottom_right: "\x6A", # ┘ # \u2518
    client:       nil,
    colour:       nil,
    enabled:      false,
    horizontal:   "\x71", # ─ # \u2500
    name:         '',
    repository:   Vedeu.borders,
    show_bottom:  true,
    show_left:    true,
    show_right:   true,
    show_top:     true,
    style:        nil,
    title:        '',
    top_left:     "\x6C", # ┌ # \u250C
    top_right:    "\x6B", # ┐ # \u2510
    vertical:     "\x78", # │ # \u2502
  }
end

#geometryVedeu::Geometry (private)

Returns:



318
319
320
# File 'lib/vedeu/output/border.rb', line 318

def geometry
  @geometry ||= Vedeu.geometries.by_name(name)
end

#heightFixnum

Returns the height of the interface determined by whether a top, bottom, both or neither borders are shown.

Returns:

  • (Fixnum)


153
154
155
# File 'lib/vedeu/output/border.rb', line 153

def height
  (by..byn).size
end

#horizontal_border(position) ⇒ Array<Vedeu::Char> (private)

Parameters:

  • position (Symbol)

    Either :top_horizontal, or :bottom_horizontal.

Returns:



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

#interfaceVedeu::Interface (private)

Returns:



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.

Parameters:

  • iy (Fixnum) (defaults to: 0)

Returns:

  • (String)


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_titleString (private)

Pads the title with a single whitespace either side.

Examples:

title = 'Truncated!'
width = 20
# => ' Truncated! '

width = 10
# => ' Trunca '

Returns:

  • (String)

See Also:



275
276
277
# File 'lib/vedeu/output/border.rb', line 275

def padded_title
  truncated_title.center(truncated_title.size + 2)
end

#parentVedeu::Interface

The parent of a border is always an interface.

Returns:



231
232
233
# File 'lib/vedeu/output/border.rb', line 231

def parent
  interface
end

#position(name, iy = 0, ix = 0) ⇒ Vedeu::Position (private)

Parameters:

  • name (Symbol)
  • iy (Fixnum) (defaults to: 0)
  • ix (Fixnum) (defaults to: 0)

Returns:



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

#renderArray<Array<Vedeu::Char>>

Returns:



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.

Parameters:

  • iy (Fixnum) (defaults to: 0)

Returns:

  • (String)


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 (private)

Return boolean indicating whether this border has a non-empty title.

Returns:

  • (Boolean)


299
300
301
# File 'lib/vedeu/output/border.rb', line 299

def title?
  defined_value?(title)
end

#title_charactersArray<String> (private)

Returns:

  • (Array<String>)


259
260
261
# File 'lib/vedeu/output/border.rb', line 259

def title_characters
  @title_characters ||= padded_title.chars
end

#titlebarArray<Vedeu::Char> (private)

From the second element of #title_characters remove the border from each #horizontal_border Vedeu::Char, and add the title character.

Returns:



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

#to_sString

Returns:

  • (String)


172
173
174
# File 'lib/vedeu/output/border.rb', line 172

def to_s
  Vedeu::Renderers::Text.render(render)
end

#topString

Renders the top border for the interface.

Returns:

  • (String)


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_titleString (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.

Examples:

title = 'Truncated!'
width = 20
# => 'Truncated!'

width = 10
# => 'Trunca'

Returns:

  • (String)


292
293
294
# File 'lib/vedeu/output/border.rb', line 292

def truncated_title
  title.chomp.slice(0..(width - 5))
end

#widthFixnum

Returns the width of the interface determined by whether a left, right, both or neither borders are shown.

Returns:

  • (Fixnum)


145
146
147
# File 'lib/vedeu/output/border.rb', line 145

def width
  (bx..bxn).size
end