#!/usr/bin/env ruby require 'optparse' require 'pathname' # How many methods / constants to return. THRESHOLD = 5 MAC = !!/darwin/.match(RUBY_PLATFORM) WINDOWS = !!/win/.match(RUBY_PLATFORM) if not MAC def clear_database file = Pathname.new(ENV["HOME"]) + ".lookup/lookup.sqlite3" FileUtils.rm(file) if File.exists?(file) end def display_url(result, options={}) # if we're not on mac or windows we default to text output if OPTIONS[:text] or not (MAC || WINDOWS) s = options[:number] ? options[:number].to_s + ". " : "" # if we're a method then show the constant in parans s += "(#{result.constant.name}) " if result.respond_to?(:constant) s += "#{result.name} #{result.url}" puts s elsif MAC `open #{result.url}` elsif WINDOWS `start #{result.url}` end end def display_results(results, search_string) if results.empty? puts "There were no results matching #{search_string}." # if entry # puts "There are no constants that match #{name} and contain #{entry}." # else # puts "There are no constants that match #{name}" # end elsif results.size == 1 display_url(results.first) elsif results.size <= THRESHOLD results.each_with_index do |result, i| display_url(result, :number => i+1) end else puts "Please refine your query, we found #{results.size} results (threshold is #{THRESHOLD})." end end if (local_lib = Pathname.new(File.expand_path('../../lib/lookup.rb', __FILE__))).file? require local_lib.to_s else require 'rubygems' require 'lookup' end OPTIONS = {} output = nil op=OptionParser.new do |opts| opts.banner = %Q{ Usage: lookup [method] [OPTIONS] Example: lookup v3.0.0 ActiveRecord::Base new } opts.on("-c", "--clear", "Clear database") do OPTIONS[:clear] = true end opts.on("-t", "--text", "Text only") do OPTIONS[:text] = true end opts.on("-v", "--version", "Version of lookup") do output = Lookup::VERSION end end op.parse! clear_database if OPTIONS[:clear] unless output begin search_string=ARGV[0..-1].join(" ") if search_string.strip.empty? puts op.help else results=Lookup.search(search_string) display_results(results, search_string) end rescue Lookup::APINotFound => e puts e.message end else puts output end