Class: Vedeu::Borders::Render

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Common
Defined in:
lib/vedeu/borders/render.rb

Overview

Renders the provided border.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Common

#demodulize, #present?, #snake_case

Constructor Details

#initialize(border) ⇒ Vedeu::Borders::Render

Returns a new instance of Vedeu::Borders::Render.

Parameters:



52
53
54
# File 'lib/vedeu/borders/render.rb', line 52

def initialize(border)
  @border = border
end

Instance Attribute Details

#borderVedeu::Borders::Border (readonly, protected)



73
74
75
# File 'lib/vedeu/borders/render.rb', line 73

def border
  @border
end

Class Method Details

.with(border) ⇒ Array<Array<Vedeu::Views::Char>>

Returns:

See Also:



44
45
46
# File 'lib/vedeu/borders/render.rb', line 44

def self.with(border)
  new(border).render
end

Instance Method Details

#bottomString (private)

Note:

If a caption has been specified, then the bottom border will include this caption unless the size of the interface is smaller than the padded caption length.

Renders the bottom border for the interface.

Returns:

  • (String)


141
142
143
144
145
# File 'lib/vedeu/borders/render.rb', line 141

def bottom
  return [] unless bottom?

  [build_bottom_left, captionbar, build_bottom_right].compact
end

#build(value, type = :border, iy = 0, ix = 0) ⇒ Vedeu::Views::Char (private)

Parameters:

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

Returns:



82
83
84
85
86
87
88
89
# File 'lib/vedeu/borders/render.rb', line 82

def build(value, type = :border, iy = 0, ix = 0)
  Vedeu::Views::Char.new(value:    value,
                         parent:   interface,
                         colour:   colour,
                         style:    style,
                         position: [iy, ix],
                         border:   type)
end

#build_bottomArray<Vedeu::Views::Char> (private)

Creates a bottom border character.

Returns:



115
116
117
# File 'lib/vedeu/borders/render.rb', line 115

def build_bottom
  build_horizontal(:bottom_horizontal, yn)
end

#build_bottom_leftVedeu::Views::Char (private)

Creates the bottom left border character.

Returns:



94
95
96
# File 'lib/vedeu/borders/render.rb', line 94

def build_bottom_left
  build(bottom_left, :bottom_left, yn, x) if left?
end

#build_bottom_rightVedeu::Views::Char (private)

Creates the bottom right border character.

Returns:



101
102
103
# File 'lib/vedeu/borders/render.rb', line 101

def build_bottom_right
  build(bottom_right, :bottom_right, yn, xn) if right?
end

#build_horizontal(position, y_coordinate) ⇒ Array<Vedeu::Views::Char> (private)

Parameters:

  • position (Symbol)

    Either :top_horizontal, or :bottom_horizontal.

  • y_coordinate (Fixnum)

    The value of either y or yn.

Returns:



156
157
158
159
160
# File 'lib/vedeu/borders/render.rb', line 156

def build_horizontal(position, y_coordinate)
  Array.new(width) do |ix|
    build(horizontal, position, y_coordinate, (bx + ix))
  end
end

#build_topArray<Vedeu::Views::Char> (private)

Creates a top border character.

Returns:



108
109
110
# File 'lib/vedeu/borders/render.rb', line 108

def build_top
  build_horizontal(:bottom_horizontal, y)
end

#build_top_leftVedeu::Views::Char (private)

Creates the top left border character.

Returns:



122
123
124
# File 'lib/vedeu/borders/render.rb', line 122

def build_top_left
  build(top_left, :top_left, y, x) if left?
end

#build_top_rightVedeu::Views::Char (private)

Creates the top right border character.

Returns:



129
130
131
# File 'lib/vedeu/borders/render.rb', line 129

def build_top_right
  build(top_right, :top_right, y, xn) if right?
end

#caption?Boolean (private)

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

Returns:

  • (Boolean)


250
251
252
# File 'lib/vedeu/borders/render.rb', line 250

def caption?
  present?(caption)
end

#caption_charactersArray<String> (private)

Returns:

  • (Array<String>)


276
277
278
# File 'lib/vedeu/borders/render.rb', line 276

def caption_characters
  @caption_characters ||= caption_padded.chars
end

#caption_fits?Boolean (private)

Return boolean indicating whether the caption fits within the width of the bottom border.

Returns:

  • (Boolean)


266
267
268
# File 'lib/vedeu/borders/render.rb', line 266

def caption_fits?
  width > caption_characters.size
end

#caption_paddedString (private)

Pads the caption with a single whitespace either side.

Examples:

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

width = 10
# => ' Trunca '

Returns:

  • (String)

See Also:



308
309
310
# File 'lib/vedeu/borders/render.rb', line 308

def caption_padded
  truncated_caption.center(truncated_caption.size + 2)
end

#captionbarArray<Vedeu::Views::Char> (private)

Overwrite the border from #build_horizontal on the bottom border to include the caption if given.

Returns:



208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/vedeu/borders/render.rb', line 208

def captionbar
  return build_bottom unless caption? && caption_fits?

  caption_starts_at = (width - caption_characters.size) - 2

  caption_char = 0
  build_bottom.each_with_index do |char, index|
    next if index <= caption_starts_at || index > (width - 2)

    char.border  = nil
    char.value   = caption_characters[caption_char]
    caption_char += 1
  end
end

#geometryVedeu::Geometry::Geometry (private)



148
149
150
# File 'lib/vedeu/borders/render.rb', line 148

def geometry
  Vedeu.geometries.by_name(name)
end

#interfaceVedeu::Models::Interface (private) Also known as: parent

The parent of a border is always an interface.



165
166
167
# File 'lib/vedeu/borders/render.rb', line 165

def interface
  @interface ||= Vedeu.interfaces.by_name(name)
end

#left(iy = 0) ⇒ String (private)

Renders the left border for the interface.

Parameters:

  • iy (Fixnum) (defaults to: 0)

Returns:

  • (String)


174
175
176
177
178
# File 'lib/vedeu/borders/render.rb', line 174

def left(iy = 0)
  return [] unless left?

  build(vertical, :left_vertical, (by + iy), x)
end

#renderArray<Array<Vedeu::Views::Char>>

Returns:



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/vedeu/borders/render.rb', line 57

def render
  return [] unless enabled?

  Vedeu.timer("Bordering: '#{name}'") do
    out = [top, bottom]

    height.times { |y| out << [left(y), right(y)] }

    out.flatten
  end
end

#right(iy = 0) ⇒ String (private)

Renders the right border for the interface.

Parameters:

  • iy (Fixnum) (defaults to: 0)

Returns:

  • (String)


184
185
186
187
188
# File 'lib/vedeu/borders/render.rb', line 184

def right(iy = 0)
  return [] unless right?

  build(vertical, :right_vertical, (by + iy), xn)
end

#title?Boolean (private)

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

Returns:

  • (Boolean)


242
243
244
# File 'lib/vedeu/borders/render.rb', line 242

def title?
  present?(title)
end

#title_charactersArray<String> (private)

Returns:

  • (Array<String>)


271
272
273
# File 'lib/vedeu/borders/render.rb', line 271

def title_characters
  @title_characters ||= title_padded.chars
end

#title_fits?Boolean (private)

Return boolean indicating whether the title fits within the width of the top border.

Returns:

  • (Boolean)


258
259
260
# File 'lib/vedeu/borders/render.rb', line 258

def title_fits?
  width > title_characters.size
end

#title_paddedString (private)

Pads the title with a single whitespace either side.

Examples:

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

width = 10
# => ' Trunca '

Returns:

  • (String)

See Also:



292
293
294
# File 'lib/vedeu/borders/render.rb', line 292

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

#titlebarArray<Vedeu::Views::Char> (private)

Overwrite the border from #build_horizontal on the top border to include the title if given.

Returns:



227
228
229
230
231
232
233
234
235
236
# File 'lib/vedeu/borders/render.rb', line 227

def titlebar
  return build_top unless title? && title_fits?

  build_top.each_with_index do |char, index|
    next if index == 0 || index > title_characters.size

    char.border = nil
    char.value  = title_characters[(index - 1)]
  end
end

#topString (private)

Note:

If a title has been specified, then the top border will include this title unless the size of the interface is smaller than the padded title length.

Renders the top border for the interface.

Returns:

  • (String)


198
199
200
201
202
# File 'lib/vedeu/borders/render.rb', line 198

def top
  return [] unless top?

  [build_top_left, titlebar, build_top_right].compact
end

#truncated_captionString (private)

Truncates the caption 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 caption.

Examples:

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

width = 10
# => 'Trunca'

Returns:

  • (String)


344
345
346
# File 'lib/vedeu/borders/render.rb', line 344

def truncated_caption
  caption.chomp.slice(0..(width - 5))
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)


326
327
328
# File 'lib/vedeu/borders/render.rb', line 326

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