86 lines
2.3 KiB
Python
86 lines
2.3 KiB
Python
#!/usr/bin/env python3
|
|
import os
|
|
import subprocess
|
|
|
|
# Define the list of workspaces to cycle through
|
|
WORKSPACES = [3, 6, 7, 8]
|
|
|
|
LAST_WS_FILE = "/tmp/lastws"
|
|
|
|
|
|
def get_current_workspace():
|
|
result = subprocess.run(["wmctrl", "-d"], capture_output=True, text=True)
|
|
lines = result.stdout.splitlines()
|
|
|
|
for line in lines:
|
|
if "*" in line: # The active workspace has a '*' symbol
|
|
try:
|
|
current_workspace = int(line.split()[0]) + 1
|
|
print(f"Current workspace detected: {current_workspace}")
|
|
return current_workspace
|
|
except (IndexError, ValueError):
|
|
print("Error parsing current workspace.")
|
|
return None
|
|
|
|
print("No active workspace found.")
|
|
return None
|
|
|
|
|
|
def switch_to_workspace(workspace):
|
|
print(f"Switching to workspace {workspace}")
|
|
subprocess.run(["wmctrl", "-s", str(workspace - 1)])
|
|
save_last_used_workspace(workspace)
|
|
|
|
|
|
def save_last_used_workspace(workspace):
|
|
with open(LAST_WS_FILE, "w") as f:
|
|
f.write(str(workspace))
|
|
|
|
|
|
def get_last_used_workspace():
|
|
if os.path.exists(LAST_WS_FILE):
|
|
with open(LAST_WS_FILE, "r") as f:
|
|
try:
|
|
return int(f.read().strip())
|
|
except ValueError:
|
|
return WORKSPACES[0]
|
|
return WORKSPACES[0]
|
|
|
|
|
|
if __name__ == "__main__":
|
|
import sys
|
|
|
|
if len(sys.argv) < 2 or sys.argv[1] not in ["up", "down", "current"]:
|
|
print("Usage: python cyclework.py [up|down|current]")
|
|
sys.exit(1)
|
|
|
|
direction = sys.argv[1]
|
|
current = get_current_workspace()
|
|
|
|
if current is None:
|
|
sys.exit(1)
|
|
|
|
if direction == "current":
|
|
if current in WORKSPACES:
|
|
print("Already in a workspace in the cycle.")
|
|
sys.exit(0)
|
|
|
|
last_used = get_last_used_workspace()
|
|
print(f"Switching to last used workspace in cycle: {last_used}")
|
|
switch_to_workspace(last_used)
|
|
sys.exit(0)
|
|
|
|
try:
|
|
index = WORKSPACES.index(current)
|
|
except ValueError:
|
|
print("Current workspace is not in the list.")
|
|
sys.exit(1)
|
|
|
|
if direction == "up":
|
|
index = (index + 1) % len(WORKSPACES)
|
|
else: # direction == 'down'
|
|
index = (index - 1) % len(WORKSPACES)
|
|
|
|
next_workspace = WORKSPACES[index]
|
|
switch_to_workspace(next_workspace)
|