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
-
#determine_valid_output_filename(action) ⇒ Pathname?
Interactively determines a valid, non-conflicting filename for an output operation.
-
#edit_file(filename) ⇒ true, ...
Opens a file in the configured editor for editing.
-
#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.
-
#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.
-
#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
#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.
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.
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.
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.
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.
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 ||= (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.
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 |