Class: Vedeu::Editor::Cursor

Inherits:
Object
  • Object
show all
Defined in:
lib/vedeu/editor/cursor.rb

Overview

Maintains a cursor position within the Document class.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Vedeu::Editor::Cursor

Returns a new instance of Vedeu::Editor::Cursor.

Parameters:

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

Options Hash (attributes):

  • y (Fixnum)

    The current line.

  • x (Fixnum)

    The current character with the line.

  • by (Fixnum)
  • bx (Fixnum)
  • byn (Fixnum)
  • bxn (Fixnum)
  • oy (Fixnum)
  • ox (Fixnum)


55
56
57
58
59
60
61
# File 'lib/vedeu/editor/cursor.rb', line 55

def initialize(attributes = {})
  @attributes = defaults.merge!(attributes)

  @attributes.each do |key, value|
    instance_variable_set("@#{key}", value)
  end
end

Instance Attribute Details

#bxFixnum

Returns:

  • (Fixnum)


12
13
14
# File 'lib/vedeu/editor/cursor.rb', line 12

def bx
  @bx
end

#bxnFixnum

Returns:

  • (Fixnum)


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

def bxn
  @bxn
end

#byFixnum

Returns:

  • (Fixnum)


16
17
18
# File 'lib/vedeu/editor/cursor.rb', line 16

def by
  @by
end

#bynFixnum

Returns:

  • (Fixnum)


24
25
26
# File 'lib/vedeu/editor/cursor.rb', line 24

def byn
  @byn
end

#oxFixnum

Returns:

  • (Fixnum)


28
29
30
# File 'lib/vedeu/editor/cursor.rb', line 28

def ox
  @ox
end

#oyFixnum

Returns:

  • (Fixnum)


32
33
34
# File 'lib/vedeu/editor/cursor.rb', line 32

def oy
  @oy
end

#xFixnum

Returns The column/character coordinate.

Returns:

  • (Fixnum)

    The column/character coordinate.



36
37
38
# File 'lib/vedeu/editor/cursor.rb', line 36

def x
  @x
end

#yFixnum

Returns The row/line coordinate.

Returns:

  • (Fixnum)

    The row/line coordinate.



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

def y
  @y
end

Instance Method Details

#bolVedeu::Editor::Cursor

Move the virtual cursor to the beginning of the line.



66
67
68
69
70
71
# File 'lib/vedeu/editor/cursor.rb', line 66

def bol
  @ox = 0
  @x  = 0

  self
end

#defaultsHash<Symbol => Fixnum|NilClass> (private)

Returns the default options/attributes for this class.

Returns:

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


152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/vedeu/editor/cursor.rb', line 152

def defaults
  {
    y:   0,
    x:   0,
    by:  nil,
    bx:  nil,
    byn: nil,
    bxn: nil,
    ox:  0,
    oy:  0,
  }
end

#downVedeu::Editor::Cursor

Move the virtual cursor down by one line.



76
77
78
79
80
# File 'lib/vedeu/editor/cursor.rb', line 76

def down
  @y += 1

  self
end

#leftVedeu::Editor::Cursor

Move the virtual cursor to the left.



85
86
87
88
89
90
# File 'lib/vedeu/editor/cursor.rb', line 85

def left
  @ox -= 1 unless @ox == 0
  @x  -= 1

  self
end

#real_xFixnum (private)

Return the real x coordinate.

Returns:

  • (Fixnum)


175
176
177
# File 'lib/vedeu/editor/cursor.rb', line 175

def real_x
  (bx + x) - ox
end

#real_yFixnum (private)

Return the real y coordinate.

Returns:

  • (Fixnum)


168
169
170
# File 'lib/vedeu/editor/cursor.rb', line 168

def real_y
  (by + y) - oy
end

#reset!Vedeu::Editor::Cursor

Move the virtual cursor to the origin (0, 0).



95
96
97
98
99
100
101
102
# File 'lib/vedeu/editor/cursor.rb', line 95

def reset!
  @x  = 0
  @y  = 0
  @ox = 0
  @oy = 0

  self
end

#rightVedeu::Editor::Cursor

Move the virtual cursor to the right.



107
108
109
110
111
# File 'lib/vedeu/editor/cursor.rb', line 107

def right
  @x += 1

  self
end

#to_sString

Return the escape sequence for setting the cursor position and show the cursor.

Returns:

  • (String)


117
118
119
# File 'lib/vedeu/editor/cursor.rb', line 117

def to_s
  "\e[#{real_y};#{real_x}H\e[?25h"
end

#upVedeu::Editor::Cursor

Move the virtual cursor up by one line.



124
125
126
127
128
129
# File 'lib/vedeu/editor/cursor.rb', line 124

def up
  @oy -= 1 unless @oy == 0
  @y  -= 1

  self
end