Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 67 additions & 17 deletions Configs/.local/lib/hyde/keybinds/hint-hyprland.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,78 @@
import time


def parse_bool(value):
"""Parse boolean values accepting both string ('true'/'false') and numeric (1/0) forms."""
return value.lower() in {"1", "true"}


def get_hyprctl_binds():
while True:
try:
result = subprocess.run(
["hyprctl", "binds", "-j"], capture_output=True, text=True, check=True, timeout=5
)
try:
result = subprocess.run(
["hyprctl", "binds", "-j"], capture_output=True, text=True, check=True
)
while result.returncode != 0:
print("Waiting for hyprctl command to succeed...")
time.sleep(1)
result = subprocess.run(
["hyprctl", "binds", "-j"],
capture_output=True,
text=True,
check=True,
)
binds = json.loads(result.stdout)
return binds
except subprocess.CalledProcessError as e:
print(f"Error executing hyprctl: {e}")
return None
except json.JSONDecodeError:
time.sleep(1)
pass
except (subprocess.CalledProcessError, subprocess.TimeoutExpired):
pass

try:
result = subprocess.run(
["hyprctl", "binds"], capture_output=True, text=True, check=True, timeout=5
)
binds = []
current = {}
for line in result.stdout.strip().split("\n"):
line = line.strip()
if line.startswith("bind"):
if current:
binds.append(current)
current = {}
elif ":" in line:

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: this fallback is parsing a schema, rather than expressing distinct control flow. A field-to-parser mapping would make new Hyprland fields a one-line addition and, with explicit defaults, ensure partially parsed binds cannot later fail with missing keys in expand_meta_data.

BIND_DEFAULTS = {
    "modmask": 0,
    "submap": "",
    "key": "",
    "keycode": 0,
    "catch_all": False,
    "description": "",
    "has_description": False,
    "dispatcher": "",
    "arg": "",
    "repeat": False,
    "locked": False,
    "mouse": False,
    "release": False,
    "non_consuming": False,
    "allow_input_capture": False,
}

parse_bool = lambda value: value.lower() == "true"
parse_int = lambda value: int(value) if value.isdigit() else 0
FIELD_PARSERS = {
    "modmask": ("modmask", parse_int),
    "submap": ("submap", str),
    "key": ("key", str),
    "keycode": ("keycode", parse_int),
    "catchall": ("catch_all", parse_bool),
    "description": ("description", str),
    "dispatcher": ("dispatcher", str),
    "arg": ("arg", str),
    "repeat": ("repeat", parse_bool),
    "locked": ("locked", parse_bool),
    "mouse": ("mouse", parse_bool),
    "release": ("release", parse_bool),
    "non_consuming": ("non_consuming", parse_bool),
    "allow_input_capture": ("allow_input_capture", parse_bool),
}

# `startswith` also accommodates other Hyprland bind variants.
if line.startswith("bind"):
    if current:
        current["has_description"] = bool(current["description"])
        binds.append(current)
    current = BIND_DEFAULTS.copy()
elif ":" in line:
    key, _, value = line.partition(":")
    field = FIELD_PARSERS.get(key.strip())
    if field:
        destination, parser = field
        current[destination] = parser(value.strip())

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make sense, adjusted 😄

key, _, value = line.partition(":")
key = key.strip()
value = value.strip()
if key == "modmask":
current["modmask"] = int(value) if value.isdigit() else 0
elif key == "submap":
current["submap"] = value
elif key == "key":
current["key"] = value
elif key == "keycode":
try:
current["keycode"] = int(value)
except ValueError:
current["keycode"] = 0
elif key == "catchall":
current["catch_all"] = parse_bool(value)
elif key == "description":
current["description"] = value
current["has_description"] = bool(value)
elif key == "dispatcher":
current["dispatcher"] = value
elif key == "arg":
current["arg"] = value
elif key == "repeat":
current["repeat"] = parse_bool(value)
elif key == "locked":
current["locked"] = parse_bool(value)
elif key == "mouse":
current["mouse"] = parse_bool(value)
elif key == "release":
current["release"] = parse_bool(value)
elif key == "non_consuming":
current["non_consuming"] = parse_bool(value)
elif key == "allow_input_capture":
current["allow_input_capture"] = parse_bool(value)
if current:
binds.append(current)
return binds if binds else None
except (subprocess.CalledProcessError, subprocess.TimeoutExpired) as e:
print(f"Error executing hyprctl: {e}")
return None


def parse_description(description):
Expand Down
4 changes: 2 additions & 2 deletions Configs/.local/lib/hyde/keybinds_hint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ dispatch=$(awk -F ':::' '{print $2}' <<< "$selected" | xargs)
arg=$(awk -F ':::' '{print $3}' <<< "$selected" | xargs)
repeat=$(awk -F ':::' '{print $4}' <<< "$selected" | xargs)
RUN() {
case "$(eval "hyprctl dispatch '$dispatch' '$arg'")" in *"Not enough arguments"*) exec $0 ;; esac
case "$(eval "hyprctl dispatch '$dispatch' '$arg'")" in *"Not enough arguments"*) exit 0 ;; esac
}
if [ -n "$dispatch" ] && [ "$(echo "$dispatch" | wc -l)" -eq 1 ]; then
if [ "$repeat" = repeat ]; then
Expand All @@ -74,5 +74,5 @@ if [ -n "$dispatch" ] && [ "$(echo "$dispatch" | wc -l)" -eq 1 ]; then
RUN
fi
else
exec $0
exit 0
fi
Loading