Class: Vedeu::Editor::Document

Inherits:
Object
  • Object
show all
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

#repository

Instance Method Summary collapse

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.

Parameters:

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

Options Hash (attributes):



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

#attributesHash (readonly)

Returns:

  • (Hash)


29
30
31
# File 'lib/vedeu/editor/document.rb', line 29

def attributes
  @attributes
end

#dataString

Returns:

  • (String)


33
34
35
# File 'lib/vedeu/editor/document.rb', line 33

def data
  @data
end

#nameString

Returns:

  • (String)


37
38
39
# File 'lib/vedeu/editor/document.rb', line 37

def name
  @name
end

Instance Method Details

#borderVedeu::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

#clearvoid

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_outputString (private)

Return the data needed to clear the area which the document is using.

Returns:

  • (String)


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

#cursorVedeu::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

#defaultsHash (private)

Returns the default options/attributes for this class.

Returns:

  • (Hash)


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_characterVedeu::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_lineVedeu::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

#downVedeu::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

#executeString

Returns the document as a string with line breaks if there is more than one line.

Returns:

  • (String)


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.

Parameters:

  • character (String|Symbol)

Returns:



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_lineVedeu::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

#leftVedeu::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

#lineArray<String|void>

Returns the current line from the collection of lines.

Returns:

  • (Array<String|void>)


149
150
151
# File 'lib/vedeu/editor/document.rb', line 149

def line
  lines.line(y)
end

#linesArray<String|void>

Returns the collection of lines which constitutes the document content.

Returns:

  • (Array<String|void>)


157
158
159
# File 'lib/vedeu/editor/document.rb', line 157

def lines
  @lines ||= Vedeu::Editor::Lines.coerce(data)
end

#outputString (private)

Return the data needed to render the document.

Returns:

  • (String)


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

#refreshVedeu::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

#rendervoid

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

#rightVedeu::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

#upVedeu::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

#visibleVedeu::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