Sha256: 6448824ed9901c470a3471d8a7625310ca8801e29a94888fafe63d78e0a762a2

Contents?: true

Size: 2 KB

Versions: 5

Compression:

Stored size: 2 KB

Contents

#!/usr/bin/ruby
require 'optparse'

# 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 = File.join(File.dirname(__FILE__), "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

OPTIONS = {}
op=OptionParser.new do |opts|
  opts.banner = "Usage: lookup <constant|method> [method] [OPTIONS]"

  opts.on("-c", "--clear", "Clear database") do
    OPTIONS[:clear] = true
  end
  opts.on("-t", "--text", "Text only") do
    OPTIONS[:text] = true
  end
end
op.parse!

clear_database if OPTIONS[:clear]


local_lib=File.join(File.dirname(__FILE__), "../lib/lookup")
if File.exists?(local_lib + ".rb")
  require local_lib
else
  require 'rubygems'
  require 'lookup' 
end

search_string=ARGV[0..-1].join(" ")
if search_string.strip.empty?
  puts op.help
  puts
else
  results=APILookup.search(ARGV[0..-1])
  display_results(results, search_string)
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
lookup-0.3.3 bin/lookup
lookup-0.3.2 bin/lookup
lookup-0.3.1 bin/lookup
lookup-0.3.0 bin/lookup
lookup-0.2.2 bin/lookup