Class: Alfred::Handler::Autocomplete

Inherits:
Base
  • Object
show all
Defined in:
lib/alfred/handler/autocomplete.rb

Constant Summary

Constants inherited from Base

Base::Base_Invoke_Order

Instance Attribute Summary

Attributes inherited from Base

#order, #status

Instance Method Summary collapse

Methods inherited from Base

#<=>, #action?, #feedback, #feedback?, #on_action, #on_close, #on_help, #on_parser, #options, #parser, #query, #register, #status_message, #ui, #xml_builder

Constructor Details

#initialize(alfred, opts = {}) ⇒ Autocomplete

Returns a new instance of Autocomplete.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/alfred/handler/autocomplete.rb', line 8

def initialize(alfred, opts = {})
  super
  @settings = {
    :handler        => 'Autocomplete' ,
    :items          => {}             ,
    :fuzzy_score    => 0.5            ,
  }.update(opts)

  if @settings[:items].empty?
    @load_from_workflow_setting = true
  else
    @load_from_workflow_setting = false
  end
end

Instance Method Details

#add_fuzzy_match_feedback(items, before, query, base_item, to_feedback) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/alfred/handler/autocomplete.rb', line 51

def add_fuzzy_match_feedback(items, before, query, base_item, to_feedback)
  matcher = FuzzyMatch.new(items, :read => :complete)
  matcher.find_all_with_score(query).each do |item, dice_similar, leven_similar|
    next if item[:complete].size < query.size

    if (item[:complete].start_with?(query) or
        dice_similar > @settings[:fuzzy_score] or
        leven_similar > @settings[:fuzzy_score])

      base_item[:autocomplete] = "#{(before + [item[:complete]]).join(' ')} "
      to_feedback.add_item(base_item.update(item))
    end
  end
end

#on_feedbackObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/alfred/handler/autocomplete.rb', line 25

def on_feedback
  if @load_from_workflow_setting
    @settings[:items].merge! @core.workflow_setting[:autocomplete]
  end

  before, option, tail = @core.last_option

  base_item ={
    :match?   => :always_match?      ,
    :subtitle => "↩ to autocomplete" ,
    :valid    => 'no'                ,
    :icon     => ::Alfred::Feedback.CoreServicesIcon('ForwardArrowIcon') ,
  }

  if @settings[:items].has_key? tail
    unify_items(@settings[:items][tail]).each do |item|
      base_item[:autocomplete] = "#{(before + [tail, item[:complete]]).join(' ')} "
      feedback.add_item(base_item.update(item))
    end
  else
    add_fuzzy_match_feedback(unify_items(@settings[:items][option]),
                             before, tail, base_item, feedback)
  end
end

#unify_items(items) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/alfred/handler/autocomplete.rb', line 66

def unify_items(items)
  return [] unless items
  items.map do |item|
    if item.is_a? String
      {:title => item, :complete => item}
    elsif item.is_a? Hash
      unless item.has_key? :complete
        item[:complete] = item[:title]
      end
      item
    else
      raise InvalidArgument, "autocomplete handler can only accept string or hash"
    end
  end
end