Class: Aka::Store

Inherits:
Object
  • Object
show all
Includes:
Methadone::CLILogging
Defined in:
lib/aka/store.rb

Instance Method Summary collapse

Instance Method Details

#add(options) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/aka/store.rb', line 39

def add(options)
  found = configuration.shortcuts.find(options)

  if found.length > 0
    unless options[:force]
      abort %{Shortcut "#{options[:shortcut]}" exists. Pass --force to overwrite. Or provide a new --tag.}
    else
      found.each do |n, row|
        configuration.shortcuts.replace(n, Configuration::Shortcut.parse(options))
      end
      configuration.save
      puts "Overwrote shortcut."
    end
  else
    configuration.shortcuts.append(Configuration::Shortcut.parse(options))
    configuration.save
    puts "Created shortcut."
  end
end

#edit(options) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/aka/store.rb', line 125

def edit(options)
  result = nil

  found = configuration.shortcuts.find(options)

  index, row = found.first

  unless row
    abort "Shortcut not found."
  end

  if options[:input]
    result = File.read(options[:input])
  else
    file = Tempfile.new('shortcut')
    begin
      file.open
      if row.tag
        tags = %{ #{row.tag.join(', ')}}
      else
        tags = ''
      end
      file.write(<<-EOS.gsub(/^            /, '')
        Shortcut: #{row.shortcut}
        Description:
        #{row.description}
        Function (y/n): #{row.function ? 'y' : 'n'}
        Tags:#{tags}
        Command:
        #{row.command}
      EOS
      )
      file.close
      editor = ENV['EDITOR'] || 'vim'
      system(%[#{editor} #{file.path}])
      debug("Editing exited with code: #{$?.exitstatus}.")
      if $?.exitstatus == 0
        file.open
        file.rewind
        result = file.read.strip
      end
    ensure
      file.unlink
    end
  end

  if result
    parse_row_txt(row, result)
    configuration.shortcuts.replace(index, row)
    configuration.save
    puts "Saved shortcut."
  else
  end
end

#generate(options) ⇒ Object



99
100
101
102
103
# File 'lib/aka/store.rb', line 99

def generate(options)
  excluded = configuration.shortcuts.generate(options)

  excluded_output(excluded)
end

#help(command, options) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/aka/store.rb', line 10

def help(command, options)
  case command
  when nil       then command = "aka.7"
  else command = "aka-#{command}.1"
  end

  manpages = %w(
    aka-add.1
    aka-edit.1
    aka-generate.1
    aka-list.1
    aka-remove.1
    aka-show.1
    aka-link.1
    aka-sync.1
    aka-upgrade.1
    aka.7)

  exit unless manpages.include?(command)

  root = File.expand_path("../man", __FILE__)

  if !ENV['NO_MAN'] && Aka::App.which("man") && root !~ %r{^file:/.+!/META-INF/jruby.home/.+}
    Kernel.exec "man #{root}/#{command}"
  else
    puts File.read("#{root}/#{command}.txt")
  end
end


105
106
107
108
109
# File 'lib/aka/store.rb', line 105

def link(options)
  configuration.links.add(options)
  configuration.save
  puts "Saved link."
end

#list(options) ⇒ Object



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/aka/store.rb', line 59

def list(options)
  excluded = configuration.shortcuts.match_by_tag(options[:tag] || []) do |tag, rows|
    puts %{##{tag}}
    puts ''.ljust(tag.length + 1, '=')
    rows.each do |row|
      list_output(row)
    end
    puts
  end

  if configuration.links.any?
    puts "====="
    puts "Links"
    puts "====="
    puts

    configuration.links.each do |index, link|
      puts "[#{index}] #{link.output}: #{link.tag.map { |tag| "##{tag}" }.join(', ')}"
    end
  end

  excluded_output(excluded)
end

#remove(options) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/aka/store.rb', line 83

def remove(options)
  found = configuration.shortcuts.find(options)

  if found.length > 0
    found.each do |n, row|
      configuration.shortcuts.delete(n)
    end

    configuration.save

    puts "Removed shortcut."
  else
    abort %{No shortcut "#{options[:shortcut]}". Aborting.}
  end
end

#show(options) ⇒ Object



180
181
182
183
184
185
186
187
188
189
190
# File 'lib/aka/store.rb', line 180

def show(options)
  found = configuration.shortcuts.find(options)

  _, row = found.first

  if row
    puts show_output(row)
  else
    abort "Shortcut not found."
  end
end

#sync(match) ⇒ Object



117
118
119
120
121
122
123
# File 'lib/aka/store.rb', line 117

def sync(match)
  configuration.links.each do |index, config|
    next if match && match != index
    excluded = configuration.shortcuts.generate(config.marshal_dump)
    excluded_output(excluded)
  end
end


111
112
113
114
115
# File 'lib/aka/store.rb', line 111

def unlink(key)
  configuration.links.delete(key)
  configuration.save
  puts "Deleted link."
end

#upgrade(options) ⇒ Object



192
193
194
# File 'lib/aka/store.rb', line 192

def upgrade(options)
  configuration.upgrade
end