Class: Vedeu::Editor::Document
- Inherits:
-
Object
- Object
- Vedeu::Editor::Document
- Extended by:
- Forwardable
- Includes:
- Model
- Defined in:
- lib/vedeu/editor/document.rb
Overview
A collection of keypresses ordered by input.
Instance Attribute Summary collapse
Attributes included from Model
Instance Method Summary collapse
-
#border ⇒ Vedeu::Borders::Border
private
Retrieve the dimensions of the document from the interface of the same name.
-
#clear ⇒ void
Clear the document content in the terminal.
-
#clear_output ⇒ String
private
Return the data needed to clear the area which the document is using.
-
#cursor ⇒ Vedeu::Editor::Cursor
private
Return a virtual cursor to track the cursor position within the document.
-
#defaults ⇒ Hash
private
Returns the default options/attributes for this class.
-
#delete_character ⇒ Vedeu::Editor::Document
Deletes the character from the line where the cursor is currently positioned.
-
#delete_line ⇒ Vedeu::Editor::Document
Delete a line.
-
#down ⇒ Vedeu::Editor::Document
Move the virtual cursor down.
-
#execute ⇒ String
Returns the document as a string with line breaks if there is more than one line.
-
#initialize(attributes = {}) ⇒ Vedeu::Editor::Document
constructor
Returns a new instance of Vedeu::Editor::Document.
-
#insert_character(character) ⇒ Vedeu::Editor::Document
Inserts the given character in to the line where the cursor is currently positioned.
-
#insert_line ⇒ Vedeu::Editor::Document
Insert an empty line.
-
#left ⇒ Vedeu::Editor::Document
Move the virtual cursor left.
-
#line ⇒ Array<String|void>
Returns the current line from the collection of lines.
-
#lines ⇒ Array<String|void>
Returns the collection of lines which constitutes the document content.
-
#output ⇒ String
private
Return the data needed to render the document.
-
#refresh ⇒ Vedeu::Editor::Document
Store the document in the documents repository, clear and render the view.
-
#render ⇒ void
Render the document content in the terminal.
-
#reset! ⇒ Vedeu::Editor::Document
Reset the document to the empty state.
-
#right ⇒ Vedeu::Editor::Document
Move the virtual cursor right.
-
#up ⇒ Vedeu::Editor::Document
Move the virtual cursor up.
-
#visible ⇒ Vedeu::Editor::Lines
private
Return only the visible lines for the document based on the current virtual cursor position.
Methods included from Model
#deputy, #dsl_class, included, #store
Methods included from Common
#demodulize, #present?, #snake_case
Constructor Details
#initialize(attributes = {}) ⇒ Vedeu::Editor::Document
Returns a new instance of Vedeu::Editor::Document.
46 47 48 49 50 51 52 |
# File 'lib/vedeu/editor/document.rb', line 46 def initialize(attributes = {}) @attributes = defaults.merge!(attributes) @attributes.each do |key, value| instance_variable_set("@#{key}", value || defaults.fetch(key)) end end |
Instance Attribute Details
#attributes ⇒ Hash (readonly)
29 30 31 |
# File 'lib/vedeu/editor/document.rb', line 29 def attributes @attributes end |
#data ⇒ String
33 34 35 |
# File 'lib/vedeu/editor/document.rb', line 33 def data @data end |
#name ⇒ String
37 38 39 |
# File 'lib/vedeu/editor/document.rb', line 37 def name @name end |
Instance Method Details
#border ⇒ Vedeu::Borders::Border (private)
Retrieve the dimensions of the document from the interface of the same name.
217 218 219 |
# File 'lib/vedeu/editor/document.rb', line 217 def border @border ||= Vedeu.borders.by_name(name) end |
#clear ⇒ void
This method returns an undefined value.
Clear the document content in the terminal.
57 58 59 |
# File 'lib/vedeu/editor/document.rb', line 57 def clear Vedeu::Output::Direct.write(value: clear_output, x: bx, y: by) end |
#clear_output ⇒ String (private)
Return the data needed to clear the area which the document is using.
225 226 227 228 229 230 231 232 233 234 |
# File 'lib/vedeu/editor/document.rb', line 225 def clear_output clear_output = '' (by..byn).each do |row| clear_output << "\e[#{row};#{bx}H" + (' ' * width) end # reset cursor to top left of document clear_output << "\e[#{by};#{bx}H" end |
#cursor ⇒ Vedeu::Editor::Cursor (private)
Return a virtual cursor to track the cursor position within the document.
267 268 269 270 271 272 273 274 |
# File 'lib/vedeu/editor/document.rb', line 267 def cursor @cursor ||= Vedeu::Editor::Cursor.new(y: 0, x: 0, by: by, bx: bx, byn: byn, bxn: bxn) end |
#defaults ⇒ Hash (private)
Returns the default options/attributes for this class.
239 240 241 242 243 244 245 |
# File 'lib/vedeu/editor/document.rb', line 239 def defaults { data: Vedeu::Editor::Lines.new([Vedeu::Editor::Line.new]), name: nil, repository: Vedeu.documents, } end |
#delete_character ⇒ Vedeu::Editor::Document
Deletes the character from the line where the cursor is currently positioned.
65 66 67 68 69 70 71 |
# File 'lib/vedeu/editor/document.rb', line 65 def delete_character @lines = lines.delete_character(y, x - 1) left refresh end |
#delete_line ⇒ Vedeu::Editor::Document
Delete a line.
76 77 78 79 80 81 82 |
# File 'lib/vedeu/editor/document.rb', line 76 def delete_line @lines = lines.delete_line(y) up refresh end |
#down ⇒ Vedeu::Editor::Document
Move the virtual cursor down.
87 88 89 90 91 |
# File 'lib/vedeu/editor/document.rb', line 87 def down cursor.down refresh end |
#execute ⇒ String
Returns the document as a string with line breaks if there is more than one line.
97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/vedeu/editor/document.rb', line 97 def execute command = lines.map(&:to_s).join("\n") reset! clear Vedeu.trigger(:_command_, command) command end |
#insert_character(character) ⇒ Vedeu::Editor::Document
Inserts the given character in to the line where the cursor is currently positioned.
114 115 116 117 118 119 120 121 122 |
# File 'lib/vedeu/editor/document.rb', line 114 def insert_character(character) return self if character.is_a?(Symbol) @lines = lines.insert_character(character, y, x) right refresh end |
#insert_line ⇒ Vedeu::Editor::Document
Insert an empty line.
127 128 129 130 131 132 133 134 135 |
# File 'lib/vedeu/editor/document.rb', line 127 def insert_line down @lines = lines.insert_line(Vedeu::Editor::Line.new, y) bol refresh end |
#left ⇒ Vedeu::Editor::Document
Move the virtual cursor left.
140 141 142 143 144 |
# File 'lib/vedeu/editor/document.rb', line 140 def left cursor.left refresh end |
#line ⇒ Array<String|void>
Returns the current line from the collection of lines.
149 150 151 |
# File 'lib/vedeu/editor/document.rb', line 149 def line lines.line(y) end |
#lines ⇒ Array<String|void>
Returns the collection of lines which constitutes the document content.
157 158 159 |
# File 'lib/vedeu/editor/document.rb', line 157 def lines @lines ||= Vedeu::Editor::Lines.coerce(data) end |
#output ⇒ String (private)
Return the data needed to render the document.
250 251 252 253 254 255 256 257 258 259 260 261 |
# File 'lib/vedeu/editor/document.rb', line 250 def output output = '' visible.each_with_index do |line, y_index| output << Vedeu::Geometry::Position.new((by + y_index), bx).to_s output << line.to_s end output << cursor.to_s output end |
#refresh ⇒ Vedeu::Editor::Document
Store the document in the documents repository, clear and render the view.
185 186 187 188 189 190 191 |
# File 'lib/vedeu/editor/document.rb', line 185 def refresh store render self end |
#render ⇒ void
This method returns an undefined value.
Render the document content in the terminal.
164 165 166 167 168 |
# File 'lib/vedeu/editor/document.rb', line 164 def render clear Vedeu::Output::Direct.write(value: output, x: bx, y: by) end |
#reset! ⇒ Vedeu::Editor::Document
Reset the document to the empty state.
173 174 175 176 177 178 179 |
# File 'lib/vedeu/editor/document.rb', line 173 def reset! @cursor = cursor.reset! @lines = defaults[:data] refresh end |
#right ⇒ Vedeu::Editor::Document
Move the virtual cursor right.
196 197 198 199 200 |
# File 'lib/vedeu/editor/document.rb', line 196 def right cursor.right refresh end |
#up ⇒ Vedeu::Editor::Document
Move the virtual cursor up.
205 206 207 208 209 |
# File 'lib/vedeu/editor/document.rb', line 205 def up cursor.up refresh end |
#visible ⇒ Vedeu::Editor::Lines (private)
Return only the visible lines for the document based on the current virtual cursor position.
280 281 282 283 284 285 286 |
# File 'lib/vedeu/editor/document.rb', line 280 def visible Vedeu::Editor::Cropper.new(lines: lines, height: height, width: width, ox: ox, oy: oy).cropped end |