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.



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

#xFixnum (readonly) Also known as: last

Returns:

  • (Fixnum)


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

def x
  @x
end

#yFixnum (readonly) Also known as: first

Returns:

  • (Fixnum)


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.

Parameters:

  • y (Fixnum)

    The row/line position.

  • x (Fixnum)

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

Parameters:

Returns:



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

Parameters:

Returns:

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

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

Returns:

  • (Array<Fixnum>)


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

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

Parameters:

Returns:

  • (Boolean)


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

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

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

#sequenceString (private)

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

Returns:

  • (String)


143
144
145
# File 'lib/vedeu/geometry/position.rb', line 143

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

#to_aArray<Fixnum>

Return a tuple containing the y and x coordinates.

Returns:

  • (Array<Fixnum>)


86
87
88
# File 'lib/vedeu/geometry/position.rb', line 86

def to_a
  [y, x]
end

#to_positionVedeu::Geometry::Position



91
92
93
# File 'lib/vedeu/geometry/position.rb', line 91

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)


102
103
104
105
106
# File 'lib/vedeu/geometry/position.rb', line 102

def to_s
  return "#{sequence}#{yield}" if block_given?

  sequence
end

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