lib/rgversion/terminal.rb in rgversion-1.0.1 vs lib/rgversion/terminal.rb in rgversion-1.1.1

- old
+ new

@@ -1,24 +1,65 @@ module Rgversion class Terminal - def initialize(argv = []) - raise NoArguments, 'No gems passed as arguments.' if argv.length.zero? - @argv = argv + def initialize(command, results) + @command = command + @results = results end + def report + render_errors + render_output + end + def copy_to_clipboard - data = process_spider - unless data.empty? - output = data.join("\n") - `echo "#{output}" | pbcopy` - puts "#{output}\n\nCopied to your clipboard!" + return if @output.blank? + if command_exists? + `echo "#{@output}" | #{@command}` + puts "Copied to your clipboard!".green + else + render_instructions end end private - def process_spider - spider = Spider.new @argv - spider.output + def render_errors + return if @results[:errors].blank? + puts @results[:errors].join("\n").red + puts + end + + def render_output + return if @results[:gems].blank? + @output = @results[:gems].join("\n") + puts "#{@output}" + puts + 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 + + def render_instructions + if command.nil? + puts "Rgversion doesn't support copy to clipboard feature if your OS isn't macOS or Linux. You can manually copy output above." + else + puts "Unable to copy to clipboard because #{@command} is missed. Try the command below if you are on Ubuntu/Debian:" + puts "sudo apt-get install #{command}" + end end end end