Class: Vedeu::Cursor

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Model
Defined in:
lib/vedeu/cursor/cursor.rb

Overview

Each interface has its own Cursor which maintains the position and visibility of the cursor within that interface.

Instance Attribute Summary collapse

Attributes included from Model

#repository

Instance Method Summary collapse

Methods included from Model

#demodulize, #deputy, #dsl_class, included, #store

Constructor Details

#initialize(attributes = {}) ⇒ Cursor

Returns a new instance of Vedeu::Cursor.

Parameters:

  • attributes (Hash) (defaults to: {})

Options Hash (attributes):

  • name (String)

    The name of the interface this cursor belongs to.

  • ox (Fixnum)

    The offset x coordinate.

  • oy (Fixnum)

    The offset y coordinate.

  • repository (Vedeu::Repository)
  • visible (Boolean)

    The visibility of the cursor.

  • x (Fixnum)

    The terminal x coordinate for the cursor.

  • y (Fixnum)

    The terminal y coordinate for the cursor.



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/vedeu/cursor/cursor.rb', line 58

def initialize(attributes = {})
  # Hack because Repository#by_name creates Cursor objects with just a
  # name.
  attributes = { name: attributes } if attributes.is_a?(String)

  @attributes = defaults.merge!(attributes)

  @name       = @attributes.fetch(:name)
  @ox         = @attributes.fetch(:ox)
  @oy         = @attributes.fetch(:oy)
  @repository = @attributes.fetch(:repository)
  @visible    = @attributes.fetch(:visible)
  @x          = @attributes.fetch(:x)
  @y          = @attributes.fetch(:y)

  @position   = Vedeu::Position.new(@y, @x)
end

Instance Attribute Details

#attributesHash (readonly)

Returns:

  • (Hash)


14
15
16
# File 'lib/vedeu/cursor/cursor.rb', line 14

def attributes
  @attributes
end

#nameString (readonly)

Returns:

  • (String)


18
19
20
# File 'lib/vedeu/cursor/cursor.rb', line 18

def name
  @name
end

#oxFixnum (readonly)

Returns:

  • (Fixnum)


22
23
24
# File 'lib/vedeu/cursor/cursor.rb', line 22

def ox
  @ox
end

#oyFixnum (readonly)

Returns:

  • (Fixnum)


26
27
28
# File 'lib/vedeu/cursor/cursor.rb', line 26

def oy
  @oy
end

#stateBoolean|Symbol (readonly)

Returns:

  • (Boolean|Symbol)


30
31
32
# File 'lib/vedeu/cursor/cursor.rb', line 30

def state
  @state
end

#visibleBoolean|Symbol (readonly) Also known as: visible?

Returns:

  • (Boolean|Symbol)


34
35
36
# File 'lib/vedeu/cursor/cursor.rb', line 34

def visible
  @visible
end

#xFixnum (readonly)

Returns:

  • (Fixnum)


39
40
41
# File 'lib/vedeu/cursor/cursor.rb', line 39

def x
  @x
end

#yFixnum (readonly)

Returns:

  • (Fixnum)


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

def y
  @y
end

Instance Method Details

#defaultsHash (private)

The default values for a new instance of this class.

Returns:

  • (Hash)


113
114
115
116
117
118
119
120
121
122
123
# File 'lib/vedeu/cursor/cursor.rb', line 113

def defaults
  {
    name:       '',
    ox:         0,
    oy:         0,
    repository: Vedeu.cursors,
    visible:    false,
    x:          1,
    y:          1,
  }
end

#inspectString

Returns:

  • (String)


77
78
79
80
81
82
83
84
# File 'lib/vedeu/cursor/cursor.rb', line 77

def inspect
  "<Vedeu::Cursor (#{name}, " \
  "#{visible}, "              \
  "x:#{x}, "                  \
  "y:#{y}, "                  \
  "ox:#{ox}, "                \
  "oy:#{oy})>"
end

#positionVedeu::Position (private)

Return the position of this cursor.

Returns:



106
107
108
# File 'lib/vedeu/cursor/cursor.rb', line 106

def position
  @position ||= Vedeu::Position.new(y, x)
end

#sequenceString (private)

Returns the escape sequence to position the cursor and set its visibility.

Returns:

  • (String)


128
129
130
# File 'lib/vedeu/cursor/cursor.rb', line 128

def sequence
  [position, visibility].join
end

#to_sString

Returns an escape sequence to position the cursor and set its visibility. When passed a block, will position the cursor, yield and return the original position.

Returns:

  • (String)


91
92
93
94
95
96
97
98
99
# File 'lib/vedeu/cursor/cursor.rb', line 91

def to_s
  if block_given?
    [sequence, yield, sequence].join

  else
    sequence

  end
end

#visibilityString (private)

Returns the escape sequence for setting the visibility of the cursor.

Returns:

  • (String)


135
136
137
138
139
140
141
142
143
# File 'lib/vedeu/cursor/cursor.rb', line 135

def visibility
  if visible?
    show_cursor

  else
    hide_cursor

  end
end