module OK::Tags
Constants
- VERSION
Public Instance Methods
delete_tag_from_file(tag, file)
click to toggle source
# File lib/ok/tags.rb, line 128 def delete_tag_from_file(tag, file) unless file ( puts 'Needs a FILE input; i.e. `-d tag1 filename`' exit ) end tags = find_tags_for(file) tags = tags.reject { |t| t == tag } new_filename = filename_with_tags(file, tags) FileUtils.mv(file, new_filename) if file != new_filename new_filename end
escape_glob(s)
click to toggle source
oktags uses –[] as the place to save tags. [] does have special meaning for Ruby Dir.glob, though. Hence, it's escaped.
# File lib/ok/tags.rb, line 17 def escape_glob(s) s&.gsub(/[\[\]]/) { |x| "\\" + x } end
main()
click to toggle source
# File lib/ok/tags.rb, line 159 def main OptionParser.new do |opts| opts.banner = 'Usage: oktags [options]' opts.on( '-a', '--add-tags TAGS FILE', 'Add comma-separated TAGS to FILE' ) do |tags| add_tags_to_file(tags, ARGV[0]) exit end opts.on( '-d', '--delete-tag-from-file TAG FILE', 'Delete TAG from FILE' ) do |tag| delete_tag_from_file(tag, ARGV[0]) exit end opts.on( '-i', '--add-tags-interactively FILE', 'Auto-complete tags and add them to FILE' ) do |file| read_and_add_tags_for(file) exit end opts.on( '-l', '--list [PATH]', 'List file tags recursively (at optional PATH)' ) do |path| path ? list_pretty_tags(path) : list_pretty_tags exit end opts.on( '-r', '--rename-tag OLD_TAG NEW_TAG', 'Rename OLD_TAG to NEW_TAG(S) recursively for all files' ) do |old_tag| rename_tag('.', old_tag, ARGV[0]) exit end opts.on( '-s', '--search-files-with-tags TAGS [PATH]', 'Search files which include (comma-separated) TAGS recursively (at optional PATH)' ) do |tags| if ARGV[0] list_files_with_tags(tags, ARGV[0]) else list_files_with_tags(tags) end exit end end.parse! end
rename_tag(path, old_tag, new_tag)
click to toggle source
# File lib/ok/tags.rb, line 145 def rename_tag(path, old_tag, new_tag) unless new_tag ( puts 'Needs a NEW_TAG input; i.e. `-r old_tag new_tag`' exit ) end Dir.glob("#{path}/**/*--\\[*#{old_tag}*\\]*").each do |file| file = add_tags_to_file(new_tag, file) delete_tag_from_file(old_tag, file) end end