Class: Clipcellar::Clipboard

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

Class Method Summary collapse

Class Method Details

.clipboardObject



77
78
79
# File 'lib/clipcellar/clipboard.rb', line 77

def clipboard
  Gtk::Clipboard.get(Gdk::Selection::CLIPBOARD)
end

.copy_to_clipboard(text) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/clipcellar/clipboard.rb', line 62

def copy_to_clipboard(text)
  if /darwin/ =~ RUBY_PLATFORM
    require "tempfile"
    Tempfile.open(["clipcellar", "w"]) do |file|
      text.each_line do |line|
        file.puts(line)
      end
      file.flush
      system("pbcopy < #{file.path}")
    end
  else
    clipboard.text = text
  end
end

.getObject



44
45
46
47
48
49
50
# File 'lib/clipcellar/clipboard.rb', line 44

def get
  if /darwin/ =~ RUBY_PLATFORM
    `pbpaste`
  else
    clipboard.wait_for_text
  end
end

.set(text) ⇒ Object



52
53
54
55
56
57
58
59
60
# File 'lib/clipcellar/clipboard.rb', line 52

def set(text)
  copy_to_clipboard(text)

  # workaround for CLI
  GLib::Timeout.add(1) do
    Gtk.main_quit
  end
  Gtk.main
end

.watchObject



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

def watch
  current_text = get
  GLib::Timeout.add(500) do
    text = get
    if current_text != text
      yield(text)
      current_text = text
    end
    true
  end

  begin
    Gtk.main
  rescue Interrupt => e
    $stderr.puts(e.message)
  end
end