Class: Vedeu::Border

Inherits:
Object
  • Object
show all
Includes:
Model
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 Model

#demodulize, #deputy, #dsl_class, included, #store

Constructor Details

#initialize(attributes = {}) ⇒ Border

Returns a new instance of 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 (Object)
  • enabled (Boolean)

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

  • horizontal (String)

    The horizontal border character.

  • name (String)
  • 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.

  • top_left (String)

    The top left border character.

  • top_right (String)

    The top right border character.

  • vertical (String)

    The vertical border character.



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/vedeu/output/border.rb', line 64

def initialize(attributes = {})
  @attributes   = defaults.merge!(attributes)
  @enabled      = @attributes[:enabled]
  @bottom_left  = @attributes[:bottom_left]
  @bottom_right = @attributes[:bottom_right]
  @show_bottom  = @attributes[:show_bottom]
  @show_left    = @attributes[:show_left]
  @show_right   = @attributes[:show_right]
  @show_top     = @attributes[:show_top]
  @top_left     = @attributes[:top_left]
  @top_right    = @attributes[:top_right]
  @horizontal   = @attributes[:horizontal]
  @vertical     = @attributes[:vertical]
  @name         = @attributes[:name]
  @colour       = @attributes[:colour]
  @repository   = Vedeu.borders
  @style        = @attributes[:style]
end

Instance Attribute Details

#attributesObject

Returns the value of attribute attributes.



17
18
19
# File 'lib/vedeu/output/border.rb', line 17

def attributes
  @attributes
end

#bottom_leftObject

Returns the value of attribute bottom_left.



17
18
19
# File 'lib/vedeu/output/border.rb', line 17

def bottom_left
  @bottom_left
end

#bottom_rightObject

Returns the value of attribute bottom_right.



17
18
19
# File 'lib/vedeu/output/border.rb', line 17

def bottom_right
  @bottom_right
end

#colourVedeu::Colour

Returns:



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

def colour
  @colour
end

#enabledObject (readonly) Also known as: enabled?

Returns the value of attribute enabled.



29
30
31
# File 'lib/vedeu/output/border.rb', line 29

def enabled
  @enabled
end

#horizontalObject

Returns the value of attribute horizontal.



17
18
19
# File 'lib/vedeu/output/border.rb', line 17

def horizontal
  @horizontal
end

#nameObject (readonly)

Returns the value of attribute name.



29
30
31
# File 'lib/vedeu/output/border.rb', line 29

def name
  @name
end

#show_bottomObject Also known as: bottom?

Returns the value of attribute show_bottom.



17
18
19
# File 'lib/vedeu/output/border.rb', line 17

def show_bottom
  @show_bottom
end

#show_leftObject Also known as: left?

Returns the value of attribute show_left.



17
18
19
# File 'lib/vedeu/output/border.rb', line 17

def show_left
  @show_left
end

#show_rightObject Also known as: right?

Returns the value of attribute show_right.



17
18
19
# File 'lib/vedeu/output/border.rb', line 17

def show_right
  @show_right
end

#show_topObject Also known as: top?

Returns the value of attribute show_top.



17
18
19
# File 'lib/vedeu/output/border.rb', line 17

def show_top
  @show_top
end

#styleVedeu::Style

Returns:



134
135
136
# File 'lib/vedeu/output/border.rb', line 134

def style
  @style
end

#top_leftObject

Returns the value of attribute top_left.



17
18
19
# File 'lib/vedeu/output/border.rb', line 17

def top_left
  @top_left
end

#top_rightObject

Returns the value of attribute top_right.



17
18
19
# File 'lib/vedeu/output/border.rb', line 17

def top_right
  @top_right
end

#verticalObject

Returns the value of attribute vertical.



17
18
19
# File 'lib/vedeu/output/border.rb', line 17

def vertical
  @vertical
end

Instance Method Details

#border(value, type = :border) ⇒ Vedeu::Char (private)

Parameters:

  • value (String)
  • type (Symbol|NilClass) (defaults to: :border)

Returns:



210
211
212
213
214
215
216
217
# File 'lib/vedeu/output/border.rb', line 210

def border(value, type = :border)
  Vedeu::Char.new({ value:    value,
                    parent:   interface,
                    colour:   colour,
                    style:    style,
                    position: nil,
                    border:   type })
end

#bottomString

Renders the bottom border for the interface.

Returns:

  • (String)


157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/vedeu/output/border.rb', line 157

def bottom
  return [] unless bottom?

  out = []

  out << border(bottom_left, :bottom_left) if left?
  width.times do
    out << border(horizontal, :bottom_horizontal)
  end
  out << border(bottom_right, :bottom_right) if right?

  out.flatten
end

#defaultsHash (private)

The default values for a new instance of this class.

Returns:

  • (Hash)


227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'lib/vedeu/output/border.rb', line 227

def defaults
  {
    bottom_left:  "\x6D", # └
    bottom_right: "\x6A", # ┘
    client:       nil,
    colour:       {},
    enabled:      false,
    horizontal:   "\x71", # ─
    name:         '',
    show_bottom:  true,
    show_left:    true,
    show_right:   true,
    show_top:     true,
    style:        [],
    top_left:     "\x6C", # ┌
    top_right:    "\x6B", # ┐
    vertical:     "\x78", # │
  }
end

#heightFixnum

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

Returns:



106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/vedeu/output/border.rb', line 106

def height
  return interface.height unless enabled?

  if top? && bottom?
    interface.height - 2

  elsif top? || bottom?
    interface.height - 1

  else
    interface.height

  end
end

#interfaceVedeu::Interface (private)

Returns:



220
221
222
# File 'lib/vedeu/output/border.rb', line 220

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

#leftString

Renders the left border for the interface.

Returns:

  • (String)


174
175
176
177
178
# File 'lib/vedeu/output/border.rb', line 174

def left
  return [] unless left?

  border(vertical, :left_vertical)
end

#rightString

Renders the right border for the interface.

Returns:

  • (String)


183
184
185
186
187
# File 'lib/vedeu/output/border.rb', line 183

def right
  return [] unless right?

  border(vertical, :right_vertical)
end

#to_sBoolean

Returns a string representation of the border for the interface without content.

Returns:

  • (Boolean)


149
150
151
152
# File 'lib/vedeu/output/border.rb', line 149

def to_s
  render = Vedeu::Viewport.new(interface).render
  render.map { |line| line.flatten.join }.join("\n")
end

#topString

Renders the top border for the interface.

Returns:

  • (String)


192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/vedeu/output/border.rb', line 192

def top
  return [] unless top?

  out = []
  out << border(top_left, :top_left) if left?
  width.times do
    out << border(horizontal, :top_horizontal)
  end
  out << border(top_right, :top_right) if right?

  out.flatten
end

#widthFixnum

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

Returns:



87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/vedeu/output/border.rb', line 87

def width
  return interface.width unless enabled?

  if left? && right?
    interface.width - 2

  elsif left? || right?
    interface.width - 1

  else
    interface.width

  end
end