bin/image_optim in image_optim-0.9.1 vs bin/image_optim in image_optim-0.10.0

- old
+ new

@@ -1,24 +1,26 @@ #!/usr/bin/env ruby # encoding: UTF-8 -require 'image_optim' -require 'progress' -require 'optparse' -require 'find' +require 'image_optim/runner' +args = ARGV.dup options = {} -OptionParser.accept(ImageOptim::TrueFalseNil, OptionParser.top.atype[TrueClass][0].merge('nil' => nil)){ |arg, val| val } - option_parser = OptionParser.new do |op| - op.banner = <<-TEXT -#{op.program_name} v#{ImageOptim.version} + op.accept(ImageOptim::TrueFalseNil, OptionParser.top.atype[TrueClass][0].merge('nil' => nil)){ |arg, val| val } -Usege: - #{op.program_name} [options] image_path … - + op.banner = <<-TEXT.gsub(/^\s*\|/, '') + |#{op.program_name} v#{ImageOptim.version} + | + |Usege: + | #{op.program_name} [options] image_path … + | + |Configuration will be read and prepanded to options from two paths: + | #{ImageOptim::Config::GLOBAL_CONFIG_PATH} + | #{ImageOptim::Config::LOCAL_CONFIG_PATH} (in current working directory) + | TEXT op.on('-r', '-R', '--recursive', 'Recurively scan directories for images') do |recursive| options[:recursive] = recursive end @@ -82,121 +84,27 @@ op.on('-v', '--verbose', 'Verbose output') do |verbose| options[:verbose] = verbose end op.on_tail('-h', '--help', 'Show full help') do - puts option_parser.help + puts op.help exit end op.on_tail('--version', 'Show version') do puts ImageOptim.version exit end end begin - option_parser.parse! + option_parser.parse!(args) + ImageOptim::Runner.run!(args, options) or exit 1 rescue OptionParser::ParseError => e abort "#{e.to_s}\n\n#{option_parser.help}" -end - -if options[:verbose] - def print_options(options, level = 1) - prefix = ' ' * level - options.each do |key, value| - if value.is_a?(Hash) - puts "#{prefix}#{key}:" - print_options(value, level + 1) - else - puts "#{prefix}#{key}: #{value.inspect}" - end - end - end - - puts 'Options:' - print_options(options) -end - -if ARGV.empty? - abort "specify image paths to optimize\n\n#{option_parser.help}" -else - recursive = options.delete(:recursive) - image_optim = begin - ImageOptim.new(options) - rescue ImageOptim::ConfigurationError => e +rescue => e + if options[:verbose] + abort "#{e.to_s}\n#{e.backtrace.join("\n")}" + else abort e.to_s - end - - paths = [] - ARGV.each do |arg| - if File.file?(arg) - if image_optim.optimizable?(arg) - paths << arg - else - warn "#{arg} is not an image or there is no optimizer for it" - end - else - if recursive - Find.find(arg) do |path| - paths << path if File.file?(path) && image_optim.optimizable?(path) - end - else - warn "#{arg} is not a file" - end - end - end - - module Space - SIZE_SYMBOLS = %w[B K M G T P E Z Y].freeze - PRECISION = 1 - LENGTH = 4 + PRECISION + 1 - COEF = 1 / Math.log(10) - - EMPTY_SPACE = ' ' * LENGTH - NOT_COUNTED_SPACE = '!' * LENGTH - - class << self - attr_writer :base10 - def denominator - @denominator ||= @base10 ? 1000.0 : 1024.0 - end - - def space(size, options = {}) - case size - when false - NOT_COUNTED_SPACE.bold.red - when 0, nil - EMPTY_SPACE - else - number, degree = size, 0 - while number.abs >= 1000 && degree < SIZE_SYMBOLS.length - 1 - number /= denominator - degree += 1 - end - - "#{degree == 0 ? number.to_s : "%.#{PRECISION}f" % number}#{SIZE_SYMBOLS[degree]}".rjust(LENGTH) - end - end - end - end - - def size_percent(src_size, dst_size) - '%5.2f%% %s' % [100 - 100.0 * dst_size / src_size, Space.space(src_size - dst_size)] - end - - paths = paths.with_progress('optimizing') - results = image_optim.optimize_images(paths) do |src, dst| - if dst - src_size, dst_size = src.size, dst.size - percent = size_percent(src_size, dst_size) - dst.replace(src) - ["#{percent} #{src}", src_size, dst_size] - else - ["------ #{Space::EMPTY_SPACE} #{src}", src.size, src.size] - end - end - lines, src_sizes, dst_sizes = results.transpose - if lines - $stdout.puts lines, "Total: #{size_percent(src_sizes.inject(:+), dst_sizes.inject(:+))}\n" end end