hsume2-aka 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,121 @@
1
+ module Aka
2
+ class Shortcuts
3
+ def initialize(shortcuts)
4
+ @shortcuts = shortcuts.dup
5
+ end
6
+
7
+ def append(shortcut)
8
+ @shortcuts[count + 1] = shortcut
9
+ end
10
+
11
+ def replace(index, shortcut)
12
+ @shortcuts[index] = shortcut
13
+ end
14
+
15
+ def delete(index)
16
+ @shortcuts.delete(index)
17
+ end
18
+
19
+ def count
20
+ result, _ = @shortcuts.max { |(n, _)| n }
21
+ result || 0
22
+ end
23
+
24
+ def all
25
+ @shortcuts.dup
26
+ end
27
+
28
+ def find(options)
29
+ if options[:tag]
30
+ @shortcuts.select do |_, row|
31
+ next unless row.shortcut == options[:shortcut]
32
+
33
+ options[:tag].find do |tag|
34
+ row.tag && row.tag.include?(tag)
35
+ end
36
+ end
37
+ else
38
+ @shortcuts.select do |_, row|
39
+ row.shortcut == options[:shortcut]
40
+ end
41
+ end
42
+ end
43
+
44
+ def generate(options)
45
+ scripts = []
46
+ functions = []
47
+
48
+ excluded = match_by_tag(options[:tag] || []) do |tag, rows|
49
+ rows.each do |row|
50
+ unless row.function
51
+ scripts << Configuration::Shortcut.generate_output(row)
52
+ else
53
+ functions << Configuration::Shortcut.generate_output(row)
54
+ end
55
+ end
56
+ end
57
+
58
+ if options[:output]
59
+ File.open(options[:output], 'w+') do |f|
60
+ scripts.each do |script|
61
+ f.puts script
62
+ end
63
+
64
+ functions.each do |function|
65
+ f.puts function
66
+ end
67
+ end
68
+
69
+ puts "Generated #{options[:output]}."
70
+ else
71
+ scripts.each do |script|
72
+ puts script
73
+ end
74
+
75
+ functions.each do |function|
76
+ puts function
77
+ end
78
+ end
79
+
80
+ excluded
81
+ end
82
+
83
+ def excluded_output(excluded)
84
+ return if excluded[:tags].empty?
85
+
86
+ tags = excluded[:tags].map { |t| '#' + t }.join(', ')
87
+ $stderr.puts "#{excluded[:shortcuts]} shortcut(s) excluded (#{tags})."
88
+ end
89
+
90
+ def match_by_tag(tags, &blk)
91
+ excluded = { :tags => [], :shortcuts => 0 }
92
+
93
+ by_tag.each do |tag, rows|
94
+ next if rows.empty?
95
+ unless tag == :default || tags.empty? || tags.include?(tag)
96
+ excluded[:tags] << tag
97
+ excluded[:shortcuts] += rows.length
98
+ next
99
+ end
100
+
101
+ yield(tag, rows)
102
+ end
103
+
104
+ excluded
105
+ end
106
+
107
+ def by_tag
108
+ @shortcuts.inject({ :default => [] }) do |acc, (_, row)|
109
+ if row.tag
110
+ row.tag.each do |tag|
111
+ acc[tag] ||= []
112
+ acc[tag] << row
113
+ end
114
+ else
115
+ acc[:default] << row
116
+ end
117
+ acc
118
+ end
119
+ end
120
+ end
121
+ end
data/lib/aka/store.rb CHANGED
@@ -20,6 +20,9 @@ module Aka
20
20
  aka-list.1
21
21
  aka-remove.1
22
22
  aka-show.1
23
+ aka-link.1
24
+ aka-sync.1
25
+ aka-upgrade.1
23
26
  aka.7)
24
27
 
25
28
  exit unless manpages.include?(command)
@@ -35,27 +38,27 @@ module Aka
35
38
  end
36
39
 
37
40
  def add(options)
38
- found = find(options)
41
+ found = configuration.shortcuts.find(options)
39
42
 
40
43
  if found.length > 0
41
44
  unless options[:force]
42
45
  abort %{Shortcut "#{options[:shortcut]}" exists. Pass --force to overwrite. Or provide a new --tag.}
43
46
  else
44
47
  found.each do |n, row|
45
- replace(n, options)
48
+ configuration.shortcuts.replace(n, Configuration::Shortcut.parse(options))
46
49
  end
47
- save
50
+ configuration.save
48
51
  puts "Overwrote shortcut."
49
52
  end
50
53
  else
51
- append(options)
52
- save
54
+ configuration.shortcuts.append(Configuration::Shortcut.parse(options))
55
+ configuration.save
53
56
  puts "Created shortcut."
54
57
  end
55
58
  end
56
59
 
57
60
  def list(options)
58
- excluded = match_shortcuts_by_tag(options[:tag] || []) do |tag, rows|
61
+ excluded = configuration.shortcuts.match_by_tag(options[:tag] || []) do |tag, rows|
59
62
  puts %{##{tag}}
60
63
  puts ''.ljust(tag.length + 1, '=')
61
64
  rows.each do |row|
@@ -64,18 +67,29 @@ module Aka
64
67
  puts
65
68
  end
66
69
 
70
+ if configuration.links.any?
71
+ puts "====="
72
+ puts "Links"
73
+ puts "====="
74
+ puts
75
+
76
+ configuration.links.each do |link|
77
+ puts "#{link.output}: #{link.tag.map { |tag| "##{tag}" }.join(', ')}"
78
+ end
79
+ end
80
+
67
81
  excluded_output(excluded)
68
82
  end
69
83
 
70
84
  def remove(options)
71
- found = find(options)
85
+ found = configuration.shortcuts.find(options)
72
86
 
73
87
  if found.length > 0
74
88
  found.each do |n, row|
75
- delete(n)
89
+ configuration.shortcuts.delete(n)
76
90
  end
77
91
 
78
- save
92
+ configuration.save
79
93
 
80
94
  puts "Removed shortcut."
81
95
  else
@@ -83,59 +97,35 @@ module Aka
83
97
  end
84
98
  end
85
99
 
86
- def matches_tag?(row, tag)
87
- return true unless row.tag
88
-
89
- if tag =~ /^~(.+)/
90
- !row.tag.include?($1)
91
- else
92
- row.tag && row.tag.include?(tag)
93
- end
94
- end
95
-
96
100
  def generate(options)
97
- scripts = []
98
- functions = []
101
+ excluded = configuration.shortcuts.generate(options)
99
102
 
100
- excluded = match_shortcuts_by_tag(options[:tag] || []) do |tag, rows|
101
- rows.each do |row|
102
- unless row.function
103
- scripts << generate_output(row)
104
- else
105
- functions << generate_output(row)
106
- end
107
- end
108
- end
109
-
110
- if options[:output]
111
- File.open(options[:output], 'w+') do |f|
112
- scripts.each do |script|
113
- f.puts script
114
- end
115
-
116
- functions.each do |function|
117
- f.puts function
118
- end
119
- end
103
+ excluded_output(excluded)
104
+ end
120
105
 
121
- puts "Generated #{options[:output]}."
106
+ def link(options)
107
+ unless options[:delete]
108
+ configuration.links.add(options)
109
+ configuration.save
110
+ puts "Saved link."
122
111
  else
123
- scripts.each do |script|
124
- puts script
125
- end
126
-
127
- functions.each do |function|
128
- puts function
129
- end
112
+ configuration.links.delete(options)
113
+ configuration.save
114
+ puts "Deleted link."
130
115
  end
116
+ end
131
117
 
132
- excluded_output(excluded)
118
+ def sync
119
+ configuration.links.each do |config|
120
+ excluded = configuration.shortcuts.generate(config)
121
+ excluded_output(excluded)
122
+ end
133
123
  end
134
124
 
135
125
  def edit(options)
136
126
  result = nil
137
127
 
138
- found = find(options)
128
+ found = configuration.shortcuts.find(options)
139
129
 
140
130
  index, row = found.first
141
131
 
@@ -180,15 +170,15 @@ module Aka
180
170
 
181
171
  if result
182
172
  parse_row_txt(row, result)
183
- shortcuts[index] = row
184
- save
173
+ configuration.shortcuts.replace(index, row)
174
+ configuration.save
185
175
  puts "Saved shortcut."
186
176
  else
187
177
  end
188
178
  end
189
179
 
190
180
  def show(options)
191
- found = find(options)
181
+ found = configuration.shortcuts.find(options)
192
182
 
193
183
  _, row = found.first
194
184
 
@@ -199,100 +189,18 @@ module Aka
199
189
  end
200
190
  end
201
191
 
202
- private
203
-
204
- def shortcuts
205
- @shortcuts ||= begin
206
- if File.exist?(aka_yml)
207
- YAML::load_file(aka_yml)
208
- else
209
- {}
210
- end
211
- end
192
+ def upgrade(options)
193
+ configuration.upgrade
212
194
  end
213
195
 
214
- def find(options)
215
- if options[:tag]
216
- shortcuts.select do |_, row|
217
- next unless row.shortcut == options[:shortcut]
218
-
219
- options[:tag].find do |tag|
220
- row.tag && row.tag.include?(tag)
221
- end
222
- end
223
- else
224
- shortcuts.select do |_, row|
225
- row.shortcut == options[:shortcut]
226
- end
227
- end
228
- end
229
-
230
- def append(options)
231
- shortcuts[count + 1] = row(options)
232
- end
233
-
234
- def replace(index, options)
235
- shortcuts[index] = row(options)
236
- end
237
-
238
- def delete(index)
239
- shortcuts.delete(index)
240
- end
241
-
242
- def count
243
- result, _ = shortcuts.max { |(n, _)| n }
244
- result || 0
245
- end
246
-
247
- def row(options)
248
- OpenStruct.new.tap do |row|
249
- row.shortcut = options['shortcut']
250
- row.command = options['command']
251
- row.tag = options['tag'] if options['tag']
252
- row.description = options['description'] if options['description']
253
- row.function = options['function'] if options['function']
254
- end
255
- end
256
-
257
- def save
258
- File.open(aka_yml, 'w+') do |f|
259
- f.write shortcuts.to_yaml
260
- end
261
- end
196
+ private
262
197
 
263
- def aka_yml
264
- ENV['AKA'] || File.expand_path('~/.aka.yml')
198
+ def configuration
199
+ @configuration ||= Configuration.new
265
200
  end
266
201
 
267
- def shortcuts_by_tag
268
- shortcuts.inject({ :default => [] }) do |acc, (_, row)|
269
- if row.tag
270
- row.tag.each do |tag|
271
- acc[tag] ||= []
272
- acc[tag] << row
273
- end
274
- else
275
- acc[:default] << row
276
- end
277
- acc
278
- end
279
- end
280
-
281
- def match_shortcuts_by_tag(tags, &blk)
282
- excluded = { :tags => [], :shortcuts => 0 }
283
-
284
- shortcuts_by_tag.each do |tag, rows|
285
- next if rows.empty?
286
- unless tag == :default || tags.empty? || tags.include?(tag)
287
- excluded[:tags] << tag
288
- excluded[:shortcuts] += rows.length
289
- next
290
- end
291
-
292
- yield(tag, rows)
293
- end
294
-
295
- excluded
202
+ def shortcuts
203
+ configuration.shortcuts.all
296
204
  end
297
205
 
298
206
  def excluded_output(excluded)
@@ -320,20 +228,6 @@ module Aka
320
228
  puts "#{row.shortcut.ljust(20)} #{description.ljust(70).strip}"
321
229
  end
322
230
 
323
- def generate_output(row)
324
- string = if row.function
325
- <<-EOS.gsub(/^ /, '')
326
- function #{row.shortcut} {
327
- #{row.command}
328
- }
329
- EOS
330
- else
331
- %{alias #{row.shortcut}="#{row.command.gsub(%{"}, %{\\"})}"}
332
- end
333
-
334
- string
335
- end
336
-
337
231
  def parse_row_txt(row, txt)
338
232
  if txt =~ /^Shortcut:(.+)$/
339
233
  row.shortcut = $1.strip
@@ -0,0 +1,23 @@
1
+ module Aka
2
+ module Upgrader
3
+ module FromV0
4
+ def self.run(aka_yml)
5
+ v0 = YAML::load_file(aka_yml)
6
+
7
+ current = {
8
+ :version => Aka::Configuration::FORMAT,
9
+ :shortcuts => v0
10
+ }
11
+
12
+ FileUtils.cp(aka_yml, "#{aka_yml}.backup")
13
+
14
+ File.open(aka_yml, 'w+') do |f|
15
+ f.write current.to_yaml
16
+ end
17
+
18
+ puts "Upgraded #{aka_yml}."
19
+ puts "Backed up to #{aka_yml}.backup."
20
+ end
21
+ end
22
+ end
23
+ end
data/lib/aka/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Aka
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
data/lib/aka.rb CHANGED
@@ -1,3 +1,7 @@
1
+ require "aka/shortcuts"
2
+ require "aka/links"
3
+ require "aka/configuration"
4
+ require "aka/upgrader"
1
5
  require "aka/store"
2
6
  require "aka/version"
3
7
  require "aka/app"
@@ -0,0 +1,41 @@
1
+ aka-link(1) -- Link keyboard shortcuts
2
+ ======================================
3
+
4
+ ## SYNOPSIS
5
+
6
+ `aka link` [`-t` <tag1>[,<tag2>...]]
7
+ [`-o` <output>]
8
+ [`--delete`]
9
+
10
+ ## DESCRIPTION
11
+
12
+ This command manages keyboard shortcuts links in `aka(7)`.
13
+
14
+ After creating a link, you can easily synchronize changes to shortcuts to designated `output` files.
15
+
16
+ ## OPTIONS
17
+
18
+ * `-t` <tag1>[,<tag2>...]:
19
+ A comma-separated list of tags to filter with. Shortcuts tagged with <tag1>[,<tag2>...] will be included. Shortcuts tagged with other tags are excluded. Shortcuts with no tags are always included.
20
+
21
+ * `-o <output>`:
22
+ The location to link the output to.
23
+
24
+ * `--delete`:
25
+ Delete the link with the given options.
26
+
27
+ ## EXAMPLES
28
+
29
+ Add a link:
30
+
31
+ $ aka link --tag os:linux --output ~/.aka.zsh
32
+ Saved link.
33
+
34
+ Remove a link:
35
+
36
+ $ aka link --delete --tag os:linux --output ~/.aka.zsh
37
+ Removed link.
38
+
39
+ ## SEE ALSO
40
+
41
+ `aka(7)`, `aka-sync(1)`
@@ -0,0 +1,26 @@
1
+ aka-sync(1) -- Synchronize keyboard shortcuts
2
+ =============================================
3
+
4
+ ## SYNOPSIS
5
+
6
+ `aka sync`
7
+
8
+ ## DESCRIPTION
9
+
10
+ This command manages synchronizes keyboard shortcuts links in `aka(7)`.
11
+
12
+ Any links you've created will be generated to the designated `output` with the given `tags`.
13
+
14
+ ## EXAMPLES
15
+
16
+ Synchronize links:
17
+
18
+ $ aka link --tag os:linux --output ~/.aka.zsh
19
+ Saved link.
20
+ $ aka sync
21
+ Generated ~/.aka.zsh.
22
+ 4 shortcut(s) excluded (#linux).
23
+
24
+ ## SEE ALSO
25
+
26
+ `aka(7)`, `aka-link(1)`
@@ -0,0 +1,22 @@
1
+ aka-upgrade(1) -- Upgrade keyboard shortcuts
2
+ =========================================
3
+
4
+ ## SYNOPSIS
5
+
6
+ `aka upgrade`
7
+
8
+ ## DESCRIPTION
9
+
10
+ This command manages upgrades the keyboard shortcuts format in `aka(7)`.
11
+
12
+ ## EXAMPLES
13
+
14
+ Upgrade links from v0 to v1:
15
+
16
+ $ aka upgrade
17
+ Upgraded ~/.aka.yml.
18
+ Backed up to ~/.aka.yml.backup."
19
+
20
+ ## SEE ALSO
21
+
22
+ `aka(7)`
data/man/aka.7.ronn CHANGED
@@ -8,7 +8,9 @@ aka(7) -- Manage Shell Keyboard Shortcuts
8
8
  `aka` edit <shortcut> \[options\]<br>
9
9
  `aka` remove <shortcut><br>
10
10
  `aka` list \[options\]<br>
11
- `aka` generate \[options\]
11
+ `aka` link \[options\]<br>
12
+ `aka` generate \[options\]<br>
13
+ `aka` sync
12
14
 
13
15
  ## DESCRIPTION
14
16
 
@@ -79,6 +81,30 @@ Remove a shortcut:
79
81
  $ aka remove ls
80
82
  Removes shortcut.
81
83
 
84
+ Add a link:
85
+
86
+ $ aka link --tag os:linux --output ~/.aka.zsh
87
+ Saved link.
88
+
89
+ Remove a link:
90
+
91
+ $ aka link --delete --tag os:linux --output ~/.aka.zsh
92
+ Removed link.
93
+
94
+ Synchronize links:
95
+
96
+ $ aka link --tag os:linux --output ~/.aka.zsh
97
+ Saved link.
98
+ $ aka sync
99
+ Generated ~/.aka.zsh.
100
+ 4 shortcut(s) excluded (#linux).
101
+
102
+ Upgrade links from v0 to v1:
103
+
104
+ $ aka upgrade
105
+ Upgraded ~/.aka.yml.
106
+ Backed up to ~/.aka.yml.backup."
107
+
82
108
  ## ENVIRONMENT
83
109
 
84
110
  * `AKA`:<br>
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hsume2-aka
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Henry Hsu
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-02-26 00:00:00.000000000 Z
11
+ date: 2014-03-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -103,7 +103,7 @@ extensions: []
103
103
  extra_rdoc_files: []
104
104
  files:
105
105
  - .gitignore
106
- - .ruby-version
106
+ - .ruby-version.example
107
107
  - .travis.yml
108
108
  - Gemfile
109
109
  - Guardfile
@@ -116,21 +116,31 @@ files:
116
116
  - features/aka/edit.feature
117
117
  - features/aka/generate.feature
118
118
  - features/aka/help.feature
119
+ - features/aka/link.feature
119
120
  - features/aka/list.feature
120
121
  - features/aka/remove.feature
121
122
  - features/aka/show.feature
123
+ - features/aka/upgrade.feature
122
124
  - features/step_definitions/aka_steps.rb
123
125
  - features/support/env.rb
124
126
  - lib/aka.rb
125
127
  - lib/aka/app.rb
128
+ - lib/aka/configuration.rb
129
+ - lib/aka/links.rb
130
+ - lib/aka/man/.gitkeep
131
+ - lib/aka/shortcuts.rb
126
132
  - lib/aka/store.rb
133
+ - lib/aka/upgrader.rb
127
134
  - lib/aka/version.rb
128
135
  - man/aka-add.1.ronn
129
136
  - man/aka-edit.1.ronn
130
137
  - man/aka-generate.1.ronn
138
+ - man/aka-link.1.ronn
131
139
  - man/aka-list.1.ronn
132
140
  - man/aka-remove.1.ronn
133
141
  - man/aka-show.1.ronn
142
+ - man/aka-sync.1.ronn
143
+ - man/aka-upgrade.1.ronn
134
144
  - man/aka.7.ronn
135
145
  - test/tc_something.rb
136
146
  - lib/aka/man/aka-add.1
@@ -139,12 +149,18 @@ files:
139
149
  - lib/aka/man/aka-edit.1.txt
140
150
  - lib/aka/man/aka-generate.1
141
151
  - lib/aka/man/aka-generate.1.txt
152
+ - lib/aka/man/aka-link.1
153
+ - lib/aka/man/aka-link.1.txt
142
154
  - lib/aka/man/aka-list.1
143
155
  - lib/aka/man/aka-list.1.txt
144
156
  - lib/aka/man/aka-remove.1
145
157
  - lib/aka/man/aka-remove.1.txt
146
158
  - lib/aka/man/aka-show.1
147
159
  - lib/aka/man/aka-show.1.txt
160
+ - lib/aka/man/aka-sync.1
161
+ - lib/aka/man/aka-sync.1.txt
162
+ - lib/aka/man/aka-upgrade.1
163
+ - lib/aka/man/aka-upgrade.1.txt
148
164
  - lib/aka/man/aka.7
149
165
  - lib/aka/man/aka.7.txt
150
166
  homepage: ''
@@ -176,9 +192,11 @@ test_files:
176
192
  - features/aka/edit.feature
177
193
  - features/aka/generate.feature
178
194
  - features/aka/help.feature
195
+ - features/aka/link.feature
179
196
  - features/aka/list.feature
180
197
  - features/aka/remove.feature
181
198
  - features/aka/show.feature
199
+ - features/aka/upgrade.feature
182
200
  - features/step_definitions/aka_steps.rb
183
201
  - features/support/env.rb
184
202
  - test/tc_something.rb
data/.ruby-version DELETED
@@ -1 +0,0 @@
1
- 2.0.0-p247