laptop
Elias Almqvist 1 year ago
parent 082f6781c8
commit 1bedea401e
No known key found for this signature in database
GPG Key ID: E31A99CE3E75A158
  1. 64
      eww/.config/eww/scripts/workspace.py

@ -14,27 +14,17 @@ from subprocess import check_output, CalledProcessError
HIDE_EMPTY_WS = True # Exclude empty workspaces HIDE_EMPTY_WS = True # Exclude empty workspaces
OVERIDE_ALL_NAMES = False # Override all the WS names and use the name below OVERIDE_ALL_NAMES = False # Override all the WS names and use the name below
OVERIDE_ALL_NAME = "" # Name to use when overriding names OVERIDE_ALL_NAME = "" # Name to use when overriding names
NAME_MAPS: dict[int, str] = { # Replace the indexs name with the given string NAME_MAPS: dict[int, str] = {
# 0: "", # 0: "",
# 1: "" # 1: ""
} }
LOG_FILE = "/tmp/eww_workspace.log" # Log file (in case of errors etc) LOG_FILE = "/tmp/eww_workspace.log" # Log file (in case of errors etc)
# Compile regex patterns
# ------------ NOTE: DO NOT TOUCH ------------ PARSE_REGEX = re.compile(r"^(\d+)\s+(\*|\-).+\s+(\w+)$")
WMCTRL_SCRIPT = "wmctrl -d" WMCTRL_SCRIPT = "wmctrl -d"
PARSE_REGEX = r"^(\d+)\s+(\*|\-).+\s+(\w+)$" NONEMPTY_SCRIPT = "wmctrl -l"
NONEMPTY_REGEX = r"^\w+\s+(\w+|\d+).+$"
WMCTRL_NONEMPTY_SCRIPT = "wmctrl -l"
def parse_groups(grps: tuple) -> tuple:
"""
Parses the given regex groups
"""
return int(grps[0]), grps[2], grps[1] == "*"
class Workspace: class Workspace:
@ -46,7 +36,7 @@ class Workspace:
self.index = index self.index = index
if OVERIDE_ALL_NAMES: if OVERIDE_ALL_NAMES:
self.name = OVERIDE_ALL_NAME self.name = OVERIDE_ALL_NAME
elif index in NAME_MAPS.keys(): elif index in NAME_MAPS:
self.name = NAME_MAPS[index] self.name = NAME_MAPS[index]
else: else:
self.name = name self.name = name
@ -60,28 +50,40 @@ class Workspace:
return json.dumps(self, default=lambda o: o.__dict__, sort_keys=True) return json.dumps(self, default=lambda o: o.__dict__, sort_keys=True)
def parse_groups(grps: tuple) -> tuple:
"""
Parses the given regex groups
"""
return int(grps[0]), grps[2], grps[1] == "*"
def parse_workspaces(): def parse_workspaces():
""" """
Function to get all the workspaces Function to get all the workspaces
""" """
nonempty_workspaces = set()
try: try:
nonempty = check_output(WMCTRL_NONEMPTY_SCRIPT.split(" ")) nonempty = check_output(NONEMPTY_SCRIPT.split(" "))
nonempty = nonempty.decode("utf-8") nonempty = nonempty.decode("utf-8")
nonempty_workspaces = re.findall(NONEMPTY_REGEX, for line in nonempty.splitlines():
nonempty, try:
flags=re.MULTILINE) ws_num = line[12]
nonempty_workspaces = list(map(int, nonempty_workspaces)) nonempty_workspaces.add(int(ws_num))
except:
continue
except CalledProcessError: except CalledProcessError:
nonempty_workspaces = [] pass
try: try:
inp = check_output(WMCTRL_SCRIPT.split(" ")) inp = check_output(WMCTRL_SCRIPT.split(" "))
inp = inp.decode("utf-8") inp = inp.decode("utf-8")
except CalledProcessError as err: except CalledProcessError as err:
logfile = open(LOG_FILE, "a") with open(LOG_FILE, "a") as logfile:
logfile.write("ERROR! COULD NOT PARSE WORKSPACES OR SOMETHING! YOU ARE ON YOUR OWN.") logfile.write(
"ERROR! COULD NOT PARSE WORKSPACES OR SOMETHING! YOU ARE ON YOUR OWN."
)
logfile.write(f"{err}") logfile.write(f"{err}")
inp = "" inp = ""
@ -89,28 +91,26 @@ def parse_workspaces():
out = [] out = []
for line in inp.splitlines(): for line in inp.splitlines():
matches = re.search(PARSE_REGEX, line) matches = PARSE_REGEX.search(line)
if matches:
grps = matches.groups() grps = matches.groups()
data = parse_groups(grps) data = parse_groups(grps)
workspace = Workspace(*data, data[0] not in nonempty_workspaces) workspace = Workspace(*data, data[0] not in nonempty_workspaces)
out.append(workspace) out.append(workspace)
return out return out
if __name__ == "__main__": if __name__ == "__main__":
args = sys.argv
# NOTE: use deflisten # NOTE: use deflisten
LAST_STRING = "" LAST_STRING = ""
while True: while True:
workspaces = parse_workspaces() workspaces = parse_workspaces()
workspaces = list(filter(lambda ws: workspaces = [
(ws.iscurrent) ws
or (not ws.isempty) for ws in workspaces
or (not HIDE_EMPTY_WS), if ws.iscurrent or (not ws.isempty) or (not HIDE_EMPTY_WS)
workspaces)) ]
json_str = json.dumps(workspaces, default=lambda ws: ws.to_json()) json_str = json.dumps(workspaces, default=lambda ws: ws.to_json())

Loading…
Cancel
Save