Skip to content

Instantly share code, notes, and snippets.

@tobstarr
Created August 31, 2012 14:40
Show Gist options
  • Save tobstarr/3553793 to your computer and use it in GitHub Desktop.
Save tobstarr/3553793 to your computer and use it in GitHub Desktop.
Bookmark System
first,link: http://www.some.url
second,link: http://www.someother.url
#!/usr/bin/env ruby
require File.expand_path("../lib/bookmark.rb", __FILE__)
Bookmark.run_with_args(ARGV)
class Bookmark
class << self
def run_with_args(args)
query = args.join(" ")
bookmarks = Bookmark.matching(query)
if bookmarks.empty?
puts "no bookmarks found for #{query.inspect}"
else
bookmarks.map(&:open)
end
end
def matching(query, bookmarks_string = default_bookmarks_string)
from_file_content(bookmarks_string).select do |bm|
bm.matches?(query)
end
end
private
def from_line(line)
tags_s, resource = line.split(": ")
new(tags_s.split(/[\s,]+/), resource)
end
def from_file_content(content)
content.split("\n").map { |line| from_line(line) }
end
def default_bookmarks_path
File.expand_path("~/.bookmarks")
end
def default_bookmarks_string
File.exists?(default_bookmarks_path) ? File.read(default_bookmarks_path) : ''
end
end
def initialize(tags, url)
@tags = tags
@url = url
end
def matches?(string)
string.strip.split(/\s+/).all? do |query_tag|
@tags.any? { |tag| tag.start_with?(query_tag) }
end
end
def open
system %(open "#{@url}")
end
private
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment