Skip to content

Commit

Permalink
Fixed a bug highlighting multiple keywords
Browse files Browse the repository at this point in the history
  • Loading branch information
epitron committed Aug 30, 2009
1 parent eaf9098 commit c64ec02
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 14 deletions.
2 changes: 1 addition & 1 deletion delicious-cli.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

Gem::Specification.new do |s|
s.name = %q{delicious-cli}
s.version = "0.1.3"
s.version = "0.1.4"

s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["epitron"]
Expand Down
11 changes: 5 additions & 6 deletions lib/delicious-cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@

#################################################################

def search(query)
matches = Database.find(query)
matches.each { |match| display(match, query) }
def search(words)
matches = Database.find(words)
matches.each { |match| display(match, words) }
end

def sync
Expand Down Expand Up @@ -148,9 +148,8 @@ def main
elsif options.sync
sync
else
exit 1 unless query = ARGV[0]
query = ARGV[0]
search(query)
exit 1 unless ARGV.size > 0
search(ARGV)
end

end
Expand Down
10 changes: 7 additions & 3 deletions lib/delicious-cli/db.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,13 @@ def self.add(params)
@@posts << params
end

def self.find(query)
query = "%#{query}%" if query.is_a? String
@@posts.filter(:extended.like(query) | :description.like(query) | :tag.like(query)).order(:time)
def self.find(words)
sequel_query = @@posts
for word in words
pattern = "%#{word}%"
sequel_query = sequel_query.filter(:extended.like(pattern) | :description.like(pattern) | :tag.like(pattern))
end
sequel_query.order(:time)
end


Expand Down
17 changes: 13 additions & 4 deletions lib/delicious-cli/display.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,25 @@
require 'colorize'
# Colourized hilite...
class String
def hilite(query, color=:white)
query = Regexp.new( Regexp.escape(query), Regexp::IGNORECASE )
self.to_s.send(color).gsub(/(.*)(#{query})(.*)/) { $1.send(color) + $2.black.on_yellow + $3.send(color)}
def hilite(words, color=:white)
escaped_words = words.map { |word| Regexp.escape(word) }
matcher = /(#{escaped_words.join('|')})/io

chunks = self.to_s.split(matcher)
chunks.map do |chunk|
if chunk =~ matcher
chunk.black.on_yellow
else
chunk.send(color)
end
end.join('')
end
end
rescue LoadError
STDERR.puts "Note: You should install the 'colorize' gem for extra prettiness.\n"
# Monochrome hilite does nothing...
class String
def hilite(query); self; end
def hilite(words); self; end
end
end

Expand Down

0 comments on commit c64ec02

Please sign in to comment.