Module: LanguageOperator::CLI::Helpers::EditorHelper

Defined in:
lib/language_operator/cli/helpers/editor_helper.rb

Overview

Helper module for editing content in the user's preferred editor. Handles tempfile creation, cleanup, and editor invocation.

Class Method Summary collapse

Class Method Details

.edit_content(content, filename_prefix, extension = '.txt', default_editor: 'vi') ⇒ String

Edit content in the user's preferred editor

Examples:

Edit agent instructions

new_content = EditorHelper.edit_content(
  current_instructions,
  'agent-instructions-',
  '.txt'
)

Edit YAML configuration

new_yaml = EditorHelper.edit_content(
  model.to_yaml,
  'model-',
  '.yaml',
  default_editor: 'vim'
)

Parameters:

  • content (String)

    Content to edit

  • filename_prefix (String)

    Prefix for the temp file name

  • extension (String) (defaults to: '.txt')

    File extension (default: '.txt')

  • default_editor (String) (defaults to: 'vi')

    Editor to use if $EDITOR not set (default: 'vi')

Returns:

  • (String)

    The edited content

Raises:

  • (RuntimeError)

    If editor command fails



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/language_operator/cli/helpers/editor_helper.rb', line 34

def self.edit_content(content, filename_prefix, extension = '.txt', default_editor: 'vi')
  editor = ENV['EDITOR'] || default_editor
  tempfile = Tempfile.new([filename_prefix, extension])

  begin
    # Write content and flush to ensure it's on disk
    tempfile.write(content)
    tempfile.flush
    tempfile.close

    # Open in editor
    success = system("#{editor} #{tempfile.path}")
    raise "Editor command failed: #{editor}" unless success

    # Read edited content
    File.read(tempfile.path)
  ensure
    # Clean up temp file
    tempfile.unlink
  end
end