lib/zpng/cli.rb in zpng-0.1.0 vs lib/zpng/cli.rb in zpng-0.1.1
- old
+ new
@@ -6,11 +6,10 @@
class ZPNG::CLI
ACTIONS = {
'chunks' => 'Show file chunks (default)',
%w'i info' => 'General image info (default)',
- 'ascii' => 'Try to display image as ASCII (works best with monochrome images)',
'scanlines' => 'Show scanlines info',
'palette' => 'Show palette'
}
DEFAULT_ACTIONS = %w'info chunks'
@@ -44,14 +43,29 @@
end
opts.on "-U", "--unpack-imagedata", "unpack Image Data (IDAT) chunk(s), output to stdout" do
@actions << :unpack_imagedata
end
+ opts.separator ""
opts.on "-c", "--crop GEOMETRY", "crop image, {WIDTH}x{HEIGHT}+{X}+{Y},",
"puts results on stdout unless --ascii given" do |x|
@actions << [:crop, x]
end
+
+ opts.separator ""
+ opts.on "-A", '--ascii', 'Try to convert image to ASCII (works best with monochrome images)' do
+ @actions << :ascii
+ end
+ opts.on "-N", '--ansi', 'Try to display image as ANSI colored text' do
+ @actions << :ansi
+ end
+ opts.on "-2", '--256', 'Try to display image as 256-colored text' do
+ @actions << :ansi256
+ end
+ opts.on "-W", '--wide', 'Use 2 horizontal characters per one pixel' do
+ @options[:wide] = true
+ end
end
if (argv = optparser.parse(@argv)).empty?
puts optparser.help
return
@@ -118,10 +132,38 @@
def chunks
@img.dump
end
def ascii
- puts @img.to_s
+ @img.height.times do |y|
+ @img.width.times do |x|
+ c = @img[x,y].to_ascii
+ c *= 2 if @options[:wide]
+ print c
+ end
+ puts
+ end
+ end
+
+ def ansi
+ spc = @options[:wide] ? " " : " "
+ @img.height.times do |y|
+ @img.width.times do |x|
+ print spc.background(@img[x,y].closest_ansi_color)
+ end
+ puts
+ end
+ end
+
+ def ansi256
+ require 'rainbow'
+ spc = @options[:wide] ? " " : " "
+ @img.height.times do |y|
+ @img.width.times do |x|
+ print spc.background(@img[x,y].to_s)
+ end
+ puts
+ end
end
def scanlines
pp @img.scanlines
end