oktags 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 064d3e1decf8e198564e3ea03bfa9ffef9b6982a7aa4626984ecd7390a4ecc7f
4
- data.tar.gz: bd34c659eb79cc5c9f27245f85390baf2cafdb6c8096d01778e83eaaaaa3a6a4
3
+ metadata.gz: a89cb6486aade586eadf73e6357523224302d7d4034a2dd3bef56a560d6e8f6c
4
+ data.tar.gz: 0d103fef3b089a96eb954b0d3fce4709f93a2c11b2c0a938519a9886fa60593e
5
5
  SHA512:
6
- metadata.gz: 8a0305539aff833792c1d76ba98acdba725115c0dad65438476d12a6ac83ae425cc2e3c4feac123f830ddc95e4156feadbca9a3ac7fc3db72d8d0f9e0660116e
7
- data.tar.gz: 83d1259fc52913ea08a3fbcfd9c688da832bd0f5c42be2e2b346b0955f146391211405eeb0b1bdf6d572bc5244136bc5b5be9a68c91ca5fadca026638626f54d
6
+ metadata.gz: f94eef23e1e45da986d19b664ec370869bdb9fe9230ddc6bd95cc46d7cd552d2c86a96781e23ef15591ca10453c56539b4a6af513dc35cb501972cd6703841f0
7
+ data.tar.gz: 6bbfbe8fc72f800293af14782d373fa7eb882ee98c970b72c63816d6e31af2cc8e4d891d49812fac5c14182283cb1ba3cccbefe7e95afc7a5b6275882768764b
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- oktags (0.1.0)
4
+ oktags (0.2.1)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -2,8 +2,28 @@
2
2
  # coding: utf-8
3
3
 
4
4
  require 'optparse'
5
+ require 'set'
5
6
  require 'readline'
6
7
  require 'fileutils'
8
+ require 'byebug'
9
+
10
+ # TODO: Put this into it's own file
11
+ class Hash
12
+ # like invert but not lossy. possibly a good blog post.
13
+ def safe_invert
14
+ inject({}) do |acc, (k, v)|
15
+ if v.is_a? Array
16
+ v.each do |vx|
17
+ acc[vx] = acc[vx].nil? ? k : [acc[vx], k].flatten
18
+ end
19
+ else
20
+ acc[v] = acc[v].nil? ? k : [acc[v], k].flatten
21
+ end
22
+ acc
23
+ end
24
+ end
25
+ end
26
+
7
27
 
8
28
  module OK
9
29
  module Tags
@@ -16,18 +36,39 @@ module OK
16
36
  s&.gsub(/[\[\]]/) { |x| "\\" + x }
17
37
  end
18
38
 
19
- def find_tags_for(path = '**/*')
20
- tags = []
39
+ def search_files_with_tags(path = '**/*')
40
+ tagged_files = Hash.new { |h, k| h[k] = [] }
41
+
21
42
  Dir.glob(escape_glob(path)).each do |file|
22
- file = File.basename(file, '.*')
43
+ basename = File.basename(file, '.*')
23
44
  file_tags =
24
- file.match(/--\[(.*)\]/)&.to_a&.at(1)&.split(',')&.map(&:strip)&.map(
25
- &:downcase
26
- )
27
- tags << file_tags if file_tags
45
+ basename.match(/--\[(.*)\]/)&.to_a&.at(1)&.split(',')&.map(&:strip)
46
+ &.map(&:downcase)
47
+ file_tags.each { |t| tagged_files[t] << file } if file_tags
28
48
  end
49
+ tagged_files
50
+ end
51
+
52
+ def list_files_with_tags(tags, path = '**/*')
53
+ # TODO: Refactor reused code
54
+ tags = tags.split(',')&.map(&:strip)&.map(&:downcase)
55
+
56
+ tagged_files = search_files_with_tags(path)
57
+ files =
58
+ tagged_files.safe_invert.filter do |files, file_tags|
59
+ file_tags = [file_tags] if file_tags.is_a? String
60
+ tags.to_set.subset?(file_tags.to_set)
61
+ end&.keys&.flatten&.sort
29
62
 
30
- tags.flatten.compact.sort
63
+ puts files
64
+ files
65
+ end
66
+
67
+ def find_tags_for(path = '**/*')
68
+ tagged_files =
69
+ path ? search_files_with_tags(path) : search_files_with_tags
70
+ # return an array with redundant tags
71
+ tagged_files.map { |k, v| Array.new(v.count, k) }.flatten.sort
31
72
  end
32
73
 
33
74
  def count_tags(tags)
@@ -136,14 +177,6 @@ module OK
136
177
  def main
137
178
  OptionParser.new do |opts|
138
179
  opts.banner = 'Usage: oktags [options]'
139
- opts.on(
140
- '-l',
141
- '--list [PATH]',
142
- 'List file tags (optionally for PATH)'
143
- ) do |path|
144
- path ? list_pretty_tags(path) : list_pretty_tags
145
- exit
146
- end
147
180
  opts.on(
148
181
  '-a',
149
182
  '--add-tags TAGS FILE',
@@ -152,6 +185,14 @@ module OK
152
185
  add_tags_to_file(tags, ARGV[0])
153
186
  exit
154
187
  end
188
+ opts.on(
189
+ '-d',
190
+ '--delete-tag-from-file TAG FILE',
191
+ 'Delete TAG from FILE'
192
+ ) do |tag|
193
+ delete_tag_from_file(tag, ARGV[0])
194
+ exit
195
+ end
155
196
  opts.on(
156
197
  '-i',
157
198
  '--add-tags-interactively FILE',
@@ -160,6 +201,14 @@ module OK
160
201
  read_and_add_tags_for(file)
161
202
  exit
162
203
  end
204
+ opts.on(
205
+ '-l',
206
+ '--list [PATH]',
207
+ 'List file tags (optional PATH)'
208
+ ) do |path|
209
+ path ? list_pretty_tags(path) : list_pretty_tags
210
+ exit
211
+ end
163
212
  opts.on(
164
213
  '-r',
165
214
  '--rename-tag OLD_TAG NEW_TAG',
@@ -169,14 +218,20 @@ module OK
169
218
  exit
170
219
  end
171
220
  opts.on(
172
- '-d',
173
- '--delete-tag-from-file TAG FILE',
174
- 'Delete TAG from FILE'
175
- ) do |tag|
176
- delete_tag_from_file(tag, ARGV[0])
221
+ '-s',
222
+ '--search-files-with-tags TAGS [PATH]',
223
+ 'Search files which include (comma-separated) tags recursively (optional PATH)'
224
+ ) do |tags|
225
+ if ARGV[0]
226
+ list_files_with_tags(tags, ARGV[0])
227
+ else
228
+ list_files_with_tags(tags)
229
+ end
177
230
  exit
178
231
  end
179
232
  end.parse!
180
233
  end
181
234
  end
182
235
  end
236
+
237
+ OK::Tags.main
@@ -1,5 +1,5 @@
1
1
  module OK
2
2
  module Tags
3
- VERSION = '0.2.0'
3
+ VERSION = '0.2.1'
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: oktags
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alain M. Lafon