Class: Vedeu::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) ⇒ Position

Initializes a new instance of Position.

Parameters:

  • y (Fixnum) (defaults to: 1)

    The row/line position.

  • x (Fixnum) (defaults to: 1)

    The column/character position.



36
37
38
39
# File 'lib/vedeu/geometry/position.rb', line 36

def initialize(y = 1, x = 1)
  @y = (y.nil? || y < 1) ? 1 : y
  @x = (x.nil? || x < 1) ? 1 : x
end

Instance Attribute Details

#xObject (readonly) Also known as: last

Returns the value of attribute x.



7
8
9
# File 'lib/vedeu/geometry/position.rb', line 7

def x
  @x
end

#yObject (readonly) Also known as: first

Returns the value of attribute y.



7
8
9
# File 'lib/vedeu/geometry/position.rb', line 7

def y
  @y
end

Class Method Details

.coerce(value) ⇒ void

This method returns an undefined value.

Parameters:



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/vedeu/geometry/position.rb', line 15

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))

  else
    # not sure how to proceed

  end
end

Instance Method Details

#==(other) ⇒ Boolean

Parameters:

Returns:

  • (Boolean)


43
44
45
# File 'lib/vedeu/geometry/position.rb', line 43

def ==(other)
  eql?(other)
end

#eql?(other) ⇒ Boolean

Parameters:

Returns:

  • (Boolean)


49
50
51
# File 'lib/vedeu/geometry/position.rb', line 49

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

#sequenceString (private)

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

Returns:

  • (String)


75
76
77
# File 'lib/vedeu/geometry/position.rb', line 75

def sequence
  ["\e[", y, ';', x, 'H'].join
end

#to_s(&block) ⇒ String

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)

Returns:

  • (String)


59
60
61
62
63
64
65
66
67
# File 'lib/vedeu/geometry/position.rb', line 59

def to_s(&block)
  if block_given?
    [ sequence, yield, sequence ].join

  else
    sequence

  end
end