Sha256: a487856868e96f9d5548604f837aeb11f180258aba2663fee3410fda40ca0035

Contents?: true

Size: 1.55 KB

Versions: 4

Compression:

Stored size: 1.55 KB

Contents

class Ppl::Command::Mutt < Ppl::Application::Command

  name        "mutt"
  description "Integration with mutt's query_command"

  attr_writer :format

  def initialize
    @format = Ppl::Format::AddressBook::MuttQuery.new
  end

  def options(parser, options)
    parser.banner = "usage: ppl mutt <query>"
  end

  def execute(input, output)
    query = require_query(input)
    matches = mutt_search(query)
    output.line(describe_result(matches))
    matches.count > 0
  end


  private

  def require_query(input)
    if input.arguments.first.nil?
      raise Ppl::Error::IncorrectUsage, "You must provide a query"
    end
    input.arguments.first
  end

  def mutt_search(query)
    @address_book = @storage.load_address_book
    matches = Ppl::Entity::AddressBook.new

    @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

      if matching_emails.length > 0
        matches.add_contact(contact)
      elsif !contact.name.nil? && contact.name.include?(query)
        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

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
ppl-1.8.0 lib/ppl/command/mutt.rb
ppl-1.7.0 lib/ppl/command/mutt.rb
ppl-1.6.0 lib/ppl/command/mutt.rb
ppl-1.5.3 lib/ppl/command/mutt.rb