Class: RageFlip::Clipboard

Inherits:
Object
  • Object
show all
Defined in:
lib/rage_flip/clipboard.rb

Class Method Summary collapse

Class Method Details

.copy(text) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/rage_flip/clipboard.rb', line 3

def self.copy(text)
  case RUBY_PLATFORM
  when /darwin/
    system("echo #{text.shellescape} | pbcopy")
  when /linux/
    if system("which xclip > /dev/null 2>&1")
      system("echo #{text.shellescape} | xclip -selection clipboard")
    elsif system("which xsel > /dev/null 2>&1")
      system("echo #{text.shellescape} | xsel --clipboard --input")
    else
      puts "Error: No clipboard utility found. Please install xclip or xsel."
      return false
    end
  when /mswin|mingw|cygwin/
    # Windows requires UTF-16LE encoding for clip command to handle Unicode properly
    require 'tempfile'
    
    Tempfile.create(['clipboard', '.txt'], binmode: true) do |temp_file|
      # Convert text to UTF-16LE with BOM for Windows clipboard
      utf16_text = "\uFEFF#{text}".encode('UTF-16LE')
      temp_file.write(utf16_text)
      temp_file.flush
      system("clip < \"#{temp_file.path}\"")
    end
  else
    puts "Error: Unsupported platform for clipboard operations."
    return false
  end
  true
end