Skip to content

Instantly share code, notes, and snippets.

@hariseldon78
Created October 7, 2023 10:48
Show Gist options
  • Save hariseldon78/2fb20d04036cad8969152f379d9b70f5 to your computer and use it in GitHub Desktop.
Save hariseldon78/2fb20d04036cad8969152f379d9b70f5 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import subprocess
import sys
import json
def get_input_from_rofi():
rofi = subprocess.run(
["rofi", "-dmenu", "-p", "Workspace Name"], stdout=subprocess.PIPE, text=True
)
return rofi.stdout.strip()
# please change this code to insert the workspace in alphabetical order
def create_or_switch_workspace(name, output):
print("Creating workspace " + name + " on " + output)
subprocess.run(["hyprctl", "dispatch", "focusmonitor", output])
subprocess.run(
["hyprctl", "dispatch", "workspace", "name:" + name],
stdout=subprocess.PIPE,
text=True,
)
activewsraw = subprocess.run(
["hyprctl", "activeworkspace", "-j"], stdout=subprocess.PIPE, text=True
).stdout.strip()
activews = json.loads(activewsraw)
if not activews["persistent"]:
subprocess.run(
["hyprctl", "dispatch", "workspaceopt", "persistent"],
stdout=subprocess.PIPE,
text=True,
)
def get_current_monitor():
hyprctl = subprocess.run(
["hyprctl", "monitors", "-j"], stdout=subprocess.PIPE, text=True
)
raw = hyprctl.stdout.strip()
list = json.loads(raw)
current = next((item for item in list if item["focused"]), None)
return current["name"]
def main():
if len(sys.argv) > 2:
workspace_name = sys.argv[2]
else:
workspace_name = get_input_from_rofi()
if workspace_name == "":
return
if len(sys.argv) > 1:
direction = sys.argv[1]
if direction == "right":
monitor = "HDMI-A-1"
elif direction == "center":
monitor = "DP-2"
elif direction == "left":
monitor = "DP-3"
else:
monitor = get_current_monitor()
if monitor == "HDMI-A-1":
suffix = "_"
elif monitor == "DP-2":
suffix = ""
elif monitor == "DP-3":
suffix = "+"
create_or_switch_workspace(workspace_name + suffix, monitor)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment