Class: Topicz::Commands::AlfredCommand

Inherits:
RepositoryCommand show all
Defined in:
lib/topicz/commands/alfred_command.rb

Instance Method Summary collapse

Methods inherited from RepositoryCommand

#find_exactly_one_topic, #load_config, #load_repository, #process_excludes

Constructor Details

#initialize(config_file = nil, arguments = []) ⇒ AlfredCommand

Returns a new instance of AlfredCommand.



8
9
10
11
12
13
14
# File 'lib/topicz/commands/alfred_command.rb', line 8

def initialize(config_file = nil, arguments = [])
  super(config_file)
  @identifiers = false
  @mode = :browse
  option_parser.order! arguments
  @filter = arguments.join ' '
end

Instance Method Details

#create_subtitle(topic) ⇒ Object



83
84
85
86
87
88
89
90
91
92
# File 'lib/topicz/commands/alfred_command.rb', line 83

def create_subtitle(topic)
  case @mode
    when :browse then
      "Browse topic '#{topic.title}' in Alfred Browser"
    when :journal then
      "Open this weeks journal entry for topic '#{topic.title}'"
    when :note then
      "Create a new note for topic '#{topic.title}'"
  end
end

#executeObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/topicz/commands/alfred_command.rb', line 52

def execute
  topics = @repository.find_all @filter
  items = []
  topics.each do |topic|
    item = {
        # https://www.alfredapp.com/help/workflows/inputs/script-filter/json/
        uid: topic.id, # Affects the ordering in Alfred, based on learning
        type: 'file',
        title: topic.title,
        subtitle: create_subtitle(topic),
        arg: @identifiers ? topic.id : topic.fullpath,
        icon: {
            type: 'fileicon',
            path: topic.fullpath
        }
    }
    if @mode == :browse
      item['mods'] = {
          cmd: {
              subtitle: "Open topic '#{topic.title}' in Finder"
          },
          alt: {
              subtitle: "Open topic '#{topic.title}' in editor"
          }
      }
    end
    items << item
  end
  puts ({items: items}.to_json)
end

#option_parserObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/topicz/commands/alfred_command.rb', line 16

def option_parser
  OptionParser.new do |options|
    options.banner = 'Usage: alfred <filter>'
    options.on('-m', '--mode MODE', 'Set the output mode') do |mode|
      case mode.strip.downcase
        when 'browse' then
          @mode = :browse
          @identifiers = false
        when 'journal' then
          @mode = :journal
          @identifiers = true
        when 'note' then
          @mode = :note
          @identifiers = true
        else
          raise "Invalid mode: '#{mode}'"
      end
    end
    options.separator ''
    options.separator 'Searches for topics and produces the result in JSON for Alfred\'s Script Filter'
    options.separator ''
    options.separator 'The filter specifies the text to search on. The text is matched against the topic\'s: '
    options.separator '- path on the filesystem'
    options.separator '- id, if specified in the topic\'s topic.yaml file'
    options.separator '- title, if specified in the topic\'s topic.yaml file'
    options.separator '- aliases, if specified in the topic\'s topic.yaml file'
    options.separator ''
    options.separator 'Supported modes are:'
    options.separator '  browse  : For browsing through topics'
    options.separator '  journal : For creating journal entries'
    options.separator '  note    : For creating notes'
    options.separator ''
    options.separator 'Alfred automatically orders the items in its UI based on your usage.'
  end
end