Sha256: 09a0ccd748b583cf7b451fe2da52dc7181ba2cbf231064a1c6d6636926543c7a

Contents?: true

Size: 1.69 KB

Versions: 1

Compression:

Stored size: 1.69 KB

Contents

require 'yaml'
require 'trollop'

class Hop
  class << self
    def run
      opts = Trollop::options do
        opt :add, "Add URL like: apple:http://www.apple.com", :type => String
        opt :force, "Force overwriting of existing keys"
        opt :list
      end

      $bookmarks_file = File.join(ENV['HOME'], '.hop', 'bookmarks.yml')

      bookmarks = {}

      if(!File.exists?(File.dirname($bookmarks_file)))
        Dir.mkdir(File.dirname($bookmarks_file))
      end

      if(File.exist?($bookmarks_file))
        bookmarks = YAML::load_file($bookmarks_file)
      end

      if opts[:add_given]
        key, *url = opts[:add].split(':')
        if(bookmarks[key] && !opts[:force_given])
          puts "You already have a bookmark for #{key}: #{url.join(':')}"
          puts "Please add -f if you want to replace it."
        else
          File.open($bookmarks_file, 'w') do |file|
            YAML::dump(bookmarks.merge(key => url.join(':')), file)
          end
        end
      elsif opts[:list_given]
        printf("%-16s %s\n", "key", "bookmark")
        puts "-" * 70
        bookmarks.each do |key, bookmark|
          printf("%-16s %s\n", key, bookmark)
        end
      else
        urls = bookmarks.keys.select do |u|
          u.to_s.match(/.*#{ARGV[0].split(//).join('.*')}.*/)
        end

        if(ARGV[1] &&
           urls[ARGV[1].to_i])
          urls = [urls[ARGV[1].to_i]]
        end

        case urls.length
        when 0:
          puts "No URLS found matching #{ARGV.inspect}"
        when 1:
          `open #{bookmarks[urls.first]}`
        else
          urls.each_with_index do |url, index|
            puts "#{index}: #{url}"
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
hop-0.0.1 lib/hop.rb