Class: Vedeu::Geometry::Position
- Inherits:
-
Object
- Object
- Vedeu::Geometry::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::Geometry::Position.
- .coerce(value) ⇒ Vedeu::Geometry::Position
Instance Method Summary collapse
- #<=>(other) ⇒ Fixnum
-
#as_indices ⇒ Array<Fixnum>
Converts a position into an index for the terminal.
-
#down ⇒ Vedeu::Geometry::Position
Increase y coordinate; moves down.
-
#eql?(other) ⇒ Boolean
(also: #==)
An object is equal when its values are the same.
-
#initialize(y = 1, x = 1) ⇒ Vedeu::Geometry::Position
constructor
Initializes a new instance of Vedeu::Geometry::Position.
-
#left ⇒ Vedeu::Geometry::Position
Decrease x coordinate; moves left.
-
#right ⇒ Vedeu::Geometry::Position
Increase x coordinate; moves right.
-
#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_position ⇒ Vedeu::Geometry::Position
-
#to_s ⇒ String
(also: #to_str)
Return the escape sequence required to position the cursor at a particular point on the screen.
-
#up ⇒ Vedeu::Geometry::Position
Decrease y coordinate; moves up.
Constructor Details
#initialize(y = 1, x = 1) ⇒ Vedeu::Geometry::Position
Initializes a new instance of Vedeu::Geometry::Position.
46 47 48 49 |
# File 'lib/vedeu/geometry/position.rb', line 46 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
16 17 18 |
# File 'lib/vedeu/geometry/position.rb', line 16 def x @x end |
#y ⇒ Fixnum (readonly) Also known as: first
11 12 13 |
# File 'lib/vedeu/geometry/position.rb', line 11 def y @y end |
Class Method Details
.[](y, x) ⇒ Object
Convenience constructor for Vedeu::Geometry::Position.
22 23 24 |
# File 'lib/vedeu/geometry/position.rb', line 22 def self.[](y, x) new(y, x) end |
.coerce(value) ⇒ Vedeu::Geometry::Position
28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/vedeu/geometry/position.rb', line 28 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
#<=>(other) ⇒ Fixnum
64 65 66 67 68 69 70 71 72 |
# File 'lib/vedeu/geometry/position.rb', line 64 def <=>(other) if y == other.y x <=> other.x else y <=> other.y end end |
#as_indices ⇒ Array<Fixnum>
Converts a position into an index for the terminal. An index is the position minus 1.
55 56 57 58 59 60 |
# File 'lib/vedeu/geometry/position.rb', line 55 def as_indices xi = ((x - 1) <= 1) ? 0 : (x - 1) yi = ((y - 1) <= 1) ? 0 : (y - 1) [yi, xi] end |
#down ⇒ Vedeu::Geometry::Position
Increase y coordinate; moves down.
112 113 114 |
# File 'lib/vedeu/geometry/position.rb', line 112 def down Vedeu::Geometry::Position.new(y + 1, x) end |
#eql?(other) ⇒ Boolean Also known as: ==
An object is equal when its values are the same.
78 79 80 |
# File 'lib/vedeu/geometry/position.rb', line 78 def eql?(other) self.class == other.class && (x == other.x && y == other.y) end |
#left ⇒ Vedeu::Geometry::Position
Decrease x coordinate; moves left.
119 120 121 |
# File 'lib/vedeu/geometry/position.rb', line 119 def left Vedeu::Geometry::Position.new(y, x - 1) end |
#right ⇒ Vedeu::Geometry::Position
Increase x coordinate; moves right.
126 127 128 |
# File 'lib/vedeu/geometry/position.rb', line 126 def right Vedeu::Geometry::Position.new(y, x + 1) end |
#sequence ⇒ String (private)
Returns the escape sequence to reposition the cursors at the coordinates specified by x and y.
143 144 145 |
# File 'lib/vedeu/geometry/position.rb', line 143 def sequence "\e[#{y};#{x}H" end |
#to_a ⇒ Array<Fixnum>
Return a tuple containing the y and x coordinates.
86 87 88 |
# File 'lib/vedeu/geometry/position.rb', line 86 def to_a [y, x] end |
#to_position ⇒ Vedeu::Geometry::Position
91 92 93 |
# File 'lib/vedeu/geometry/position.rb', line 91 def to_position self end |
#to_s ⇒ String Also known as: to_str
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.
102 103 104 105 106 |
# File 'lib/vedeu/geometry/position.rb', line 102 def to_s return "#{sequence}#{yield}" if block_given? sequence end |
#up ⇒ Vedeu::Geometry::Position
Decrease y coordinate; moves up.
133 134 135 |
# File 'lib/vedeu/geometry/position.rb', line 133 def up Vedeu::Geometry::Position.new(y - 1, x) end |