Class: Vedeu::Borders::Refresh

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

Overview

Renders and refreshes the named border.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name = Vedeu.focus) ⇒ Vedeu::Borders::Refresh

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

Parameters:

  • name (String|Symbol) (defaults to: Vedeu.focus)

    The name of the interface/view border to be refreshed. Defaults to ‘Vedeu.focus`.



60
61
62
# File 'lib/vedeu/borders/refresh.rb', line 60

def initialize(name = Vedeu.focus)
  @name = present?(name) ? name : Vedeu.focus
end

Instance Attribute Details

#nameString|Symbol (readonly, protected)

Returns:

  • (String|Symbol)


74
75
76
# File 'lib/vedeu/borders/refresh.rb', line 74

def name
  @name
end

Class Method Details

.by_name(name = Vedeu.focus) ⇒ Array<Array<Vedeu::Views::Char>>

Examples:

Vedeu.trigger(:_refresh_border_, name)

Parameters:

  • name (String|Symbol) (defaults to: Vedeu.focus)

    The name of the interface/view border to be refreshed. Defaults to ‘Vedeu.focus`.

Returns:



49
50
51
52
53
# File 'lib/vedeu/borders/refresh.rb', line 49

def self.by_name(name = Vedeu.focus)
  name || Vedeu.focus

  new(name).by_name
end

Instance Method Details

#absent?(variable) ⇒ Boolean Originally defined in module Common

Returns a boolean indicating whether a variable is nil or empty.

Parameters:

  • variable (String|Symbol|Array|Fixnum)

    The variable to check.

Returns:

  • (Boolean)

#borderVedeu::Borders::Border (private)



79
80
81
# File 'lib/vedeu/borders/refresh.rb', line 79

def border
  @border ||= Vedeu.borders.by_name(name)
end

#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)


158
159
160
161
162
# File 'lib/vedeu/borders/refresh.rb', line 158

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:



99
100
101
102
103
104
105
106
# File 'lib/vedeu/borders/refresh.rb', line 99

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:



132
133
134
# File 'lib/vedeu/borders/refresh.rb', line 132

def build_bottom
  build_horizontal(:bottom_horizontal, yn)
end

#build_bottom_leftVedeu::Views::Char (private)

Creates the bottom left border character.

Returns:



111
112
113
# File 'lib/vedeu/borders/refresh.rb', line 111

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:



118
119
120
# File 'lib/vedeu/borders/refresh.rb', line 118

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:



173
174
175
176
177
# File 'lib/vedeu/borders/refresh.rb', line 173

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:



125
126
127
# File 'lib/vedeu/borders/refresh.rb', line 125

def build_top
  build_horizontal(:bottom_horizontal, y)
end

#build_top_leftVedeu::Views::Char (private)

Creates the top left border character.

Returns:



139
140
141
# File 'lib/vedeu/borders/refresh.rb', line 139

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:



146
147
148
# File 'lib/vedeu/borders/refresh.rb', line 146

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

#by_nameArray<Array<Vedeu::Views::Char>> Also known as: render

Returns:



65
66
67
# File 'lib/vedeu/borders/refresh.rb', line 65

def by_name
  Vedeu.render_output(output) if enabled? && visible?
end

#caption?Boolean (private)

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

Returns:

  • (Boolean)


267
268
269
# File 'lib/vedeu/borders/refresh.rb', line 267

def caption?
  present?(caption)
end

#caption_charactersArray<String> (private)

Returns:

  • (Array<String>)


293
294
295
# File 'lib/vedeu/borders/refresh.rb', line 293

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)


283
284
285
# File 'lib/vedeu/borders/refresh.rb', line 283

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:



325
326
327
# File 'lib/vedeu/borders/refresh.rb', line 325

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:



225
226
227
228
229
230
231
232
233
234
235
236
237
238
# File 'lib/vedeu/borders/refresh.rb', line 225

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

#demodulize(klass) ⇒ String Originally defined in module Common

Removes the module part from the expression in the string.

Examples:

demodulize('Vedeu::SomeModule::SomeClass') # => "SomeClass"

Parameters:

  • klass (Class|String)

Returns:

  • (String)

#geometryVedeu::Geometry::Geometry (private)



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

def geometry
  Vedeu.geometries.by_name(name)
end

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

The parent of a border is always an interface.



182
183
184
# File 'lib/vedeu/borders/refresh.rb', line 182

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)


191
192
193
194
195
# File 'lib/vedeu/borders/refresh.rb', line 191

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

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

#outputArray<Array<Vedeu::Views::Char>> (private)

Returns:



84
85
86
87
88
89
90
91
92
# File 'lib/vedeu/borders/refresh.rb', line 84

def output
  Vedeu.timer("Drawing border: '#{name}'".freeze) do
    out = [top, bottom]

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

    out.flatten
  end
end

#present?(variable) ⇒ Boolean Originally defined in module Common

Returns a boolean indicating whether a variable has a useful value.

Parameters:

  • variable (String|Symbol|Array|Fixnum)

    The variable to check.

Returns:

  • (Boolean)

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

Renders the right border for the interface.

Parameters:

  • iy (Fixnum) (defaults to: 0)

Returns:

  • (String)


201
202
203
204
205
# File 'lib/vedeu/borders/refresh.rb', line 201

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

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

#snake_case(name) ⇒ String Originally defined in module Common

Converts a class name to a lowercase snake case string.

Examples:

snake_case(MyClassName) # => "my_class_name"
snake_case(NameSpaced::ClassName)
# => "name_spaced/class_name"

Parameters:

  • name (String)

Returns:

  • (String)

#title?Boolean (private)

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

Returns:

  • (Boolean)


259
260
261
# File 'lib/vedeu/borders/refresh.rb', line 259

def title?
  present?(title)
end

#title_charactersArray<String> (private)

Returns:

  • (Array<String>)


288
289
290
# File 'lib/vedeu/borders/refresh.rb', line 288

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)


275
276
277
# File 'lib/vedeu/borders/refresh.rb', line 275

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:



309
310
311
# File 'lib/vedeu/borders/refresh.rb', line 309

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:



244
245
246
247
248
249
250
251
252
253
# File 'lib/vedeu/borders/refresh.rb', line 244

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)


215
216
217
218
219
# File 'lib/vedeu/borders/refresh.rb', line 215

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)


361
362
363
# File 'lib/vedeu/borders/refresh.rb', line 361

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)


343
344
345
# File 'lib/vedeu/borders/refresh.rb', line 343

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