#!/usr/bin/env python3 ''' monitor-menu.py Usage: monitor-menu.py monitor-menu.py ''' from os.path import expanduser import dialog import docopt import json import subprocess d = dialog.Dialog() class MonitorMenu(): def __init__(self, config_file='~/.config/monitor-profiles.json'): with open(expanduser(config_file)) as fd: self.profiles = json.load(fd) def run(self, profile_idx=None): choices = [] if profile_idx is None: i = 0 for p in self.profiles: choices.append((str(i), p['name'])) i += 1 code, profile_idx = d.menu('Select the profile you want to use.', choices=choices) if code == d.ESC or code == d.CANCEL: return profile = self.profiles[int(profile_idx)] # We build the command line starting from just "xrandr" and adding arguments. xrandr_cmd = ['xrandr'] feh_cmd = ['feh', '--bg-fill'] try: xrandr_cmd += profile['xrandr-opts'] except KeyError: pass for monitor in profile['monitors']: xrandr_cmd.extend(monitor['xrandr-opts']) if monitor['background'] is not None: feh_cmd.append(monitor['background']); subprocess.run(xrandr_cmd) subprocess.run(feh_cmd) def main(argv=None): args = docopt.docopt(__doc__, version='1', argv=argv) menu = MonitorMenu() try: if '' in args and args[''] is not None: menu.run(int(args[''])) else: menu.run() except ValueError as e: raise docopt.DocoptExit(str(e)) if __name__ == '__main__': main()