class Alfred::Core

Attributes

cached_feedback_reload_option[RW]
handler_controller[R]
query[R]
raw_query[R]
with_help_feedback[RW]
with_rescue_feedback[RW]

Public Class Methods

new(&blk) click to toggle source
# File lib/alfred.rb, line 152
def initialize(&blk)
  @with_rescue_feedback = true
  @with_help_feedback = false
  @cached_feedback_reload_option = {
    :use_reload_option => false,
    :use_exclamation_mark => false
  }

  @query = ARGV
  @raw_query = ARGV.dup

  @handler_controller = ::Alfred::Handler::Controller.new

  instance_eval(&blk) if block_given?

  raise NoBundleIDError unless bundle_id
end

Public Instance Methods

bundle_id() click to toggle source

Returns nil if not set.

# File lib/alfred.rb, line 361
def bundle_id
  @bundle_id ||= info_plist['bundleid'] unless info_plist['bundleid'].empty?
end
cached_feedback?() click to toggle source
# File lib/alfred.rb, line 383
def cached_feedback?
  @cached_feedback_reload_option.values.any?
end
close() click to toggle source
# File lib/alfred.rb, line 251
def close
  @feedback.close if @feedback
  @setting.close if @setting
  # @workflow_setting.close if @workflow_setting
end
debug?() click to toggle source
# File lib/alfred.rb, line 171
def debug?
  ui.level >= LogUI::WARN
end
feedback(opts = {}, &blk) click to toggle source
# File lib/alfred.rb, line 350
def feedback(opts = {}, &blk)
  @feedback ||= new_feedback(opts, &blk)
end
Also aliased as: with_cached_feedback
info_plist() click to toggle source
# File lib/alfred.rb, line 356
def info_plist
  @info_plist ||= Plist::parse_xml('info.plist')
end
last_option() click to toggle source

Parse and return user query to three parts

[ [before], last option, tail ]
# File lib/alfred.rb, line 288
def last_option
  (@raw_query.size - 1).downto(0) do |i|
    if @raw_query[i].start_with? '-'
      if @raw_query[i] == @raw_query[-1]
        return @raw_query[0...i], '', @raw_query[i]
      else
        return @raw_query[0..i], @raw_query[i], @raw_query[(i + 1)..-1].join(' ')
      end
    end
  end

  return [], '', @raw_query.join(' ')
end
new_feedback(opts, &blk) click to toggle source
# File lib/alfred.rb, line 416
def new_feedback(opts, &blk)
  ::Alfred::Feedback.new(self, opts, &blk)
end
new_setting(opts) click to toggle source
# File lib/alfred.rb, line 421
def new_setting(opts)
  default_opts = {
    :file    => File.join(Alfred.workflow_folder, "setting.yaml"),
    :format  => 'yaml',
  }
  opts = default_opts.update(opts)

  ::Alfred::Setting.new(self) do
    @backend_file = opts[:file]
    @formt = opts[:format]
  end
end
on_help() click to toggle source
# File lib/alfred.rb, line 411
def on_help
  reload_help_item
end
options(opts = {}) click to toggle source
# File lib/alfred.rb, line 304
def options(opts = {})
  @options ||= OpenStruct.new(opts)
end
query_parser() click to toggle source
# File lib/alfred.rb, line 308
def query_parser
  @query_parser ||= init_query_parser
end
rescue_feedback(opts = {}) click to toggle source
# File lib/alfred.rb, line 388
def rescue_feedback(opts = {})
  default_opts = {
    :title        => "Failed Query!"                                  ,
    :subtitle     => "Check log #{ui.log_file} for extra debug info." ,
    :uid          => 'Rescue Feedback'                                ,
    :valid        => 'no'                                             ,
    :autocomplete => ''                                               ,
    :icon         => Feedback.CoreServicesIcon('AlertStopIcon')
  }
  if @with_help_feedback
   default_opts[:autocomplete] = '-h'
  end
  opts = default_opts.update(opts)

  items = []
  items << Feedback::Item.new(opts[:title], opts)
  log_item = Feedback::FileItem.new(ui.log_file)
  log_item.uid = nil
  items << log_item

  feedback.to_alfred('', items)
end
setting(&blk)
Alias for: user_setting
start_handler() click to toggle source

Main loop to work with handlers

# File lib/alfred.rb, line 178
def start_handler

  if @with_help_feedback
    ::Alfred::Handler::Help.new(self, :with_handler_help => true).register
  end

  return if @handler_controller.empty?

  # step 1: register option parser for handlers
  @handler_controller.each do |handler|
    handler.on_parser
  end

  begin
    query_parser.parse!
  rescue OptionParser::InvalidOption, OptionParser::MissingArgument => e
    ui.warn(
      "Fail to parse user query.\n" \
      "  #{e.inspect}\n  #{e.backtrace.join("  \n")}\n") if debug?
  end

  if @cached_feedback_reload_option[:use_exclamation_mark] && !options.should_reload_cached_feedback
    if ARGV[0].eql?('!')
      ARGV.shift
      options.should_reload_cached_feedback = true
    elsif ARGV[-1].eql?('!')
      ARGV.delete_at(-1)
      options.should_reload_cached_feedback = true
    end
  end

  @query = ARGV

  # step 2: dispatch options to handler for FEEDBACK or ACTION
  case options.workflow_mode
  when :feedback
    @handler_controller.each_handler do |handler|
      handler.on_feedback
    end

    puts feedback.to_alfred(@query)
  when :action
    arg = @query
    if @query.length == 1
      if hsh = xml_parser(@query[0])
        arg = hsh
      end
    end

    if arg.is_a?(Hash)
      @handler_controller.each_handler do |handler|
        handler.on_action(arg)
      end
    else
      #fallback default action
      arg.each do |a|
        if File.exist? a
          %x{open "#{a}"}
        end
      end
    end
  else
    raise InvalidArgument, "#{options.workflow_mode} mode is not supported."
  end

  # step 3: close
  close
  @handler_controller.each_handler do |handler|
    handler.on_close
  end

end
storage_path() click to toggle source

Non-volatile storage directory for this bundle

# File lib/alfred.rb, line 374
def storage_path
  path = "#{ENV['HOME']}/Library/Application Support/Alfred 2/Workflow Data/#{bundle_id}"
  unless File.exist?(path)
    FileUtils.mkdir_p(path)
  end
  path
end
ui() click to toggle source
# File lib/alfred.rb, line 327
def ui
  @ui ||= LogUI.new(bundle_id)
end
user_query() click to toggle source

User query without reload options

# File lib/alfred.rb, line 261
def user_query
  q = @raw_query.dup

  if cached_feedback?
    if @cached_feedback_reload_option[:use_exclamation_mark] 
      if q[0].eql?('!')
        q.shift
      elsif q[-1].eql?('!')
        q.delete_at(-1)
      end
    end

    if @cached_feedback_reload_option[:use_reload_option]
      q.delete_if do |v|
        ['-r', '--reload'].include? v
      end
    end
  end

  q
end
user_setting(&blk) click to toggle source

user setting is stored in the storage_path by default

# File lib/alfred.rb, line 342
def user_setting(&blk)
  @setting ||= new_setting(
    :file => File.join(storage_path, "setting.yaml")
  )
end
Also aliased as: setting
volatile_storage_path() click to toggle source
# File lib/alfred.rb, line 365
def volatile_storage_path
  path = "#{ENV['HOME']}/Library/Caches/com.runningwithcrayons.Alfred-2/Workflow Data/#{bundle_id}"
  unless File.directory?(path)
    FileUtils.mkdir_p(path)
  end
  path
end
with_cached_feedback(opts = {}, &blk)
Alias for: feedback
workflow_setting(opts = {}) click to toggle source

workflow setting is stored in the workflow_folder

# File lib/alfred.rb, line 335
def workflow_setting(opts = {})
  @workflow_setting ||= new_setting(opts)
end
xml_builder(arg) click to toggle source
# File lib/alfred.rb, line 323
def xml_builder(arg)
  Gyoku.xml(:root => arg)
end
xml_parser(xml) click to toggle source
# File lib/alfred.rb, line 312
def xml_parser(xml)
  @xml_parser ||= Nori.new(:parser => :rexml,
                           :convert_tags_to => lambda { |tag| tag.to_sym })
  begin
    hsh = @xml_parser.parse(xml)
    return hsh[:root]
  rescue REXML::ParseException, Nokogiri::XML::SyntaxError
    return nil
  end
end

Private Instance Methods

init_query_parser() click to toggle source
# File lib/alfred.rb, line 458
def init_query_parser
  options.workflow_mode = :feedback
  options.modifier = :none
  options.should_reload_cached_feedback = false

  modifiers = [:command, :alt, :control, :shift, :fn, :none]
  OptionParser.new do |opts|
    opts.separator ""
    opts.separator "Built-in Options:"

    opts.on("--workflow-mode [TYPE]", [:feedback, :action],
            "Alfred handler working mode (feedback, action)") do |t|
      options.workflow_mode = t
    end

    opts.on("--modifier [MODIFIER]", modifiers,
            "Alfred action modifier (#{modifiers})") do |t|
      options.modifier = t
    end

    if @cached_feedback_reload_option[:use_reload_option]
      opts.on("-r", "--reload", "Reload cached feedback") do
        options.should_reload_cached_feedback = true
      end
    end
    opts.separator ""
    opts.separator "Handler Options:"
  end

end
reload_help_item() click to toggle source
# File lib/alfred.rb, line 436
def reload_help_item
  title = []
  if @cached_feedback_reload_option[:use_exclamation_mark]
    title.push "!"
  end

  if @cached_feedback_reload_option[:use_reload_option]
    title.push "-r, --reload"
  end

  unless title.empty?
    return {
      :kind  => 'text',
      :order => (Handler::HelpItem::Base_Order * 10),
      :title => "#{title.join(', ')} [Reload cached feedback unconditionally]" ,
      :subtitle => %q{The '!' mark must be at the beginning or end of the query.} ,
    }
  else
    return nil
  end
end