From 91353d5951957341842347f6e11c6177ee8e60e7 Mon Sep 17 00:00:00 2001 From: Roberto Alsina Date: Wed, 5 Feb 2020 16:58:08 -0300 Subject: [PATCH] Parse a little more --- xrandroll/monitor.py | 31 +++++++++++++++++++++++++++---- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/xrandroll/monitor.py b/xrandroll/monitor.py index 0d2af2a..13fce06 100644 --- a/xrandroll/monitor.py +++ b/xrandroll/monitor.py @@ -2,6 +2,8 @@ import re +import parse + def _split_by_lines_matching(pattern, lines): """Return a list of groups of lines, splitting on lines @@ -29,15 +31,26 @@ class Field: class Mode: - """One of the modes for a monitor.""" + """One of the modes for a monitor. + + Note: + + mode.name is the hex thing, like "0x56", not "1920x1080" + """ def __init__(self, data): """Initialize Mode from xrandr data.""" self.data = data - self.name = data[0].strip().split()[0] + self.header = data[0] + self.name = parse.search("({mode_name})", self.header)["mode_name"] + self.res_x = parse.search("h: width {res_x:d}", data[1])["res_x"] + self.res_y = parse.search("v: height {res_y:d}", data[2])["res_y"] + self.refresh = parse.search("{refresh:f}Hz", data[2])["refresh"] + self.preferred = "+preferred" in self.header + self.current = "*current" in self.header def __repr__(self): - return self.data[0].strip() + return self.header.strip() class Monitor: @@ -49,7 +62,8 @@ class Monitor: data is a list of lines. """ - _ = data.pop(0) + self.header = data.pop(0) + self.pos_x, self.pos_y = parse.search("+{:d}+{:d}", self.header) modes_data = _split_by_lines_matching("^ [^ ]", data) fields_data = _split_by_lines_matching(r"^\t[^ ]", modes_data.pop(0)) @@ -60,3 +74,12 @@ class Monitor: self.fields = {} for f in (Field(d) for d in fields_data): self.fields[f.name] = f + + def get_current_mode_name(self): + for k, v in self.modes.values(): + if v.current: + return k + + def set_current_mode(self, mode_name): + for k, v in self.modes.items(): + v.current = k == mode_name