oktags 0.2.0 → 0.2.1
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.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/lib/ok/tags.rb +76 -21
- data/lib/ok/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a89cb6486aade586eadf73e6357523224302d7d4034a2dd3bef56a560d6e8f6c
|
4
|
+
data.tar.gz: 0d103fef3b089a96eb954b0d3fce4709f93a2c11b2c0a938519a9886fa60593e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f94eef23e1e45da986d19b664ec370869bdb9fe9230ddc6bd95cc46d7cd552d2c86a96781e23ef15591ca10453c56539b4a6af513dc35cb501972cd6703841f0
|
7
|
+
data.tar.gz: 6bbfbe8fc72f800293af14782d373fa7eb882ee98c970b72c63816d6e31af2cc8e4d891d49812fac5c14182283cb1ba3cccbefe7e95afc7a5b6275882768764b
|
data/Gemfile.lock
CHANGED
data/lib/ok/tags.rb
CHANGED
@@ -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
|
20
|
-
|
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
|
-
|
43
|
+
basename = File.basename(file, '.*')
|
23
44
|
file_tags =
|
24
|
-
|
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
|
-
|
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
|
-
'-
|
173
|
-
'--
|
174
|
-
'
|
175
|
-
) do |
|
176
|
-
|
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
|
data/lib/ok/version.rb
CHANGED