Class: Vedeu::Editor::Cursor

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
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.

  • name (String|Symbol)
  • oy (Fixnum)
  • ox (Fixnum)


48
49
50
51
52
# File 'lib/vedeu/editor/cursor.rb', line 48

def initialize(attributes = {})
  defaults.merge!(attributes).each do |key, value|
    instance_variable_set("@#{key}", value)
  end
end

Instance Attribute Details

#nameString|Symbol (readonly)

Returns:

  • (String|Symbol)


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

def name
  @name
end

#oxFixnum

Returns:

  • (Fixnum)


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

def ox
  @ox
end

#oyFixnum

Returns:

  • (Fixnum)


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

def oy
  @oy
end

#xFixnum

Returns The column/character coordinate.

Returns:

  • (Fixnum)

    The column/character coordinate.



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

attr_writer :x

#yFixnum

Returns The row/line coordinate.

Returns:

  • (Fixnum)

    The row/line coordinate.



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

attr_writer :y

Instance Method Details

#bolVedeu::Editor::Cursor

Move the virtual cursor to the beginning of the line.



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

def bol
  @ox = 0
  @x  = 0

  self
end

#borderVedeu::Borders::Border (private)



160
161
162
# File 'lib/vedeu/editor/cursor.rb', line 160

def border
  @border ||= Vedeu.borders.by_name(name)
end

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

Returns the default options/attributes for this class.

Returns:

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


167
168
169
170
171
172
173
174
175
# File 'lib/vedeu/editor/cursor.rb', line 167

def defaults
  {
    name: '',
    ox:   0,
    oy:   0,
    x:    0,
    y:    0,
  }
end

#downVedeu::Editor::Cursor

Move the virtual cursor down by one line.



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

def down
  @y += 1

  self
end

#leftVedeu::Editor::Cursor

Move the virtual cursor to the left.



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

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

  self
end

#real_xFixnum (private)

Return the real x coordinate.

Returns:

  • (Fixnum)


187
188
189
# File 'lib/vedeu/editor/cursor.rb', line 187

def real_x
  (bx + x) - ox
end

#real_yFixnum (private)

Return the real y coordinate.

Returns:

  • (Fixnum)


180
181
182
# File 'lib/vedeu/editor/cursor.rb', line 180

def real_y
  (by + y) - oy
end

#refreshVedeu::Editor::Cursor

TODO:

GL 2015-10-02 Should ox/oy be 0; or set to @ox/@oy?

Overwrite the cursor of the same name. This ensure that on refresh the cursor stays in the correct position for the document being edited.



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

def refresh
  Vedeu::Cursors::Cursor.store(name:    name,
                               x:       real_x,
                               y:       real_y,
                               ox:      0,
                               oy:      0,
                               visible: true)

  Vedeu.trigger(:_refresh_cursor_, name)

  self
end

#reset!Vedeu::Editor::Cursor

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



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

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

  self
end

#rightVedeu::Editor::Cursor

Move the virtual cursor to the right.



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

def right
  @x += 1

  self
end

#to_sString

Return the escape sequence as a string for setting the cursor position and show the cursor.

Returns:

  • (String)


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

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

#upVedeu::Editor::Cursor

Move the virtual cursor up by one line.



134
135
136
137
138
139
# File 'lib/vedeu/editor/cursor.rb', line 134

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

  self
end