lib/vedeu/editor/document.rb in vedeu-0.6.51 vs lib/vedeu/editor/document.rb in vedeu-0.6.52
- old
+ new
@@ -75,13 +75,13 @@
# Deletes the character from the line where the cursor is
# currently positioned.
#
# @return [Vedeu::Editor::Document]
def delete_character
- return self if (x == 0 || x - 1 < 0) && y == 0
+ return self if document_start?
- if (x == 0 || x - 1 < 0) && y > 0
+ if first_char? && y > 0
delete_line
return
else
@@ -130,17 +130,16 @@
def insert_line
@lines = lines.insert_line(y + 1)
down
- cursor.bol.refresh
-
refresh
end
# Returns the current line from the collection of lines.
#
+ # @param index [Fixnum]
# @return [Array<String|void>]
def line(index = y)
lines.line(index)
end
@@ -158,10 +157,12 @@
def reset!
@cursor = cursor.reset!
@lines = defaults[:data]
+ cursor.refresh
+
refresh
end
# Store the document in the documents repository, clear and
# send the content to the terminal.
@@ -170,79 +171,74 @@
def refresh
store
Vedeu.trigger(:_clear_view_content_, name)
- Vedeu.render_output(output)
+ Vedeu.buffer_update(output)
- cursor.refresh
+ Vedeu.direct_write(output.map(&:to_s).join)
self
end
# Move the virtual cursor left.
#
# @return [Vedeu::Editor::Document]
def left
- return self if x - 1 < 0
+ return self if first_char?
cursor.left.refresh
self
end
# Move the virtual cursor right.
#
# @return [Vedeu::Editor::Document]
def right
- return self if x + 1 > line.size
+ return self if last_char?
cursor.right.refresh
self
end
# Move the virtual cursor up.
#
# @return [Vedeu::Editor::Document]
def up
- return self if y - 1 < 0
+ return self if first_line?
- cursor.up.refresh
+ cursor.up(prev_line_size).refresh
- reposition_cursor_x!
-
self
end
# Move the virtual cursor down.
#
# @return [Vedeu::Editor::Document]
def down
- return self if y + 1 >= lines.size
+ return self if last_line?
- cursor.down.refresh
+ cursor.down(next_line_size).refresh
- reposition_cursor_x!
-
self
end
private
- # Repositions the x coordinate of the virtual cursor to the end
- # of the line if the x coordinate is beyond the end of the line.
+ # @return [Boolean]
+ def document_start?
+ first_char? && first_line?
+ end
+
+ # Return a virtual cursor to track the cursor position within
+ # the document.
#
- # This is used when the cursor moves up or down, moving from a
- # long line to a shorter line.
- #
# @return [Vedeu::Editor::Cursor]
- def reposition_cursor_x!
- if x > line.size
- cursor.x = line.size
- cursor.refresh
- end
+ def cursor
+ @cursor ||= Vedeu::Editor::Cursor.new(name: name)
end
# Returns the default options/attributes for this class.
#
# @return [Hash<Symbol => void|Symbol]
@@ -252,10 +248,35 @@
name: nil,
repository: Vedeu.documents,
}
end
+ # @return [Boolean]
+ def first_char?
+ x - 1 < 0
+ end
+
+ # @return [Boolean]
+ def first_line?
+ y - 1 < 0
+ end
+
+ # @return [Boolean]
+ def last_char?
+ x + 1 > line.size
+ end
+
+ # @return [Boolean]
+ def last_line?
+ y + 1 >= lines.size
+ end
+
+ # @return [Fixnum]
+ def next_line_size
+ line(y + 1).size
+ end
+
# Return the data needed to render the document, based on the
# current virtual cursor position. This output may be a cropped
# representation of the full document depending on the size of
# the interface.
#
@@ -265,15 +286,12 @@
name: name,
ox: ox,
oy: oy).viewport
end
- # Return a virtual cursor to track the cursor position within
- # the document.
- #
- # @return [Vedeu::Editor::Cursor]
- def cursor
- @cursor ||= Vedeu::Editor::Cursor.new(name: name)
+ # @return [Fixnum]
+ def prev_line_size
+ line(y - 1).size
end
end # Document
end # Editor