diff options
Diffstat (limited to 'common/bin/random-wallpaper')
-rwxr-xr-x | common/bin/random-wallpaper | 72 |
1 files changed, 72 insertions, 0 deletions
diff --git a/common/bin/random-wallpaper b/common/bin/random-wallpaper new file mode 100755 index 0000000..2261ca1 --- /dev/null +++ b/common/bin/random-wallpaper @@ -0,0 +1,72 @@ +#!/usr/bin/ruby + +$sw, $sh = `xrandr --screen 0` + .split("\n")[0] + .sub(/^.+current (\d+) x (\d+).+$/, "\\1 \\2") + .split(" ") + .map(&:to_i) + +$dims = {} +def image_dimensions img + unless $dims[img] + iw, ih = `identify -format "%w %h" "#{img}"` + .split(" ") + .map(&:to_i) + $dims[img] = [iw, ih] + end + $dims[img] +end + +def image_fits? img + iw, ih = image_dimensions img + (iw >= $sw && ih >= $sh) && + ((iw > ih && $sw > $sh) || + (ih > iw && $sh > $sw)) +end + +def better_fit? new, old + oiw, oih = image_dimensions old + niw, nih = image_dimensions new + if ($sh - nih).abs < ($sh - oih).abs || + ($sw - niw).abs < ($sw - oiw).abs + new + else + old + end +end + +def get_appropriate_version img + upscale = img.sub(/\.(\w+)/, "_upscale.\\1") + if !image_fits?(img) && File.exist?(upscale) + return upscale + end + crop = img.sub(/\.(\w+)/, "_crop.\\1") + if File.exist?(crop) && better_fit?(img, crop) + return get_appropriate_version crop + end + return img +end + +# def main +# walldir = ARGV[0] || File.expand_path("~/img/wallpapers") +# Dir[File.join(walldir, "**/*")] +# .select {|f| File.file? f } +# .reject {|img| img =~ /_(upscale|crop)\./ } +# .shuffle +# .find do |img| +# i = get_appropriate_version img +# if image_fits? i +# puts i +# exit +# end +# end +# end + +def main + walldir = ARGV[0] || File.expand_path("~/img/wallpapers") + Dir[File.join(walldir, "**/*")] + .select {|f| File.file? f } + .each {|img| puts "#{img}\t#{image_fits? img}" } +end + +main if __FILE__ == $0 |