Class: Mooncats::Tool

Inherits:
Object
  • Object
show all
Defined in:
lib/mooncats.rb

Instance Method Summary collapse

Instance Method Details

#run(args) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/mooncats.rb', line 24

def run( args )
  opts = { zoom: 1,
           outdir: '.',
         }

  parser = OptionParser.new do |cmd|
    cmd.banner = "Usage: mooncat [options] IDs"

    cmd.separator "  Mint mooncats from original designs - for IDs use 5 byte hexstrings (e.g 0x004fc21270)"
    cmd.separator ""
    cmd.separator "  Options:"

    cmd.on("-z", "--zoom=ZOOM", "Zoom factor x2, x4, x8, etc. (default: #{opts[:zoom]})", Integer ) do |zoom|
      opts[:zoom] = zoom
    end

    cmd.on("-d", "--dir=DIR", "Output directory (default: #{opts[:outdir]})", String ) do |outdir|
      opts[:outdir] = outdir
    end


    cmd.on("-h", "--help", "Prints this help") do
      puts cmd
      exit
    end
  end

  parser.parse!( args )

  puts "opts:"
  pp opts

  puts "    setting zoom to #{opts[:zoom]}x"   if opts[:zoom] != 1

  ## make sure outdir exits (default is current working dir e.g. .)
  FileUtils.mkdir_p( opts[:outdir] )  unless Dir.exist?( opts[:outdir] )

  args.each_with_index do |arg,index|
    cat_id = arg.downcase
    ## note: cut-off optionial 0x
    cat_id = cat_id[2..-1]   if cat_id.start_with?( '0x')

    cat = Image.generate( cat_id )

    cat_name  = "mooncat-#{cat_id}"

    ##  if zoom - add x2,x4 or such
    if opts[:zoom] != 1
      cat = cat.zoom( opts[:zoom] )
      cat_name << "_x#{opts[:zoom]}"
    end

    path  = "#{opts[:outdir]}/#{cat_name}.png"
    puts "==> (#{index+1}/#{args.size}) minting mooncat 0x#{cat_id}; writing to >#{path}<..."

    cat.save( path )
  end

  puts "done"
end