Skip to content

Instantly share code, notes, and snippets.

@eam
Created May 18, 2016 19:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eam/0c054ab590b1fbb4c2b543f1c8eb1698 to your computer and use it in GitHub Desktop.
Save eam/0c054ab590b1fbb4c2b543f1c8eb1698 to your computer and use it in GitHub Desktop.
#!/usr/bin/ruby
# Quick and dirty comic book reader
# Bugs: Can't go backwards and won't clean up properly if you quit early.
require 'RUDL'
include RUDL
include Constant
XRES = 1600
YRES = 1200
OLDDIR = Dir.pwd
TMPDIR = '/tmp/comics' + Process.euid.to_s + '.' + Process.pid.to_s
#TMPDIR = '/mnt/hdc/pub/tmp/comics' + Process.euid.to_s + '.' + Process.pid.to_s
UNZIP = "/usr/bin/unzip"
UNRAR = "/usr/bin/unrar"
begin
Dir.mkdir TMPDIR, 448
rescue
raise "Couldn't make dir #{TMPDIR}, #{$!}"
end
screen = DisplaySurface.new [XRES,YRES], HWSURFACE|DOUBLEBUF, 32
ARGV.each do |v|
case v
when /\.(zip|cbz)$/i
system UNZIP, "-j", "-d", TMPDIR, v
raise "Couldn't unzip #{v}. #{$?}." unless $?.to_i.zero?
when /\.(rar|cbr)$/i
system UNRAR, "e", "-ep", v, TMPDIR
raise "Couldn't unrar #{v}. #{$?}." unless $?.to_i.zero?
else
puts "File #{v} is unsupported. Add me."
end
pages = Dir.entries(TMPDIR)
pages = pages.select {|p| p =~ /(jpg|jpeg|png|bmp|gif)$/i}.map {|p| TMPDIR + "/" + p}
pages.sort!
puts pages.inspect
pages.each do |page|
ObjectSpace::garbage_collect
surface = Surface.load_new page
ratiox = screen.w / surface.w.to_f
ratioy = screen.h / surface.h.to_f
ratio = (ratiox > ratioy) ? ratioy : ratiox
zoomed = surface.zoom(ratio, ratio, true)
screen.fill(0)
screen.blit zoomed, [0,0]
screen.flip
while true do
event = EventQueue.wait
case event
when QuitEvent
raise "quitting"
when KeyDownEvent
case event.key
when K_ESCAPE, K_q
raise "quitting"
when K_n, K_SPACE
break
end
end
end
end
Dir.foreach(TMPDIR) {|d|
next if d =~ /^(\.|\.\.)/
File.delete(TMPDIR + '/' + d)
}
end
Dir.rmdir(TMPDIR)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment