Module: Editor

Defined in:
worklog/editor.rb

Overview

Editor to handle editing of log entries.

Constant Summary collapse

EDITOR_PREAMBLE =
ERB.new <<~README
  # Edit the content below, then save the file and quit the editor.
  # The update content will be saved. The content MUST be valid YAML
  # in order for the application to be able to update the records.

  <%= content %>
README

Class Method Summary collapse

Class Method Details

.open_editor(initial_text) ⇒ String

Open text editor (currently ViM) with the initial text. Upon saving and exiting the editor, the updated text is returned.

Parameters:

  • initial_text (String)

    The initial text to display in the editor.

Returns:

  • (String)

    The updated text.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'worklog/editor.rb', line 20

def self.open_editor(initial_text)
  file_handle = Tempfile.create
  file_handle.write(initial_text)
  file_handle.close

  # Open the editor with the temporary file.
  system('vim', file_handle.path)

  updated_text = nil

  # Read the updated text from the file.
  File.open(file_handle.path, 'r') do |f|
    updated_text = f.read
    WorkLogger.debug("Updated text: #{updated_text}")
  end
  File.unlink(file_handle.path)
  updated_text
end