#!/usr/bin/env ruby require 'io/console' require 'image_size' def files(ext) fs = [] fs << Dir.glob(ext.downcase).sort fs << Dir.glob(ext.upcase).sort fs end def file_size(path) File::stat(path).size.to_s.reverse.scan(/.{1,3}/).join(',').reverse end term_width = `tput cols`.to_i * 2 term_height = `tput lines`.to_i * 4 images = ARGV images.size == 0 and ['*.jpg', '*.jpeg', '*.png', '*.gif'].each {|ext| images << files(ext) } images.flatten! images.size == 0 and (warn('No image files.'); exit(1)) n = 0; loop { image = images[n % images.size] # TODO: check file type # TODO: / + * image_size = ImageSize.path(image) wr = image_size.width / term_width.to_f hr = image_size.height / term_height.to_f width = wr > hr ? width = term_width * 0.9 : width = term_width * wr / hr * 0.9 system('catimg -r 2 -w %d %s' % [width, image]) puts('(%d / %d) %s [ %s: %d x %d ] %s bytes' % \ [n % images.size + 1, images.size, image, image_size.format, image_size.width, image_size.height, file_size(image)]) c = STDIN.getch c == ?q and break c == ?\C-c and break c == ' ' and n += 1 c == ?j and n += 1 c == ?k and n -= 1 c == ?0 and n = 0 c == ?G and n = images.size - 1 c == ?v and system('eom %s' % image) puts } __END__