Class: Vedeu::Editor::Cursor Private

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

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Maintains a cursor position within the Document class.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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

Parameters:

  • attributes (Hash<Symbol => Fixnum|String|Symbol>) (defaults to: {})

Options Hash (attributes):

  • y (Fixnum)

    The current line.

  • x (Fixnum)

    The current character within the line.

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


52
53
54
55
56
# File 'lib/vedeu/editor/cursor.rb', line 52

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

Instance Attribute Details

#nameNilClass|Symbol|String (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns The name of the model, the target model or the name of the associated model.

Returns:

  • (NilClass|Symbol|String)

    The name of the model, the target model or the name of the associated model.



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

def name
  @name
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

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns The column/character coordinate.

Returns:

  • (Fixnum)

    The column/character coordinate.



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

attr_writer :x

#yFixnum

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns The row/line coordinate.

Returns:

  • (Fixnum)

    The row/line coordinate.



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

attr_writer :y

Instance Method Details

#bolVedeu::Editor::Cursor

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Move the virtual cursor to the beginning of the line.



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

def bol
  @ox = 0
  @x  = 0

  self
end

#defaultsHash<Symbol => void> (private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The default options/attributes for a new instance of this class.

Returns:

  • (Hash<Symbol => void>)


173
174
175
176
177
178
179
180
181
# File 'lib/vedeu/editor/cursor.rb', line 173

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

#down(size = nil) ⇒ Vedeu::Editor::Cursor

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Move the virtual cursor down by one line.

Parameters:

  • size (Fixnum) (defaults to: nil)

    The number of characters on the next line. The cursor is placed at the end of this next line if the x coordinate is greater than the next line length.

Returns:



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

def down(size = nil)
  @y += 1
  @x = size if size && x > size

  self
end

#geometryVedeu::Geometries::Geometry (private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the named geometry from the geometries repository.



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

def geometry
  @geometry ||= Vedeu.geometries.by_name(name)
end

#leftVedeu::Editor::Cursor

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Move the virtual cursor to the left.



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

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

  self
end

#new_attributesHash<Symbol => Fixnum, String, Symbol> (private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Hash<Symbol => Fixnum, String, Symbol>)


198
199
200
201
202
203
204
205
206
207
# File 'lib/vedeu/editor/cursor.rb', line 198

def new_attributes
  {
    name:    name,
    x:       real_x,
    y:       real_y,
    ox:      0,
    oy:      0,
    visible: true,
  }
end

#real_xFixnum (private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Return the real x coordinate.

Returns:

  • (Fixnum)


193
194
195
# File 'lib/vedeu/editor/cursor.rb', line 193

def real_x
  (bx + x) - ox
end

#real_yFixnum (private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Return the real y coordinate.

Returns:

  • (Fixnum)


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

def real_y
  (by + y) - oy
end

#refreshVedeu::Editor::Cursor

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

TODO:

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

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



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

def refresh
  Vedeu::Cursors::Cursor.store(new_attributes)

  Vedeu.trigger(:_refresh_cursor_, name)

  self
end

#reset!Vedeu::Editor::Cursor

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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



108
109
110
111
112
113
114
115
# File 'lib/vedeu/editor/cursor.rb', line 108

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

  self
end

#rightVedeu::Editor::Cursor

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Move the virtual cursor to the right.



120
121
122
123
124
# File 'lib/vedeu/editor/cursor.rb', line 120

def right
  @x += 1

  self
end

#to_sString Also known as: to_str

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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

Returns:

  • (String)


130
131
132
# File 'lib/vedeu/editor/cursor.rb', line 130

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

#up(size = nil) ⇒ Vedeu::Editor::Cursor

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Move the virtual cursor up by one line.

Parameters:

  • size (Fixnum) (defaults to: nil)

    The number of characters on the next line. The cursor is placed at the end of this next line if the x coordinate is greater than the next line length.

Returns:



141
142
143
144
145
146
147
# File 'lib/vedeu/editor/cursor.rb', line 141

def up(size = nil)
  @oy -= 1 unless @oy == 0
  @y -= 1
  @x = size if size && x > size

  self
end