Class: Vedeu::Position
- Inherits:
-
Object
- Object
- Vedeu::Position
- Defined in:
- lib/vedeu/geometry/position.rb
Overview
Change coordinates into an escape sequence to set the cursor position.
Instance Attribute Summary collapse
- #x ⇒ Fixnum (also: #last) readonly
- #y ⇒ Fixnum (also: #first) readonly
Class Method Summary collapse
-
.[](y, x) ⇒ Object
Convenience constructor for Vedeu::Position.
- .coerce(value) ⇒ void
Instance Method Summary collapse
-
#eql?(other) ⇒ Boolean
(also: #==)
An object is equal when its values are the same.
-
#initialize(y = 1, x = 1) ⇒ Position
constructor
Initializes a new instance of Vedeu::Position.
-
#sequence ⇒ String
private
Returns the escape sequence to reposition the cursors at the coordinates specified by x and y.
-
#to_a ⇒ Array<Fixnum>
Return a tuple containing the y and x coordinates.
-
#to_s ⇒ String
Return the escape sequence required to position the cursor at a particular point on the screen.
Constructor Details
#initialize(y = 1, x = 1) ⇒ Position
Initializes a new instance of Vedeu::Position.
44 45 46 47 |
# File 'lib/vedeu/geometry/position.rb', line 44 def initialize(y = 1, x = 1) @y = (y.nil? || y < 1) ? 1 : y @x = (x.nil? || x < 1) ? 1 : x end |
Instance Attribute Details
#x ⇒ Fixnum (readonly) Also known as: last
12 13 14 |
# File 'lib/vedeu/geometry/position.rb', line 12 def x @x end |
#y ⇒ Fixnum (readonly) Also known as: first
8 9 10 |
# File 'lib/vedeu/geometry/position.rb', line 8 def y @y end |
Class Method Details
.[](y, x) ⇒ Object
Convenience constructor for Vedeu::Position.
20 21 22 |
# File 'lib/vedeu/geometry/position.rb', line 20 def self.[](y, x) new(y, x) end |
.coerce(value) ⇒ void
This method returns an undefined value.
26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/vedeu/geometry/position.rb', line 26 def self.coerce(value) if value.is_a?(self) value elsif value.is_a?(Array) new(*value) elsif value.is_a?(Hash) new(value.fetch(:y, 1), value.fetch(:x, 1)) end end |
Instance Method Details
#eql?(other) ⇒ Boolean Also known as: ==
An object is equal when its values are the same.
53 54 55 |
# File 'lib/vedeu/geometry/position.rb', line 53 def eql?(other) self.class == other.class && (x == other.x && y == other.y) end |
#sequence ⇒ String (private)
Returns the escape sequence to reposition the cursors at the coordinates specified by x and y.
88 89 90 |
# File 'lib/vedeu/geometry/position.rb', line 88 def sequence ["\e[", y, ';', x, 'H'].join end |
#to_a ⇒ Array<Fixnum>
Return a tuple containing the y and x coordinates.
61 62 63 |
# File 'lib/vedeu/geometry/position.rb', line 61 def to_a [y, x] end |
#to_s ⇒ String
Return the escape sequence required to position the cursor at a particular point on the screen. When passed a block, will do the aforementioned, call the block and then reposition to this location.
72 73 74 75 76 77 78 79 80 |
# File 'lib/vedeu/geometry/position.rb', line 72 def to_s if block_given? [sequence, yield, sequence].join else sequence end end |