Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- """
- https://askubuntu.com/questions/625761/how-to-change-displays-position-from-command-line
- """
- import re
- import subprocess
- import json
- from pathlib import Path
- xrandr_reg = re.compile(r"^([\w-]+).+?(\d+x\d+)\+(\d+\+\d+)")
- config_file = Path.home().joinpath(".config/xrandr_config.json")
- def get_config():
- proc = subprocess.run(["xrandr", "-q"], capture_output=True, encoding="utf8")
- modes = []
- for line in proc.stdout.splitlines():
- if match := xrandr_reg.search(line):
- port, mode, pos = match.groups()
- pos = pos.replace("+", "x")
- modes.append((port, mode, pos))
- return modes
- def get_saved_config():
- if config_file.exists():
- with config_file.open() as fd:
- return json.load(fd)
- else:
- with config_file.open("w") as fd:
- config = get_config()
- json.dump(config, fd)
- return config
- def set_modes(config):
- for port, mode, pos in config:
- cmd = ["xrandr", "--output", port, "--mode", mode, "--pos", pos]
- subprocess.run(cmd)
- if __name__ == "__main__":
- config = get_saved_config()
- set_modes(config
Add Comment
Please, Sign In to add comment