Module: OllamaChat::Clipboard

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

Instance Method Summary collapse

Instance Method Details

#copy_to_clipboardNilClass

Copy the last assistant’s message to the system clipboard.

This method checks if there is a last message from an assistant in the ‘@messages` array and copies its content to the clipboard using the specified command from `config.copy`. If no assistant response is available or the clipboard command is not found, appropriate error messages are displayed.

Returns:

  • (NilClass)

    Always returns nil.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/ollama_chat/clipboard.rb', line 11

def copy_to_clipboard
  if message = @messages.last and message.role == 'assistant'
    copy = `which #{config.copy}`.chomp
    if copy.present?
      IO.popen(copy, 'w') do |clipboard|
        clipboard.write(message.content)
      end
      STDOUT.puts "The last response has been copied to the system clipboard."
    else
      STDERR.puts "#{config.copy.inspect} command not found in system's path!"
    end
  else
    STDERR.puts "No response available to copy to the system clipboard."
  end
  nil
end

#paste_from_inputString

Paste content from the input.

Prompts the user to paste their content and then press C-d (Ctrl+D) to terminate input. Reads all lines from standard input until Ctrl+D is pressed and returns the pasted content as a string.

Returns:

  • (String)

    The pasted content entered by the user.



35
36
37
38
# File 'lib/ollama_chat/clipboard.rb', line 35

def paste_from_input
  STDOUT.puts bold { "Paste your content and then press C-d!" }
  STDIN.read
end