Class: Vedeu::Geometry::Position

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

Overview

Change coordinates into an escape sequence to set the cursor position.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(y = 1, x = 1) ⇒ Vedeu::Geometry::Position

Initializes a new instance of Vedeu::Geometry::Position.

Parameters:

  • y (Fixnum) (defaults to: 1)

    The row/line position.

  • x (Fixnum) (defaults to: 1)

    The column/character 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

#xFixnum (readonly) Also known as: last

Returns:

  • (Fixnum)


17
18
19
# File 'lib/vedeu/geometry/position.rb', line 17

def x
  @x
end

#yFixnum (readonly) Also known as: first

Returns:

  • (Fixnum)


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.

Parameters:

  • y (Fixnum)

    The row/line position.

  • x (Fixnum)

    The column/character 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

Parameters:

Returns:



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

Parameters:

Returns:

  • (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_indicesArray<Fixnum>

Converts a position into an index for the terminal. An index is the position minus 1.

Returns:

  • (Array<Fixnum>)


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

#downVedeu::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.

Parameters:

Returns:

  • (Boolean)


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

#leftVedeu::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

#rightVedeu::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

#sequenceString (private)

Returns the escape sequence to reposition the cursors at the coordinates specified by x and y.

Returns:

  • (String)


147
148
149
# File 'lib/vedeu/geometry/position.rb', line 147

def sequence
  "\e[#{y};#{x}H".freeze
end

#to_aArray<Fixnum>

Return a tuple containing the y and x coordinates.

Returns:

  • (Array<Fixnum>)


89
90
91
# File 'lib/vedeu/geometry/position.rb', line 89

def to_a
  [y, x]
end

#to_positionVedeu::Geometry::Position



94
95
96
# File 'lib/vedeu/geometry/position.rb', line 94

def to_position
  self
end

#to_sString 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.

Yield Returns:

  • (String)

    Returns the block wrapped in position escape sequences.

Returns:

  • (String)


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

#upVedeu::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