class Object

Constants

CONFIGDIR
HOMEDIR
SCREEN_WIDTH

Public Instance Methods

config(first_time = false) click to toggle source
# File lib/delicious-cli.rb, line 39
def config(first_time = false)
  # prompt user and stuff
  puts "Delicious login info"
  puts "---------------------------"
  puts
  print "Username: "
  username = gets.strip
  print "Password: "
  password = gets.strip
  
  puts
  puts "User: #{username.inspect} | Pass: #{password.inspect}"
  print "Is this correct? [y/N] "
  answer = gets.strip
  if answer =~ /^[Yy]$/
    puts 
    
    puts "* Checking that login/password works..."
    Delicious.basic_auth(username, password)
    if Delicious.valid_auth?    
      puts "  |_ Login successful! Saving config..."
      Settings["username"] = username
      Settings["password"] = password
      Settings.save!

      sync if first_time
      
      puts "* Everything is ready. Search away!" 
    else
      puts "  |_ Login failed! (Please check your credentials or network.)"
    end
    puts
  else
    puts "Aborting..."
  end
end
configfile(filename, check=false) click to toggle source
# File lib/delicious-cli/settings.rb, line 16
def configfile(filename, check=false)
  path = File.join(CONFIGDIR, filename)
  
  if check and not File.exists? path
    raise FileNotFound, path
  end
  
  path
end
display(post, query=nil, indent_size=11) click to toggle source
# File lib/delicious-cli/display.rb, line 94
def display(post, query=nil, indent_size=11)
  indent    = " " * indent_size
  wrap_size = SCREEN_WIDTH - indent_size

  date         = formatdate(post["time"], indent_size)
  desc_lines   = post["description"].wrap(wrap_size).map { |line| line.hilite(query, :light_white) }
  url          = post["href"].hilite(query, :light_blue)
  tag_lines    = post["tag"].wrap(wrap_size-2).map { |line| line.hilite(query, :light_cyan) }

  if post["extended"].blank?
    ext_lines = []
  else
    ext_lines = post["extended"].wrap(wrap_size).map { |line| line.hilite(query, :white) }
  end
  
  # date / description
  puts date + desc_lines.first
  if desc_lines.size > 1
    desc_lines[1..-1].each { |line| puts indent + line }
  end
  
  # extended description
  ext_lines.each do |line|
    puts indent + line
  end  
  
  # url
  puts indent + url
  
  # tags
  print indent + "(".cyan + tag_lines.first
  if tag_lines.size > 1
    tag_lines[1..-1].each do |line|
      print "\n" + indent + " " + line
    end
  end
  puts ")".cyan
  
  puts
end
formatdate(dt, width=11) click to toggle source
# File lib/delicious-cli/display.rb, line 84
def formatdate(dt, width=11)
  time = "%l:%M%P"
  date = "%d %b %g"
  #dt.strftime("#{date} #{time}")
  result = dt.strftime(date)
  
  result.ljust(width).light_yellow
end
main() click to toggle source
# File lib/delicious-cli.rb, line 79
def main

  # In it!
  
  Database.init!
  begin
    Settings.load!
  rescue Errno::ENOENT
    config(true)
    retry
  end
  
  Delicious.basic_auth(Settings["username"], Settings["password"])

  # Parse de opts
  
  ARGV.push("--help") if ARGV.empty?
  
  options = OpenStruct.new
  OptionParser.new do |opts|
    opts.banner = "Usage: delicious [options] <search query>"
    opts.separator " "
    opts.separator "Specific options:"
    
    opts.on("-c", "--config", "(Re)configure login info (set your delicious username/password)") do |opt|
      options.config = true
    end

    opts.on("-t", "--test-auth", "Test that authentication info works") do |opt|
      options.test_auth = true 
    end

    opts.on("-s", "--sync", "Synchronize links") do |opt|
      options.sync = true
    end
  
    opts.on("-r", "--redownload", "Erase database and redownload all links") do |opt|
      options.redownload = true
    end

    opts.on("-a", "--all", "Show all links.") do |opt|
      options.list = 0
    end

    opts.on("-l", "--list [num]", "List the last [num] links (default 5, 0 for all)") do |opt|
      if opt
        options.list = opt.to_i
      else
        options.list = 5
      end
    end

    opts.on("-j", "--json", "Output results as (pretty) JSON.") do |opt|
      options.json = true
    end
  
    opts.on("-d", "--debug", "Debug info") do |opt|
      options.debug = true
    end
  

    opts.separator " "
    opts.separator "Common options:"

    # No argument, shows at tail.  This will print an options summary.
    # Try it and see!
    opts.on_tail("-h", "--help", "Show this message") do
      puts opts
      puts
      exit
    end

    opts.on("-v", "--version", "Version information") do |opt|
      versionfile = File.join(File.dirname(File.expand_path(__FILE__)), "..", "VERSION")
      versionstring = open(versionfile).read
      puts "delicious-cli v#{versionstring}"
      puts
    end
    
  end.parse!
  
  
  # Handle options

  $log.level = Logger::DEBUG if options.debug
   
  if options.test_auth
    puts "Logging in as '#{Settings["username"]}': #{Delicious.valid_auth? ? "success!" : "fail!"}"
  elsif options.config 
    config
  elsif options.redownload
    redownload
  elsif options.sync
    sync
  else
    
    # get posts
    if options.list
      posts = Database.last(options.list)
    else
      words = ARGV
      exit 1 unless words.size > 0
      posts = Database.find(words)
    end
    
    
    if options.json
      require 'json'
      puts JSON.pretty_generate(posts)
    else
      if words
        posts.each { |match| display(match, words) }
      else
        posts.each { |post| display(post) }
      end
    end
    
  end
  
end
printflush(string) click to toggle source
# File lib/delicious-cli/db.rb, line 22
def printflush(string)
  print string
  STDOUT.flush
end
redownload() click to toggle source
# File lib/delicious-cli.rb, line 33
def redownload
  puts "* Redownloading database..."
  Database.clear!
  Database.sync
end
sync() click to toggle source
# File lib/delicious-cli.rb, line 28
def sync
  puts "* Synchronizing database..."
  Database.sync
end
term_width() click to toggle source
# File lib/delicious-cli/display.rb, line 36
def term_width
  if `stty size` =~ /(\d+) (\d+)/
    return $2.to_i
  else
    return 80
  end
end