Module: OllamaChat::FileEditing

Included in:
Chat
Defined in:
lib/ollama_chat/file_editing.rb

Overview

Module for editing files using the configured editor

Instance Method Summary collapse

Instance Method Details

#determine_valid_output_filename(action) ⇒ Pathname?

Interactively determines a valid, non-conflicting filename for an output operation.

This method prompts the user for a filename and ensures that the resulting file does not already exist on the filesystem. If the user cancels the prompt (e.g., via C-c), it returns nil.

Parameters:

  • action (String)

    a description of the action being performed, used in the prompt

Returns:

  • (Pathname, nil)

    the validated filename as a Pathname, or nil if the operation was cancelled



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/ollama_chat/file_editing.rb', line 113

def determine_valid_output_filename(action)
  loop do
    filename_str = ask?(
      prompt: "❓ Enter filename #{action}, C-c ⇒ cancel: "
    )
    if filename_str.nil?
      STDOUT.puts "Cancelled."
      return nil
    end

    filename = Pathname.new(filename_str)
    if filename.exist?
      STDERR.puts "File #{filename.to_path.inspect} already exists!"
    else
      return filename
    end
  end
end

#edit_file(filename) ⇒ true, ...

Opens a file in the configured editor for editing.

Parameters:

  • filename (String, Pathname)

    Path to the file to edit

Returns:

  • (true, false, nil)

    Exit status if successful, nil if editor not configured



8
9
10
11
12
13
14
# File 'lib/ollama_chat/file_editing.rb', line 8

def edit_file(filename)
  unless editor = OC::EDITOR?
    STDERR.puts "Need the environment variable var EDITOR defined to use an editor"
    return
  end
  system Shellwords.join([ editor, filename ])
end

#edit_text(text = nil, basename: %w[ text .md ])) ⇒ String?

Opens a temporary file in the configured editor, populates it with the given text, and returns the edited content.

The basename is used by the external editor to detect the filetype and provide appropriate syntax highlighting.

Parameters:

  • text (String, nil) (defaults to: nil)

    Initial content to pre-populate the editor

  • basename (Array<String>) (defaults to: %w[ text .md ]))

    Base name and extension for the temporary file

Returns:

  • (String, nil)

    The edited content if successful, nil otherwise



45
46
47
48
49
50
51
52
53
54
# File 'lib/ollama_chat/file_editing.rb', line 45

def edit_text(text = nil, basename: %w[ text .md ])
  edit_text_block(text, basename:) do |tmp|
    if result = edit_file(tmp.path)
      new_text = File.read(tmp.path)
      return new_text
    else
      STDERR.puts "Editor failed to edit #{tmp.path.inspect}."
    end
  end
end

#edit_text_block(text = nil, basename: %w[ text .md ],) {|Tempfile| ... } ⇒ Object

Creates a temporary file with the given basename, optionally populates it with text, and yields the file to the provided block.

The basename is used by the external editor to detect the filetype and provide appropriate syntax highlighting.

Parameters:

  • text (String, nil) (defaults to: nil)

    Initial content to write to the temporary file

  • basename (Array<String>) (defaults to: %w[ text .md ],)

    Base name and extension for the temporary file

Yields:

  • (Tempfile)

    The created temporary file

Returns:

  • (Object)

    The result of the block



26
27
28
29
30
31
32
33
34
# File 'lib/ollama_chat/file_editing.rb', line 26

def edit_text_block(text = nil, basename: %w[ text .md ], &block)
  Tempfile.create(basename) do |tmp|
    if text
      tmp.write(text)
      tmp.flush
    end
    block.(tmp)
  end
end

#perform_insert(text: nil, content: false) ⇒ true, false

Inserts provided text into Vim using the configured editor client.

Parameters:

  • text (String, nil) (defaults to: nil)

    – Text to insert; if ‘nil`, defaults to the last response.

  • content (true, false) (defaults to: false)

    – Treat input as content‑mode (‘true`) or plain text (`false`).

Returns:

  • (true, false)

    true if insertion succeeded; otherwise raises OllamaChat::OllamaChatError.

Raises:

  • (OllamaChat::OllamaChatError)
    • Raised when no text is available and insertion cannot proceed.

    • Also raised if the underlying Vim client reports a failure.



89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/ollama_chat/file_editing.rb', line 89

def perform_insert(text: nil, content: false)
  text ||= last_message_content(content:)
  if text
    unless vim.insert(text)
      raise OllamaChat::OllamaChatError, "Inserting text into editor failed!"
    end
    true
  else
    raise OllamaChat::OllamaChatError,
      "No text available to copy to the system clipboard."
  end
end

#vim(server_name = nil) ⇒ OllamaChat::Vim

The vim method creates and returns a new Vim instance for interacting with a Vim server.

This method initializes a Vim client that can be used to insert text into Vim buffers or open files in a running Vim server. It derives the server name from the provided argument or uses a default server name based on the current working directory.

Parameters:

  • server_name (String, nil) (defaults to: nil)

    the name of the Vim server to connect to If nil or empty, a default server name is derived from the current working directory

Returns:

  • (OllamaChat::Vim)

    a new Vim instance configured with the specified server name



70
71
72
73
# File 'lib/ollama_chat/file_editing.rb', line 70

def vim(server_name = nil)
  clientserver = config.vim?&.clientserver
  OllamaChat::Vim.new(server_name, clientserver:)
end