Module: OllamaChat::Clipboard

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

Overview

A module that provides clipboard functionality for copying and pasting chat messages.

This module enables users to copy the last assistant message to the system clipboard and paste content from input, facilitating easy transfer of conversation content between different applications and contexts.

Examples:

Copying a message to clipboard

chat.copy_to_clipboard

Pasting content from input

content = chat.paste_from_input

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.



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

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.



46
47
48
49
# File 'lib/ollama_chat/clipboard.rb', line 46

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