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.
Instance Method Summary collapse
-
#edit_file(filename) ⇒ true, ...
The edit_file method opens a specified file in an editor for user modification.
-
#edit_temp_file(content) ⇒ String?
The edit_temp_file method opens a temporary file in an editor for user modification.
-
#find_editor ⇒ String?
The find_editor method determines the appropriate editor to use for interactive tasks.
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.
75 76 77 78 79 80 81 82 83 84 |
# File 'lib/gem_hadar/editor.rb', line 75 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.
50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/gem_hadar/editor.rb', line 50 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_editor ⇒ String?
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.
29 30 31 32 33 34 35 36 |
# File 'lib/gem_hadar/editor.rb', line 29 def find_editor editor = ENV.fetch('EDITOR', `which vi`.chomp) unless File.exist?(editor) warn "Can't find EDITOR. => Returning." return end editor end |