Class: OllamaChat::Vim
- Inherits:
-
Object
- Object
- OllamaChat::Vim
- Defined in:
- lib/ollama_chat/vim.rb
Overview
A class that provides functionality for inserting text into Vim buffers via remote communication.
Class Method Summary collapse
-
.default_server_name(name = Dir.pwd) ⇒ String
The default_server_name method generates a standardized server name based on a given name or the current working directory.
Instance Method Summary collapse
-
#col ⇒ Object
Returns the current column position of the cursor in the Vim server.
-
#initialize(server_name, clientserver: nil) ⇒ OllamaChat::Vim
constructor
Initializes a new Vim server connection.
-
#insert(text) ⇒ Object
Inserts text at the current cursor position in Vim.
Constructor Details
#initialize(server_name, clientserver: nil) ⇒ OllamaChat::Vim
Initializes a new Vim server connection
Creates a new OllamaChat::Vim instance for interacting with a specific Vim server. If no server name is provided, it derives a standardized server name based on the current working directory using the default_server_name method.
24 25 26 27 28 |
# File 'lib/ollama_chat/vim.rb', line 24 def initialize(server_name, clientserver: nil) server_name.full? or server_name = self.class.default_server_name @server_name = server_name @clientserver = clientserver || 'socket' end |
Class Method Details
.default_server_name(name = Dir.pwd) ⇒ String
The default_server_name method generates a standardized server name based on a given name or the current working directory.
This method creates a unique server identifier by combining the basename of the provided name (or current working directory) with a truncated MD5 hash digest of the full path. The resulting name is converted to uppercase for consistent formatting.
43 44 45 46 47 48 |
# File 'lib/ollama_chat/vim.rb', line 43 def self.default_server_name(name = Dir.pwd) prefix = File.basename(name) suffix = Digest::MD5.hexdigest(name)[0, 8] name = [ prefix, suffix ] * ?- name.upcase end |
Instance Method Details
#col ⇒ Object
Returns the current column position of the cursor in the Vim server
This method queries the specified Vim server for the current cursor position using Vim’s remote expression feature. It executes a Vim command that returns the result of ‘col(’.‘)`, which represents the current column number (1-indexed) of the cursor position.
73 74 75 |
# File 'lib/ollama_chat/vim.rb', line 73 def col `vim --clientserver "#@clientserver" --servername "#@server_name" --remote-expr "col('.')"`.chomp.to_i end |
#insert(text) ⇒ Object
Inserts text at the current cursor position in Vim
This method writes the provided text to a temporary file and uses Vim’s remote-send functionality to insert it at the current cursor position. The text is automatically indented to match the current column position.
57 58 59 60 61 62 63 64 65 |
# File 'lib/ollama_chat/vim.rb', line 57 def insert(text) spaces = (col - 1).clamp(0..) text = text.gsub(/^/, ' ' * spaces) Tempfile.open do |tmp| tmp.write(text) tmp.flush system %{vim --clientserver "#@clientserver" --servername "#@server_name" --remote-send "<ESC>:r #{tmp.path}<CR>"} end end |