Class: Vedeu::Geometry

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

Overview

TODO:

Consider storing the Terminal size at the time of first creation,

this allows us to return the interface to its original dimensions if the terminal resizes back to normal size.

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

Geometry for Vedeu, as the same for ANSI terminals, has the origin at top-left, y = 1, x = 1. The ‘y’ coordinate is deliberately first.

     x    north    xn           # north:  y - 1
   y +--------------+           # top:    y
     |     top      |           # west:   x - 1
     |              |           # left:   x
west | left   right | east      # right:  xn
     |              |           # east:   xn + 1
     |    bottom    |           # bottom: yn
  yn +--------------+           # south:  yn + 1
          south

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 = {}) ⇒ Geometry

Returns a new instance of Geometry.

Parameters:

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

Options Hash (attributes):



50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/vedeu/models/geometry.rb', line 50

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

  @centred = @attributes[:centred]
  @height  = @attributes[:height]
  @name    = @attributes[:name]
  @width   = @attributes[:width]
  @x       = @attributes[:x]
  @xn      = @attributes[:xn]
  @y       = @attributes[:y]
  @yn      = @attributes[:yn]
  @repository = Vedeu.geometries
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



35
36
37
# File 'lib/vedeu/models/geometry.rb', line 35

def attributes
  @attributes
end

#centredObject

Returns the value of attribute centred.



34
35
36
# File 'lib/vedeu/models/geometry.rb', line 34

def centred
  @centred
end

#heightObject

See Also:



131
132
133
# File 'lib/vedeu/models/geometry.rb', line 131

def height
  @height
end

#nameObject

Returns the value of attribute name.



34
35
36
# File 'lib/vedeu/models/geometry.rb', line 34

def name
  @name
end

#widthFixnum

Returns a dynamic value calculated from the current terminal dimensions, combined with the desired 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:



126
127
128
# File 'lib/vedeu/models/geometry.rb', line 126

def width
  @width
end

#xFixnum

Returns the column/character start position for the interface.

Returns:



93
94
95
96
97
98
99
100
101
# File 'lib/vedeu/models/geometry.rb', line 93

def x
  if @x.is_a?(Proc)
    @x.call

  else
    @x

  end
end

#yFixnum

Returns the row/line start position for the interface.

Returns:



67
68
69
70
71
72
73
74
75
# File 'lib/vedeu/models/geometry.rb', line 67

def y
  if @y.is_a?(Proc)
    @y.call

  else
    @y

  end
end

Instance Method Details

#bottomFixnum

Returns the bottom coordinate of the interface, a fixed or dynamic value depending on the value of #top.

Returns:



203
204
205
# File 'lib/vedeu/models/geometry.rb', line 203

def bottom
  top + height
end

#defaultsHash (private)

The default values for a new instance of this class.

Returns:

  • (Hash)


250
251
252
253
254
255
256
257
258
259
260
261
262
# File 'lib/vedeu/models/geometry.rb', line 250

def defaults
  {
    centred: false,
    client:  nil,
    height:  Terminal.height,
    name:    '',
    width:   Terminal.width,
    x:       1,
    xn:      Terminal.width,
    y:       1,
    yn:      Terminal.height,
  }
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:



241
242
243
# File 'lib/vedeu/models/geometry.rb', line 241

def east(value = 1)
  right + value
end

#leftFixnum

Note:

Division which results in a non-integer will be rounded down.

Returns the left coordinate of the interface, a fixed or dynamic value depending on whether the interface is centred or not.

Returns:



174
175
176
177
178
179
180
181
182
# File 'lib/vedeu/models/geometry.rb', line 174

def left
  if centred
    Terminal.centre_x - (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:



163
164
165
# File 'lib/vedeu/models/geometry.rb', line 163

def north(value = 1)
  top - value
end

#rightFixnum

Returns the right coordinate of the interface, a fixed or dynamic value depending on the value of #left.

Returns:



226
227
228
# File 'lib/vedeu/models/geometry.rb', line 226

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:



218
219
220
# File 'lib/vedeu/models/geometry.rb', line 218

def south(value = 1)
  bottom + value
end

#topFixnum

Note:

Division which results in a non-integer will be rounded down.

Returns the top coordinate of the interface, a fixed or dynamic value depending on whether the interface is centred or not.

Returns:



142
143
144
145
146
147
148
149
150
# File 'lib/vedeu/models/geometry.rb', line 142

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

  else
    y

  end
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:



195
196
197
# File 'lib/vedeu/models/geometry.rb', line 195

def west(value = 1)
  left - value
end

#xnFixnum

Returns the column/character end position for the interface.

Returns:



106
107
108
109
110
111
112
113
114
# File 'lib/vedeu/models/geometry.rb', line 106

def xn
  if @xn.is_a?(Proc)
    @xn.call

  else
    @xn

  end
end

#ynFixnum

Returns the row/line end position for the interface.

Returns:



80
81
82
83
84
85
86
87
88
# File 'lib/vedeu/models/geometry.rb', line 80

def yn
  if @yn.is_a?(Proc)
    @yn.call

  else
    @yn

  end
end