Permalink
Browse files

randomline: choose a random line from the input. Useful for things

            like choosing a random wallpaper or a random beep sound.
randombg:   Set a random wallpaper, choosing from a directory named
            according to the current X resolution (or 1st argument).
  • Loading branch information...
1 parent a5293a3 commit a21017ab8b7739b5a6b817a6cd7332e09792bedb @akkana committed Sep 22, 2012
Showing with 93 additions and 0 deletions.
  1. +67 −0 randombg
  2. +26 −0 randomline
View
@@ -0,0 +1,67 @@
+#! /usr/bin/env python
+
+# Pick a new random background
+# usage: newbg [directory]
+# Default directory is ~/Images/Backgrounds/$dpy_width
+
+import sys, os, subprocess
+import random
+
+def set_random_bg(bgdir, arg) :
+ images = os.listdir(bgdir)
+ while len(images) > 0 :
+ img = random.choice(images)
+ if img.find('.') :
+ print "Setting background to", os.path.join(bgdir, img)
+ subprocess.call(['hsetroot', arg, os.path.join(bgdir, img)])
+ sys.exit(0)
+ print "No images in", bgdir
+ sys.exit(1)
+
+# If the dir is specified explicitly, go ahead and do it.
+if len(sys.argv) > 1 :
+ set_random_bg(sys.argv[1], '-fill')
+
+# Find the current resolution:
+fp = os.popen("xdpyinfo")
+while True :
+ line = fp.readline().strip()
+ if line[0:11] == 'dimensions:' :
+ parts = line[12:].split()[0].split('x')
+ width = int(parts[0])
+ height = int(parts[1])
+ break
+fp.close()
+if not width or not height :
+ print "Couldn't find screen dimensions!"
+ sys.exit(1)
+
+basedir = os.path.expanduser("~/Images/Backgrounds")
+bgdir = os.path.join(basedir, '%dx%d' % (width, height))
+
+# If the exact right directory exists, use it.
+if os.path.exists(bgdir) :
+ print bgdir, "exists: using it"
+ set_random_bg(bgdir, '-fill')
+
+# We don't have images at exactly the right resolution.
+# Try to find the closest match:
+print "Looking for the closest match"
+mindiff = 99999
+bgdir = None
+for d in os.listdir(basedir) :
+ try :
+ parts = d.split('x')
+ if len(parts) == 2 :
+ w = int(parts[0])
+ h = int(parts[1])
+ diff = abs(w - width) + abs(h - height)
+ if diff < mindiff :
+ mindiff = diff
+ bgdir = os.path.join(basedir, d)
+ except :
+ continue
+
+print "Will use", bgdir
+set_random_bg(bgdir, '-fill')
+
View
@@ -0,0 +1,26 @@
+#!/usr/bin/env python
+
+# randomline: echo one line, randomly chosen, from stdin.
+# Copyright 2007 Akkana Peck, GPL blah blah, free to reuse blah blah
+#
+# Thanks to saz and http://www.goldfish.org/books/O'Reilly%20Perl%20CD%20Bookshelf%202.0/cookbook/ch08_07.htm
+# for the cute algorithm.
+
+import sys, string, random
+
+# main
+
+random.seed()
+num_lines = 0
+picked = ""
+while 1 :
+ line = sys.stdin.readline()
+ if not line : break
+ line = string.strip(line, " \n")
+ if line == '' : continue
+ num_lines += 1
+ if random.randint(1, num_lines) == 1 :
+ picked = line
+
+print picked
+

0 comments on commit a21017a

Please sign in to comment.