Skip to content

Instantly share code, notes, and snippets.

@arioch
Forked from barn/quicky.rb
Created April 17, 2013 08:27
Show Gist options
  • Save arioch/5402684 to your computer and use it in GitHub Desktop.
Save arioch/5402684 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
#
# Put something god awful in your muttrc like:
# macro attach <space> "<enter-command>unset wait_key<enter><shell-escape>rm -f $HOME/.quicky<enter><save-entry><kill-line>$HOME/.quicky<enter><shell-escape>quicky.rb $HOME/.quicky<enter><enter-command>set wait_key<enter>" "Open with quicklook"
#
require 'tmpdir'
if ARGV.empty?
puts "need a filename"
exit 1
end
file = ARGV[0]
if ! File.exists? file
puts "Canny find #{file}"
exit 2
end
sowhatisit = %x{ file -I "#{file}" }.chomp
# Now regexp out:
# .quicky: text/html; charset=us-ascii
# Desktop/Screen Shot 2013-03-24 at 21.32.57.png: image/png; charset=binary
regex = /(?<filename>[\w\/\.-]+):\s(?<class>\w+)\/(?<type>\w+);\scharset=(?<charset>[\w\-]+)/
m = sowhatisit.match regex
# lazy?
if m.nil?
# This probably means the filename has something weird in it.
require 'rubygems'
require 'awesome_print'
ap sowhatisit
raise
end
Dir.mktmpdir do |dir|
newfilename = "#{dir}/#{File.basename file}.#{m[:type]}"
# use the directory...
FileUtils.cp file, newfilename
system %{qlmanage -p "#{newfilename}" >/dev/null}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment