Skip to content

Commit

Permalink
feat: Allow more flexible modified key syntax
Browse files Browse the repository at this point in the history
Allows either a hyphen or a pipe as a delimiter between modifiers and key, and allows arbitrary amount of spaces between modifiers
  • Loading branch information
al-ce committed Apr 14, 2023
1 parent 9cdb948 commit 6307267
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
4 changes: 2 additions & 2 deletions karaml/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@ def invalidTotalParensInMods(mod_string: str, opt_mod_matches: list):
f"For opt mods: {opt_mod_matches}\n"
"Too many sets of parens for optional mods. Use a single set.\n"
"Put the optional mods entirely to the left or right.\n"
"e.g. <c(oms)> or <c(oms)>\n"
"NOT: <c(o)(m)s> or <(o)(m)s> or <c(om)s> etc."
"e.g. c(oms) or (c)oms or <c(oms)> or <(c)oms>\n"
"NOT: c(o)(m)s or (o)(m)s or <c(om)s> etc."
)


Expand Down
24 changes: 18 additions & 6 deletions karaml/map_translator.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
get_multi_keys, validate_mod_aliases, validate_optional_mod_sets,
validate_sticky_mod_value, validate_sticky_modifier, validate_var_value,
validate_shnotify_dict, validate_mouse_pos_args,
check_and_validate_str_as_dict
check_and_validate_str_as_dict,
)
from karaml.exceptions import invalidKey, invalidSoftFunct
from karaml.key_codes import KEY_CODE_REF_LISTS, MODIFIERS, PSEUDO_FUNCS
Expand Down Expand Up @@ -169,7 +169,7 @@ def translate_pseudo_func(event: str, cmd: str) -> tuple[str, str]:
event, cmd = "mouse_key", mouse_key(cmd)
case "mousePos":
event, cmd = "software_function", \
{"set_mouse_cursor_position": mouse_pos(cmd)}
{"set_mouse_cursor_position": mouse_pos(cmd)}
case "notify":
event, cmd = "set_notification_message", notification(cmd)
case "notifyOff":
Expand Down Expand Up @@ -367,11 +367,23 @@ def parse_primary_key_and_mods(usr_key: str, usr_map) -> tuple[str, dict]:
return usr_key, {}


def is_modded_key(string: str) -> ModifiedKey:
expr = "<(.+)-(.+)>"
def is_modded_key(mapping: str) -> ModifiedKey:
"""
Check if a mapping matches a valid syntax for a modified key. If so,
return a ModifiedKey object with the key and modifiers. Otherwise,
return None.
A valid syntax must have a delimiter betwwen modifiers and the key (either
a hyphen or a pipe). The modifiers may be enclosed in angle brackets.
"""

expression = r"<?([^|>-]+)[|-]([^>]+)>?"
if query := search(expression, mapping):
modifiers, key = query.groups()
modifiers = modifiers.replace(" ", "")
key = key.strip()
return ModifiedKey(modifiers, key)

if query := search(expr, string):
return ModifiedKey(*query.groups())


def get_modifiers(usr_mods: str, usr_map: str) -> dict:
Expand Down

0 comments on commit 6307267

Please sign in to comment.