Module: GemHadar::Editor

Included in:
GemHadar
Defined in:
lib/gem_hadar/editor.rb

Overview

A module that provides editor integration functionality for GemHadar.

This module contains methods for determining the appropriate editor to use for interactive tasks, opening temporary files in editors for user modification, and editing specified files in an editor. It serves as a utility for automating text editing operations within the GemHadar framework.

Examples:

Finding and using an editor

editor = GemHadar::Editor.find_editor

Editing a temporary file returning the changed content

content = "Initial content"
edited_content = GemHadar::Editor.edit_temp_file(content)

Editing a specific file

GemHadar::Editor.edit_file('CHANGELOG.md')

Instance Method Summary collapse

Instance Method Details

#edit_file(filename) ⇒ true, ...

The edit_file method opens a specified file in an editor for user modification.

This method retrieves the configured editor using find_editor, then invokes the editor command with the provided filename as an argument. It waits for the editor process to complete and returns true if successful, or false if the editor command fails.

Parameters:

  • filename (String)

    the path to the file to be opened in the editor

Returns:

  • (true)

    if the editor command executes successfully

  • (false)

    if the editor command fails or returns a non-zero exit status

  • (nil)

    if the editor cannot be found or the operation is aborted



86
87
88
89
90
91
92
93
94
95
# File 'lib/gem_hadar/editor.rb', line 86

def edit_file(filename)
  editor = find_editor or return

  unless system("#{editor} #{filename}")
    warn "#{editor} returned #{$?.exitstatus} => Returning."
    return false
  end

  true
end

#edit_temp_file(content) ⇒ String?

The edit_temp_file method opens a temporary file in an editor for user modification.

This method creates a temporary markdown file with the provided content, opens it in the configured editor, waits for the user to finish editing, and then reads the modified content back from the file.

Parameters:

  • content (String)

    the initial content to write to the temporary file

Returns:

  • (String)

    the content of the file after editing, or nil if the editor could not be invoked or the file could not be read

  • (nil)

    if the editor could not be found or the operation was aborted



61
62
63
64
65
66
67
68
69
70
71
# File 'lib/gem_hadar/editor.rb', line 61

def edit_temp_file(content)
  temp_file = Tempfile.new(%w[ changelog .md ])
  temp_file.write(content)
  temp_file.close

  edit_file(temp_file.path) or return

  File.read(temp_file.path)
ensure
  temp_file&.close&.unlink
end

#find_editorString?

The find_editor method determines the appropriate editor to use for interactive tasks.

This method first checks the EDITOR environment variable for a custom editor specification. If the environment variable is not set, it falls back to using the vi editor by default. It then verifies that the identified editor exists in the file system before returning it.

Returns:

  • (String, nil)

    the path to the editor command if found, or nil if the editor cannot be located

  • (nil)

    if the editor cannot be found in the file system



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/gem_hadar/editor.rb', line 29

def find_editor
  if editor = ENV['EDITOR'].full?
    unless File.exist?(editor)
      if editor = `which #{editor}`.chomp.full?
        File.exist?(editor) or editor = nil
      end
    end
  end
  editor ||= %w[ vim vi ].map {
    path = `which #{_1}`.chomp.full? or next
    path if File.exist?(path)
  }.compact.first
  unless editor && File.exist?(editor)
    warn "Can't find EDITOR. => Returning."
    return
  end
  editor
end