class BatchRename::CLI

Constants

EDITOR
EXIT_CODE_FAILURE
EXIT_CODE_SUCCESS
INSTRUCTIONS

Attributes

catalogs[R]
diff_tmp_dir[R]

Public Class Methods

new() click to toggle source
# File lib/batch_rename/cli.rb, line 42
def initialize
  @diff_tmp_dir = Dir.mktmpdir('batch-rename-')

  @catalogs = [
    Catalog.new(:before, INSTRUCTIONS[:before], diff_tmp_dir),
    Catalog.new(:after, INSTRUCTIONS[:after], diff_tmp_dir)
  ]
end
run!() click to toggle source
# File lib/batch_rename/cli.rb, line 19
def self.run!
  begin
    CLI::new.run!
    exit EXIT_CODE_SUCCESS
  rescue Editor::EditorNotFoundError => e
    warn e.message
    exit EXIT_CODE_FAILURE
  end
end

Public Instance Methods

run!() click to toggle source
# File lib/batch_rename/cli.rb, line 29
def run!
  caption = "batch_rename v#{VERSION} - #{ Dir.pwd }"
  write_catalog_files!

  puts 'Launching graphical editor…'
  result = EDITOR.new(catalogs, caption).launch!.result

  puts "Renaming #{ result.modified_count } file(s)"
  puts rename!(result)
end

Private Instance Methods

rename!(result) click to toggle source
# File lib/batch_rename/cli.rb, line 55
def rename!(result)
  result.modified_pairs
    .map { |pair| pair.map(&Shellwords.method(:escape)) }
    .map { |a, b| `mv -nv #{a} #{b}` }
end
write_catalog_files!() click to toggle source
# File lib/batch_rename/cli.rb, line 51
def write_catalog_files!
  `ls -A | tee #{ catalogs.map(&:escaped_file_name).join(' >') }`
end