Class: Vedeu::Geometry

Inherits:
Object
  • Object
show all
Defined in:
lib/vedeu/models/geometry.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Geometry

Calculate and provide interface geometry determined by both the client’s requirements and the terminal’s current viewing area.

Parameters:

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


11
12
13
14
15
16
17
# File 'lib/vedeu/models/geometry.rb', line 11

def initialize(attributes = {})
  @attributes = defaults.merge!(attributes)

  @centred = @attributes[:centred]
  @height  = @attributes[:height]
  @width   = @attributes[:width]
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



4
5
6
# File 'lib/vedeu/models/geometry.rb', line 4

def attributes
  @attributes
end

#centredObject (readonly)

Returns the value of attribute centred.



4
5
6
# File 'lib/vedeu/models/geometry.rb', line 4

def centred
  @centred
end

#heightObject (readonly)

Returns the value of attribute height.



4
5
6
# File 'lib/vedeu/models/geometry.rb', line 4

def height
  @height
end

#widthObject (readonly)

Returns the value of attribute width.



4
5
6
# File 'lib/vedeu/models/geometry.rb', line 4

def width
  @width
end

Instance Method Details

#bottomFixnum

Returns:



154
155
156
# File 'lib/vedeu/models/geometry.rb', line 154

def bottom
  top + height
end

#defaultsHash (private)

Returns:

  • (Hash)


213
214
215
216
217
218
219
220
221
# File 'lib/vedeu/models/geometry.rb', line 213

def defaults
  {
    y:       1,
    x:       1,
    width:   Terminal.width,
    height:  Terminal.height,
    centred: false,
  }
end

#east(value = 1) ⇒ Fixnum

Returns the column after right by default.

Examples:

`right` is 19.

east     # => 20
east(2)  # => 21
east(-4) # => 15

Parameters:

  • value (Fixnum) (defaults to: 1)

Returns:



192
193
194
# File 'lib/vedeu/models/geometry.rb', line 192

def east(value = 1)
  right + value
end

#leftFixnum

Returns a fixed or dynamic value depending on whether the interface is centred or not.

Returns:



128
129
130
131
132
133
134
135
136
# File 'lib/vedeu/models/geometry.rb', line 128

def left
  if centred
    Terminal.centre_x - (viewport_width / 2)

  else
    x

  end
end

#north(value = 1) ⇒ Fixnum

Returns the row above the top by default.

Examples:

`top` is 4.

north     # => 3
north(2)  # => 2
north(-4) # => 8

Parameters:

  • value (Fixnum) (defaults to: 1)

Returns:



120
121
122
# File 'lib/vedeu/models/geometry.rb', line 120

def north(value = 1)
  top - value
end

#origin(index = 0, &block) ⇒ String

Returns the top-left coordinate, relative to the interface’s position.

Parameters:

  • index (Fixnum) (defaults to: 0)
  • block (Proc)

Returns:

  • (String)


91
92
93
# File 'lib/vedeu/models/geometry.rb', line 91

def origin(index = 0, &block)
  Esc.set_position(virtual_y[index], left, &block)
end

#rightFixnum

Returns a fixed or dynamic value depending on whether the interface is centred or not.

Returns:



177
178
179
# File 'lib/vedeu/models/geometry.rb', line 177

def right
  left + width
end

#south(value = 1) ⇒ Fixnum

Returns the row below the bottom by default.

Examples:

`bottom` is 12.

south     # => 13
south(2)  # => 14
south(-4) # => 8

Parameters:

  • value (Fixnum) (defaults to: 1)

Returns:



169
170
171
# File 'lib/vedeu/models/geometry.rb', line 169

def south(value = 1)
  bottom + value
end

#topFixnum

Returns a fixed or dynamic value depending on whether the interface is centred or not.

Returns:



99
100
101
102
103
104
105
106
107
# File 'lib/vedeu/models/geometry.rb', line 99

def top
  if centred
    Terminal.centre_y - (viewport_height / 2)

  else
    y

  end
end

#viewport_heightFixnum

Returns a dynamic value calculated from the current terminal height, combined with the desired row start point.

If the interface is ‘centred` then if the terminal resizes, this value should attempt to accommodate that.

For uncentred interfaces, when the terminal resizes, then this will help Vedeu render the view to ensure the content is not off-screen.

Returns:



76
77
78
79
80
81
82
83
84
# File 'lib/vedeu/models/geometry.rb', line 76

def viewport_height
  if (y + height) > Terminal.height
    height - ((y + height) - Terminal.height)

  else
    height

  end
end

#viewport_widthFixnum

Returns a dynamic value calculated from the current terminal width, combined with the desired column start point.

If the interface is ‘centred` then if the terminal resizes, this value should attempt to accommodate that.

For uncentred interfaces, when the terminal resizes, then this will help Vedeu render the view to ensure no row/line overruns or that the content is not off-screen.

Returns:



56
57
58
59
60
61
62
63
64
# File 'lib/vedeu/models/geometry.rb', line 56

def viewport_width
  if (x + width) > Terminal.width
    width - ((x + width) - Terminal.width)

  else
    width

  end
end

#virtual_xArray (private)

Provides a virtual x position within the interface’s dimensions.

Returns:

  • (Array)


208
209
210
# File 'lib/vedeu/models/geometry.rb', line 208

def virtual_x
  (left..right).to_a
end

#virtual_yArray (private)

Provides a virtual y position within the interface’s dimensions.

Returns:

  • (Array)


201
202
203
# File 'lib/vedeu/models/geometry.rb', line 201

def virtual_y
  (top..bottom).to_a
end

#west(value = 1) ⇒ Fixnum

Returns the column before left by default.

Examples:

`left` is 8.

west      # => 7
west(2)   # => 6
west(-4)  # => 12

Parameters:

  • value (Fixnum) (defaults to: 1)

Returns:



149
150
151
# File 'lib/vedeu/models/geometry.rb', line 149

def west(value = 1)
  left - value
end

#xFixnum

Returns the column position for the interface.

Returns:



35
36
37
38
39
40
41
42
43
# File 'lib/vedeu/models/geometry.rb', line 35

def x
  if attributes[:x].is_a?(Proc)
    attributes[:x].call

  else
    attributes[:x]

  end
end

#yFixnum

Returns the row/line position for the interface.

Returns:



22
23
24
25
26
27
28
29
30
# File 'lib/vedeu/models/geometry.rb', line 22

def y
  if attributes[:y].is_a?(Proc)
    attributes[:y].call

  else
    attributes[:y]

  end
end