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
-
#edit_file(filename) ⇒ true, ...
Opens a file in the configured editor for editing.
-
#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.
-
#perform_insert(text: nil, content: false) ⇒ true, false
Inserts provided text into Vim using the configured editor client.
-
#vim(server_name = nil) ⇒ OllamaChat::Vim
The vim method creates and returns a new Vim instance for interacting with a Vim server.
Instance Method Details
#edit_file(filename) ⇒ true, ...
Opens a file in the configured editor for editing.
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.
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.
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 ||= (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.
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 |