Sha256: 01351f6e16fe3b2679296e37a51cfd29733f5b49e42dab62cf85d8074589c794

Contents?: true

Size: 1.05 KB

Versions: 4

Compression:

Stored size: 1.05 KB

Contents

module Rgversion
  class Clipboard
    def initialize(content, command)
      @content = content
      @command = command
    end

    def copy
      if command_exists?
        system("echo \"#{@content}\" | #{clarified_command}")
        puts "\nCopied to your clipboard!".green
      else
        instruction = Instruction.new(@command)
        instruction.render
      end
    end

    private

    def clarified_command
      return "#{@command} -selection clipboard" if OS.linux?
      @command
    end

    def command_exists?
      return false if which(@command.to_s).nil?
      true
    end

    # based on https://stackoverflow.com/a/5471032
    # let's avoid find_executable from mkmf because in this case we need to supress logs
    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
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
rgversion-1.1.8 lib/rgversion/clipboard.rb
rgversion-1.1.7 lib/rgversion/clipboard.rb
rgversion-1.1.7.rc1 lib/rgversion/clipboard.rb
rgversion-1.1.7.beta2 lib/rgversion/clipboard.rb