Class: Vedeu::Geometries::Position Private

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

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

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::Geometries::Position

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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

Parameters:

  • y (Fixnum) (defaults to: 1)

    The row/line position.

  • x (Fixnum) (defaults to: 1)

    The column/character position.



58
59
60
61
62
63
# File 'lib/vedeu/geometries/position.rb', line 58

def initialize(y = 1, x = 1)
  @y = Vedeu::Point.coerce(value: y, max: Vedeu.height).value
  @x = Vedeu::Point.coerce(value: x, max: Vedeu.width).value

  freeze
end

Instance Attribute Details

#xFixnum (readonly) Also known as: last

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Fixnum)


21
22
23
# File 'lib/vedeu/geometries/position.rb', line 21

def x
  @x
end

#yFixnum (readonly) Also known as: first

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Fixnum)


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

def y
  @y
end

Class Method Details

.[](y = 1, x = 1) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Convenience constructor for Vedeu::Geometries::Position.

Parameters:

  • y (Fixnum) (defaults to: 1)

    The row/line position.

  • x (Fixnum) (defaults to: 1)

    The column/character position.



27
28
29
# File 'lib/vedeu/geometries/position.rb', line 27

def self.[](y = 1, x = 1)
  new(y, x)
end

.coerce(value) ⇒ Vedeu::Geometries::Position

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:

Returns:



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/vedeu/geometries/position.rb', line 34

def self.coerce(value)
  if value.is_a?(self)
    value

  elsif value.is_a?(Array)
    new(*value)

  elsif value.is_a?(Fixnum)
    new(value, 1)

  elsif value.is_a?(Hash)
    new(value.fetch(:y, 1), value.fetch(:x, 1))

  elsif value.is_a?(NilClass)
    nil

  end
end

Instance Method Details

#<=>(other) ⇒ Fixnum

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:

Returns:

  • (Fixnum)


78
79
80
81
82
83
84
85
86
# File 'lib/vedeu/geometries/position.rb', line 78

def <=>(other)
  if y == other.y
    x <=> other.x

  else
    y <=> other.y

  end
end

#as_indicesArray<Fixnum>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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

Returns:

  • (Array<Fixnum>)


69
70
71
72
73
74
# File 'lib/vedeu/geometries/position.rb', line 69

def as_indices
  xi = ((x - 1) <= 1) ? 0 : (x - 1)
  yi = ((y - 1) <= 1) ? 0 : (y - 1)

  [yi, xi]
end

#downVedeu::Geometries::Position

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Increase y coordinate; moves down.



141
142
143
# File 'lib/vedeu/geometries/position.rb', line 141

def down
  Vedeu::Geometries::Position.new(y + 1, x)
end

#eql?(other) ⇒ Boolean Also known as: ==

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

An object is equal when its values are the same.

Parameters:

Returns:



92
93
94
# File 'lib/vedeu/geometries/position.rb', line 92

def eql?(other)
  self.class == other.class && x == other.x && y == other.y
end

#leftVedeu::Geometries::Position

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Decrease x coordinate; moves left.



148
149
150
# File 'lib/vedeu/geometries/position.rb', line 148

def left
  Vedeu::Geometries::Position.new(y, x - 1)
end

#rightVedeu::Geometries::Position

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Increase x coordinate; moves right.



155
156
157
# File 'lib/vedeu/geometries/position.rb', line 155

def right
  Vedeu::Geometries::Position.new(y, x + 1)
end

#sequenceString (private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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

Returns:

  • (String)


172
173
174
# File 'lib/vedeu/geometries/position.rb', line 172

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

#to_aArray<Fixnum>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Return a tuple containing the y and x coordinates.

Returns:

  • (Array<Fixnum>)


100
101
102
# File 'lib/vedeu/geometries/position.rb', line 100

def to_a
  [y, x]
end

#to_astString

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (String)


105
106
107
# File 'lib/vedeu/geometries/position.rb', line 105

def to_ast
  ":y#{y}_x#{x}"
end

#to_hHash<Symbol => Fixnum|NilClass> Also known as: to_hash

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Return the position as a Hash.

Returns:

  • (Hash<Symbol => Fixnum|NilClass>)


112
113
114
115
116
117
118
119
# File 'lib/vedeu/geometries/position.rb', line 112

def to_h
  {
    position: {
      y: y,
      x: x,
    },
  }
end

#to_s(&block) ⇒ String Also known as: to_str

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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.

Parameters:

  • block (Proc)

Yield Returns:

  • (String)

    Returns the block wrapped in position escape sequences.

Returns:

  • (String)


131
132
133
134
135
# File 'lib/vedeu/geometries/position.rb', line 131

def to_s(&block)
  return "#{sequence}#{yield}" if block_given?

  sequence
end

#upVedeu::Geometries::Position

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Decrease y coordinate; moves up.



162
163
164
# File 'lib/vedeu/geometries/position.rb', line 162

def up
  Vedeu::Geometries::Position.new(y - 1, x)
end