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

#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) ⇒ String?

The edit_text method temporarily writes the given text to a file, attempts to edit it using an external editor, and returns the edited content if successful.

Parameters:

  • text (String)

    the text to be edited

Returns:

  • (String, nil)

    the edited text or nil if editing failed



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

def edit_text(text)
  Tempfile.open do |tmp|
    tmp.write(text)
    tmp.flush

    if result = edit_file(tmp.path)
      new_text = File.read(tmp.path)
      return new_text
    else
      STDERR.puts "Editor failed to edit message."
    end
  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.



70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/ollama_chat/file_editing.rb', line 70

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



51
52
53
54
# File 'lib/ollama_chat/file_editing.rb', line 51

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