-
-
Save tomjnixon/756408 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env python2 | |
import sys | |
import shelve | |
import subprocess | |
import os | |
import pickle | |
def no_spaces(line): | |
return len(line) - len(line.lstrip()) | |
def get_block(lines, line_no): | |
max_spaces = no_spaces(lines[line_no]) | |
next_max = [n for n, l in list(enumerate(lines))[line_no + 1:] if no_spaces(l) <= max_spaces][0] | |
return lines[line_no + 1 : next_max] | |
def get_screen_id(): | |
xr = subprocess.Popen(["xrandr", "--prop"], stdout=subprocess.PIPE) | |
lines = xr.communicate()[0].replace('\t', ' ').split('\n') | |
vga_no, vga_line = [(n, l) for n,l in enumerate(lines) if "VGA1" in l][0] | |
if vga_line.split()[1] == "connected": | |
return ''.join(map(str.strip,get_block(lines, vga_no + 1))) | |
def call_xrandr(args): | |
subprocess.check_call(["xrandr"] + args) | |
def main(): | |
screen_data = shelve.open(os.path.expanduser("~/.screen_data")) | |
if "--restore" in sys.argv: | |
screen_id = get_screen_id() | |
if screen_data[pickle.dumps("last")] is not None and screen_id is not None: | |
call_xrandr(screen_data[pickle.dumps(None)]) | |
call_xrandr(screen_data[pickle.dumps(screen_id)]) | |
screen_data[pickle.dumps("last")] = screen_id | |
elif "--save" in sys.argv: | |
sys.argv.remove("--save") | |
screen_id = get_screen_id() | |
screen_data[pickle.dumps(screen_id)] = sys.argv[1:] | |
else: | |
call_xrandr(sys.argv[1:]) | |
screen_data.close() | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment