lib/ppl/command/mutt.rb in ppl-1.5.2 vs lib/ppl/command/mutt.rb in ppl-1.5.3

- old
+ new

@@ -1,59 +1,43 @@ class Ppl::Command::Mutt < Ppl::Application::Command + name "mutt" + description "Integration with mutt's query_command" + attr_writer :format def initialize - @name = "mutt" - @description = "Integration with mutt's query_command" - @format = Ppl::Format::AddressBook::MuttQuery.new end def options(parser, options) parser.banner = "usage: ppl mutt <query>" end def execute(input, output) - query = input.arguments.shift - if query.nil? - raise Ppl::Error::IncorrectUsage, "You must provide a query" - end + query = require_query(input) + matches = mutt_search(query) + output.line(describe_result(matches)) + matches.count > 0 + end - address_book = @storage.load_address_book - matches = mutt_search(address_book, query) + private - if matches.count > 0 - - line = sprintf( - "Searching database... %d entries... %d matching:", - address_book.count, - matches.count - ) - - results = @format.process(matches) - - output.line(line) - output.line(results) unless results == "" - true - - else - output.line("No matches") - false + def require_query(input) + if input.arguments.first.nil? + raise Ppl::Error::IncorrectUsage, "You must provide a query" end - + input.arguments.first end - - private - - def mutt_search(address_book, query) + def mutt_search(query) + @address_book = @storage.load_address_book matches = Ppl::Entity::AddressBook.new - address_book.each do |contact| + @address_book.each do |contact| next if contact.email_addresses.empty? matching_emails = contact.email_addresses.select do |email_address| email_address.include? query end @@ -64,9 +48,27 @@ matches.add_contact(contact) end end matches + end + + def describe_result(matches) + if matches.count > 0 + describe_matches(matches) + else + "No matches" + end + end + + def describe_matches(matches) + summary = sprintf( + "Searching address book... %d entries... %d matching:", + @address_book.count, + matches.count + ) + results = @format.process(matches) + [summary, results].join("\n").strip end end