Skip to content

Instantly share code, notes, and snippets.

@SED4906
Last active October 14, 2025 15:51
Show Gist options
  • Select an option

  • Save SED4906/edfa3a3ddfc45e94fb22b4ff382967b0 to your computer and use it in GitHub Desktop.

Select an option

Save SED4906/edfa3a3ddfc45e94fb22b4ff382967b0 to your computer and use it in GitHub Desktop.
WScreenSaver helper script
#!/usr/bin/env python
import argparse
from pathlib import Path
import os
import random
import subprocess
import sys
def screensavers(args):
programs = Path(args.path).glob('xscreensaver-*')
programs = [str(x).rsplit('-')[-1] for x in programs]
filtered = []
try:
with Path(f'{sys.argv[0]}.broken').open() as broken:
filtered.extend(broken.read().splitlines())
except: print(f"{sys.argv[0]}.broken couldn't be read. Did you run `{sys.argv[0]} check` yet?")
try:
with Path(f'{sys.argv[0]}.disabled').open() as disabled:
filtered.extend(disabled.read().splitlines())
except: print(f"{sys.argv[0]}.disabled couldn't be read. It may not exist yet.")
result = list(set(programs) - set(filtered))
random.shuffle(result)
return result
def disable(args, name):
with Path(f'{sys.argv[0]}.disabled').open(mode='a') as disabled:
print(name, file=disabled)
def last(args):
try:
with Path(f'{sys.argv[0]}.history').open() as history:
last = history.read().splitlines().pop()
if last is not None:
print(last)
if args.disable: disable(args, last)
except: print(f"{sys.argv[0]}.history does not appear to exist yet.")
def run(args):
screensaver = screensavers(args).pop()
with Path(f'{sys.argv[0]}.history').open(mode='a') as history:
print(screensaver, file=history)
os.execv(f'{args.path}/xscreensaver-{screensaver}',[f'{args.path}/xscreensaver-{screensaver}'])
def check(args):
if args.recheck:
Path(f'{sys.argv[0]}.broken').open(mode='w').close()
screensavers_to_check = screensavers(args)
with Path(f'{sys.argv[0]}.broken').open(mode='a') as broken:
for screensaver in screensavers_to_check:
try:
print(screensaver, end=' ', flush=True)
status = subprocess.run([f'{args.path}/xscreensaver-{screensaver}'], timeout=1, check=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
except subprocess.TimeoutExpired:
print('ok')
continue
except KeyboardInterrupt:
print('cancelled')
break
except:
print(':(')
print(screensaver, file=broken)
def main():
parser = argparse.ArgumentParser(
description='Runs a screensaver from the WScreenSaver suite.',
)
parser.set_defaults(func=run)
subparsers = parser.add_subparsers()
parser_check = subparsers.add_parser('check')
parser_check.add_argument('--recheck', '-f', action='store_true')
parser_check.set_defaults(func=check)
parser_last = subparsers.add_parser('last')
parser_last.add_argument('--disable', '-d', action='store_true')
parser_last.set_defaults(func=last)
parser.add_argument('--path', '-p', default='/usr/bin')
args = parser.parse_args()
args.func(args)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment