Module: Drakkon::Images::BulkRename
- Defined in:
- lib/drakkon/lib/images/bulk_rename.rb
Overview
This renames png’s in a directory to match the index_frame 0..10 etc
Class Method Summary collapse
- .images ⇒ Object
- .prompt ⇒ Object
- .run!(args = []) ⇒ Object
- .sorted_images(sort_method) ⇒ Object
- .start(list) ⇒ Object
Class Method Details
.images ⇒ Object
61 62 63 64 65 |
# File 'lib/drakkon/lib/images/bulk_rename.rb', line 61 def self.images # Dir["#{Dir.pwd}/*.png"].sort_by(&:File.basename) Dir["#{Dir.pwd}/*.png"] end |
.prompt ⇒ Object
67 68 69 |
# File 'lib/drakkon/lib/images/bulk_rename.rb', line 67 def self.prompt TTY::Prompt.new(active_color: :cyan, interrupt: :exit) end |
.run!(args = []) ⇒ Object
5 6 7 8 9 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/drakkon/lib/images/bulk_rename.rb', line 5 def self.run!(args = []) args ||= [] sort_method = if args.empty? :mtime else args.shift.to_sym end # Lazy Safety sort_method = :mtime unless %i[name mtime].include?(sort_method) puts <<~HELP Usage [sort_method = mtime, name] - Run in the directory you wish to rename the images (uses mtime) Current Directory; #{Dir.pwd.pastel(:yellow)} Images: HELP list = sorted_images(sort_method) list.each do |img| img_s = img.gsub("#{Dir.pwd}/", '').pastel(:yellow) puts " - #{img_s}" end puts exit unless prompt.yes? "#{'Are you sure?'.pastel(:bright_yellow)} Rename!? #{'(Destructive)'.pastel(:red)}" start(list) end |
.sorted_images(sort_method) ⇒ Object
54 55 56 57 58 59 |
# File 'lib/drakkon/lib/images/bulk_rename.rb', line 54 def self.sorted_images(sort_method) case sort_method when :mtime then images.sort_by { |x| File.mtime(x) } when :name then images.sort_by { |x| File.basename(x) } end end |
.start(list) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/drakkon/lib/images/bulk_rename.rb', line 39 def self.start(list) # Create tmp_dir to send stuff too Dir.mktmpdir do |tmp_dir| # Move / rename all out into tmp_dir to avoid clobbering list.each_with_index do |f, i| FileUtils.mv(f, "#{tmp_dir}/#{i}.png") end # Move all back Dir["#{tmp_dir}/*.png"].each do |f| FileUtils.mv(f, Dir.pwd) end end end |