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 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)
  • state (Boolean|Symbol)

    The visibility of the cursor, either true or false, :hide or :show.

  • x (Fixnum)

    The terminal x coordinate for the cursor.

  • y (Fixnum)

    The terminal y coordinate for the cursor.



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

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

  @attributes = defaults.merge!(attributes)

  @name       = @attributes.fetch(:name)
  @ox         = @attributes.fetch(:ox)
  @oy         = @attributes.fetch(:oy)
  @repository = @attributes.fetch(:repository)
  @state      = Vedeu::Visible.coerce(@attributes.fetch(:state))
  @x          = @attributes.fetch(:x)
  @y          = @attributes.fetch(:y)

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

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



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

def attributes
  @attributes
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#oxObject (readonly)

Returns the value of attribute ox.



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

def ox
  @ox
end

#oyObject (readonly)

Returns the value of attribute oy.



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

def oy
  @oy
end

#positionObject (readonly, private)

Returns the value of attribute position.



73
74
75
# File 'lib/vedeu/cursor/cursor.rb', line 73

def position
  @position
end

#stateObject (readonly)

Returns the value of attribute state.



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

def state
  @state
end

#xObject (readonly)

Returns the value of attribute x.



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

def x
  @x
end

#yObject (readonly)

Returns the value of attribute y.



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

def y
  @y
end

Instance Method Details

#defaultsHash (private)

The default values for a new instance of this class.

Returns:

  • (Hash)


78
79
80
81
82
83
84
85
86
87
88
# File 'lib/vedeu/cursor/cursor.rb', line 78

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

#sequenceString (private)

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

Returns:

  • (String)


93
94
95
# File 'lib/vedeu/cursor/cursor.rb', line 93

def sequence
  [ position, visibility ].join
end

#to_s(&block) ⇒ String

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.

Parameters:

  • block (Proc)

Returns:

  • (String)


61
62
63
64
65
66
67
68
69
# File 'lib/vedeu/cursor/cursor.rb', line 61

def to_s(&block)
  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)


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

def visibility
  state.cursor
end