Module: Clipboard::Utils

Extended by:
Utils
Included in:
Utils
Defined in:
lib/clipboard/utils.rb

Instance Method Summary collapse

Instance Method Details

#executable_installed?(cmd) ⇒ Boolean

Returns:

  • (Boolean)


9
10
11
12
13
# File 'lib/clipboard/utils.rb', line 9

def executable_installed?(cmd)
  ENV['PATH'].split(::File::PATH_SEPARATOR).any? do |path|
    ::File.executable?(::File.join(path, cmd))
  end
end

#popen(cmd, data, read_output_stream = false) ⇒ Object

Utility to call external command

  • pure .popen2 becomes messy with xsel when not reading the output stream

  • xclip doesn’t like to have output stream read



18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/clipboard/utils.rb', line 18

def popen(cmd, data, read_output_stream = false)
  Open3.popen2(cmd) { |input, output, waiter_thread|
    output_thread = Thread.new { output.read } if read_output_stream

    begin
      input.write data
    rescue Errno::EPIPE
    end

    input.close
    output_thread.value if read_output_stream
    waiter_thread.value
  }
end