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.

Instance Method Summary collapse

Instance Method Details

#copy_to_clipboard(edit: false) ⇒ TrueClass (private)

Copies the last assistant message to the system clipboard.

This method is the interface for copying assistant messages to the clipboard in the chat. It calls perform_copy_to_clipboard internally and handles any OllamaChat::OllamaChatError exceptions by printing the error message to standard error and does not re-raise the exception.

Parameters:

  • edit (true, false) (defaults to: false)

    If true, opens the content in the editor for modification before copying (default: true)

Returns:

  • (TrueClass)

    if the copying has been performed successfully.



115
116
117
118
119
120
121
# File 'lib/ollama_chat/clipboard.rb', line 115

def copy_to_clipboard(edit: false)
  perform_copy_to_clipboard(edit:)
  STDOUT.puts "The last response has been successfully copied to the system clipboard."
  true
rescue OllamaChat::OllamaChatError => e
  STDERR.puts e.message
end

#last_message_content(content: false) ⇒ String? (private)

Returns the content of the last assistant message.

This private helper method finds the most recent message from the assistant in the messages array and returns its content. It is used by perform_copy_to_clipboard when no custom text is provided.

Examples:

# Assuming @messages contains assistant messages
last_message_content
# => "This is the last assistant response"

Parameters:

  • content (true, false) (defaults to: false)

    If true, returns the content of the message; if false, returns nil if no assistant message is found (default: false)

Returns:

  • (String, nil)

    The content of the last assistant message, or nil if no assistant message is found



100
101
102
# File 'lib/ollama_chat/clipboard.rb', line 100

def last_message_content(content: false)
  @messages.find_last(content:) { _1.role == 'assistant' }&.content
end

#paste_from_clipboard(edit: false) ⇒ String? (private)

Pastes content from the system clipboard into the chat.

This method retrieves content from the system clipboard using the configured paste command and integrates it into the chat session. It handles clipboard errors gracefully by displaying error messages to standard error.

Parameters:

  • edit (true, false) (defaults to: false)

    If true, opens the content in the editor for modification before returning (default: false)

Returns:

  • (String, nil)

    The content retrieved from the system clipboard, or nil if an error occurred



134
135
136
137
138
139
140
# File 'lib/ollama_chat/clipboard.rb', line 134

def paste_from_clipboard(edit: false)
  result = perform_paste_from_clipboard(edit:)
  STDOUT.puts "The clipboard content has been successfully copied to the chat."
  result
rescue OllamaChat::OllamaChatError => e
  STDERR.puts e.message
end

#perform_copy_to_clipboard(text: nil, content: false, edit: true) ⇒ TrueClass

Copies the last assistant message to the system clipboard.

This method finds the most recent message from the assistant and writes its content to the system clipboard using the command specified in the configuration (config.copy).

Parameters:

  • text (String, nil) (defaults to: nil)

    The text to copy. If nil, the last assistant message is used (default: nil)

  • content (true, false) (defaults to: false)

    If true, copies the content of the message; if false, copies the entire message object (default: false)

  • edit (true, false) (defaults to: true)

    If true, opens the content in the editor for modification before copying (default: true)

Returns:

  • (TrueClass)

    if the copying has been performed successfully.

Raises:



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/ollama_chat/clipboard.rb', line 27

def perform_copy_to_clipboard(text: nil, content: false, edit: true)
  text ||= last_message_content(content:)
  if text
    edit and text = edit_text(text)
    copy = `which #{config.copy}`.chomp
    if copy.present?
      IO.popen(copy, 'w') do |clipboard|
        clipboard.write(text)
      end
      true
    else
      raise OllamaChat::OllamaChatError,
        "#{config.copy.inspect} command not found in system's path!"
    end
  else
    raise OllamaChat::OllamaChatError,
      "No text available to copy to the system clipboard."
  end
end

#perform_paste_from_clipboard(edit: false) ⇒ String

Performs the actual clipboard paste operation.

This method executes the configured clipboard paste command to retrieve content from the system clipboard. It uses the command specified in the configuration (config.paste) to fetch clipboard content.

Examples:

# Assuming config.paste is "pfc"
content = perform_paste_from_clipboard
# => "Some content from clipboard"

Parameters:

  • edit (true, false) (defaults to: false)

    If true, opens the retrieved content in the editor for modification before returning it (default: false)

Returns:

  • (String)

    The content retrieved from the system clipboard

Raises:



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/ollama_chat/clipboard.rb', line 63

def perform_paste_from_clipboard(edit: false)
  paste = `which #{config.paste}`.chomp
  if paste.present?
    IO.popen(paste, 'r') do |clipboard|
      text = clipboard.read
      if text.empty?
        raise OllamaChat::OllamaChatError,
          "No content available to paste from the system clipboard."
      else
        edit and text = edit_text(text)
        return text
      end
    end
  else
    raise OllamaChat::OllamaChatError,
      "#{config.paste.inspect} command not found in system's path!"
  end
end