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.
47 48 49 50 51 52 |
# File 'lib/vedeu/geometry/position.rb', line 47 def initialize(y = 1, x = 1) @y = (y.nil? || y < 1) ? 1 : y @x = (x.nil? || x < 1) ? 1 : x freeze end |
Instance Attribute Details
#x ⇒ Fixnum (readonly) Also known as: last
17 18 19 |
# File 'lib/vedeu/geometry/position.rb', line 17 def x @x end |
#y ⇒ Fixnum (readonly) Also known as: first
12 13 14 |
# File 'lib/vedeu/geometry/position.rb', line 12 def y @y end |
Class Method Details
.[](y, x) ⇒ Object
Convenience constructor for Vedeu::Geometry::Position.
23 24 25 |
# File 'lib/vedeu/geometry/position.rb', line 23 def self.[](y, x) new(y, x) end |
.coerce(value) ⇒ Vedeu::Geometry::Position
29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/vedeu/geometry/position.rb', line 29 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
67 68 69 70 71 72 73 74 75 |
# File 'lib/vedeu/geometry/position.rb', line 67 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.
58 59 60 61 62 63 |
# File 'lib/vedeu/geometry/position.rb', line 58 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.
116 117 118 |
# File 'lib/vedeu/geometry/position.rb', line 116 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.
81 82 83 |
# File 'lib/vedeu/geometry/position.rb', line 81 def eql?(other) self.class == other.class && (x == other.x && y == other.y) end |
#left ⇒ Vedeu::Geometry::Position
Decrease x coordinate; moves left.
123 124 125 |
# File 'lib/vedeu/geometry/position.rb', line 123 def left Vedeu::Geometry::Position.new(y, x - 1) end |
#right ⇒ Vedeu::Geometry::Position
Increase x coordinate; moves right.
130 131 132 |
# File 'lib/vedeu/geometry/position.rb', line 130 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.
147 148 149 |
# File 'lib/vedeu/geometry/position.rb', line 147 def sequence "\e[#{y};#{x}H".freeze end |
#to_a ⇒ Array<Fixnum>
Return a tuple containing the y and x coordinates.
89 90 91 |
# File 'lib/vedeu/geometry/position.rb', line 89 def to_a [y, x] end |
#to_position ⇒ Vedeu::Geometry::Position
94 95 96 |
# File 'lib/vedeu/geometry/position.rb', line 94 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.
106 107 108 109 110 |
# File 'lib/vedeu/geometry/position.rb', line 106 def to_s return "#{sequence}#{yield}".freeze if block_given? sequence end |
#up ⇒ Vedeu::Geometry::Position
Decrease y coordinate; moves up.
137 138 139 |
# File 'lib/vedeu/geometry/position.rb', line 137 def up Vedeu::Geometry::Position.new(y - 1, x) end |