Class: Diakonos::ClipboardOSX

Inherits:
Object
  • Object
show all
Defined in:
lib/diakonos/clipboard-osx.rb

Overview

Same interface as Diakonos::Clipboard, except interacts with pbcopy and pbpaste on OSX

Instance Method Summary collapse

Constructor Details

#initializeClipboardOSX

Returns a new instance of ClipboardOSX.



7
8
# File 'lib/diakonos/clipboard-osx.rb', line 7

def initialize
end

Instance Method Details

#add_clip(text) ⇒ Object

Returns true iff a clip was added.

Parameters:

Returns:

  • true iff a clip was added



37
38
39
# File 'lib/diakonos/clipboard-osx.rb', line 37

def add_clip(text)
  send_to_pbcopy text
end

#append_to_clip(text) ⇒ Object

Appends the lines to the current clip. If no current clip, then a new clip is created.

Parameters:

  • lines (Array<String>)

    of text

Returns:

  • true iff the text was successfully appended



49
50
51
52
53
54
55
56
# File 'lib/diakonos/clipboard-osx.rb', line 49

def append_to_clip(text)
  return false  if text.nil?

  last_clip = clip
  last_clip.pop  if last_clip[-1] == ""

  send_to_pbcopy  last_clip + text
end

#clipObject




31
32
33
# File 'lib/diakonos/clipboard-osx.rb', line 31

def clip
  `pbpaste`.split( "\n", -1 )
end

#eachObject

no-op



42
43
# File 'lib/diakonos/clipboard-osx.rb', line 42

def each
end

#send_to_pbcopy(text) ⇒ Object

Returns true iff some text was copied to pbcopy.

Returns:

  • true iff some text was copied to pbcopy



11
12
13
14
15
16
17
18
# File 'lib/diakonos/clipboard-osx.rb', line 11

def send_to_pbcopy(text)
  return false  if text.nil?

  clip_filename = write_to_clip_file( text.join( "\n" ) )
  `pbcopy < #{clip_filename}`

  true
end

#write_to_clip_file(text) ⇒ Object

TODO: DRY this up with other Clipboard classes



21
22
23
24
25
26
27
# File 'lib/diakonos/clipboard-osx.rb', line 21

def write_to_clip_file(text)
  clip_filename = $diakonos.diakonos_home + "/clip.txt"
  File.open( clip_filename, "w" ) do |f|
    f.print text
  end
  clip_filename
end