Skip to content

Instantly share code, notes, and snippets.

@Ayehavgunne
Created October 12, 2024 06:41
Show Gist options
  • Save Ayehavgunne/3ca568038fe4783bf87dda4186f47e3a to your computer and use it in GitHub Desktop.
Save Ayehavgunne/3ca568038fe4783bf87dda4186f47e3a to your computer and use it in GitHub Desktop.
Hyprland keybinds cheatsheet using Rofi (dmenu)
#!/usr/bin/env python3.12
import json
from dataclasses import dataclass
from subprocess import PIPE, Popen
SUPER = 64
ALT = 8
CTRL = 4
SHIFT = 1
@dataclass
class Response:
return_code: int
std_out: str
std_err: str
def send_command(cmd: str) -> Response:
shell = Popen(cmd, stdin=PIPE, stdout=PIPE, stderr=PIPE, shell=True)
std_out, std_err = shell.communicate()
return Response(
shell.returncode, std_out.decode().strip(), std_err.decode().strip()
)
def get_keys_from_mask(mask: int) -> list[str]:
modifiers = []
if mask >= SUPER:
mask -= SUPER
modifiers.append("SUPER")
if mask >= ALT:
mask -= ALT
modifiers.append("Alt")
if mask >= CTRL:
mask -= CTRL
modifiers.append("Ctrl")
if mask == SHIFT:
modifiers.append("Shift")
return modifiers
def main() -> None:
binds = json.loads(send_command("hyprctl binds -j").std_out)
binds_map = {}
for binding in binds:
if binding["description"]:
line_input = get_keys_from_mask(binding["modmask"])
line_input.append(binding["key"])
line_input = "+".join(line_input)
binds_map[f"{line_input} - {binding['description']}"] = binding
rofi_input = "\n".join(binds_map.keys())
choice = send_command(f"echo '{rofi_input}' | rofi -dmenu").std_out
if choice:
binding = binds_map[choice]
command = f"{binding["dispatcher"]} {binding["arg"]}"
send_command(f"hyprctl dispatch -- {command}")
if __name__ == "__main__":
main()
@Ayehavgunne
Copy link
Author

Ayehavgunne commented Oct 12, 2024

This script checks for binds that have a description and only displays those. It could be altered easily to behave differently if so desired.
Any command selected with rofi will be sent to hyprctl dispatch to be executed.

As far as I know there is only one problem when trying to execute some commands using this script. If the command being executed tries to use or logic (two pipes ||) then hyprctl doesn't execute the command correctly. Not sure why that is or if there is something I must do differently. I suspect the same would be true of and logic (two ampersands &&)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment