Module: VER::Clipboard

Defined in:
lib/ver/clipboard.rb

Overview

Tries to provide a simple interface to the X clipboard. In particular we worry about exceptions and encodings.

Please note that you should not use this module if you want to set or get binary contents from the clipboard, you’ll have to use Tk::Clipboard directly or put up with a base64 and marshal overhead.

Class Method Summary collapse

Class Method Details

.ascii=(string) ⇒ Object

Note that this will encode string without making a copy, so only pass US-ASCII strings or make a copy if you mind the encoding change.

The benefit of using this method is that it should work with older applications too, as they might not know about UTF8_STRING.



95
96
97
# File 'lib/ver/clipboard.rb', line 95

def ascii=(string)
  set(string.encode!('US-ASCII'), 'STRING')
end

.clearObject



11
12
13
# File 'lib/ver/clipboard.rb', line 11

def clear
  Tk::Clipboard.clear(VER.root)
end

.dwimObject



19
20
21
# File 'lib/ver/clipboard.rb', line 19

def dwim
  string || marshal
end

.dwim=(object) ⇒ Object

dwim stands for “do what i mean”, and for most of your clipboard needs this should just do what you want.



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/ver/clipboard.rb', line 53

def dwim=(object)
  if object.respond_to?(:to_str)
    self.string = object.to_str
  elsif object.respond_to?(:to_ary)
    array = object.to_ary
    self.string = array.join("\n")
    self.marshal = object
  else
    self.marshal = object
  end
end

.get(type) ⇒ Object



15
16
17
# File 'lib/ver/clipboard.rb', line 15

def get(type)
  Tk::Clipboard.get(VER.root, type)
end

.marshalObject



39
40
41
42
43
44
45
# File 'lib/ver/clipboard.rb', line 39

def marshal
  got = get('RUBY_MARSHAL')
rescue
  nil
else
  Marshal.load(got.unpack('m0').first)
end

.marshal=(object) ⇒ Object

This will put any object capable of being marshalled into the clipboard. We use RUBY_MARSHAL as type, but it’s also base64-encoded as specified in RFC 4648 to keep it ASCII.



102
103
104
105
# File 'lib/ver/clipboard.rb', line 102

def marshal=(object)
  marshal = [Marshal.dump(object)].pack('m0')
  set(marshal, 'RUBY_MARSHAL')
end

.set(string, type) ⇒ Object



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

def set(string, type)
  Tk::Clipboard.set(string, type: type)
end

.stringObject



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

def string
  got = Tk.execute('::tk::GetSelection', VER.root, 'CLIPBOARD').to_s
rescue => ex
  l ex
  nil
else
  if got.ascii_only?
    got.encode!('ASCII')
  else
    got.force_encoding('UTF-8')
    got.encode!(Encoding.default_external)
  end

  got
end

.string=(string) ⇒ Object



65
66
67
68
69
70
71
72
73
74
# File 'lib/ver/clipboard.rb', line 65

def string=(string)
  case string.encoding
  when Encoding::ASCII
    self.ascii = string
  when Encoding::UTF_8
    self.utf8 = string
  else
    self.utf8 = string.encode('UTF-8')
  end
end

.utf8=(string) ⇒ Object

Note that this will encode string without making a copy, so only pass UTF-8 strings or make a copy if you mind the encoding change.

Use this method on *nix if possible, most modern applications query for this type first.



81
82
83
84
85
86
87
88
# File 'lib/ver/clipboard.rb', line 81

def utf8=(string)
  case Tk::TkCmd.windowingsystem
  when :x11
    set(string.encode!('UTF-8'), 'UTF8_STRING')
  else
    set(string.encode!('UTF-8'), 'STRING')
  end
end