class Rgversion::Clipboard

Public Class Methods

new(content, command) click to toggle source
# File lib/rgversion/clipboard.rb, line 3
def initialize(content, command)
  @content = content
  @command = command
end

Public Instance Methods

copy() click to toggle source
# File lib/rgversion/clipboard.rb, line 8
def copy
  if command_exists?
    system("echo \"#{@content}\" | #{clarified_command}")
    puts "\nCopied to clipboard!".green
  else
    instruction = Instruction.new(@command)
    instruction.render
  end
end

Private Instance Methods

clarified_command() click to toggle source
# File lib/rgversion/clipboard.rb, line 20
def clarified_command
  return "#{@command} -selection clipboard" if OS.linux?
  @command
end
command_exists?() click to toggle source
# File lib/rgversion/clipboard.rb, line 25
def command_exists?
  return false if which(@command.to_s).nil?
  true
end
which(cmd) click to toggle source

based on stackoverflow.com/a/5471032 let's avoid find_executable from mkmf because in this case we need to supress logs

# File lib/rgversion/clipboard.rb, line 32
def which(cmd)
  exts = ENV['PATHEXT'] ? ENV['PATHEXT'].split(';') : ['']
  ENV['PATH'].split(File::PATH_SEPARATOR).each do |path|
    exts.each do |ext|
      exe = File.join(path, "#{cmd}#{ext}")
      return exe if File.executable?(exe) && !File.directory?(exe)
    end
  end
  nil
end