Skip to content

Commit

Permalink
keys/views: Add mapping of contexts to broader contexts they belong to.
Browse files Browse the repository at this point in the history
Update the contextual help menu to use the parent contexts as well.
Add linting for parent contexts mapping.
  • Loading branch information
Niloth-p committed Jul 18, 2024
1 parent aa23737 commit 016ddf0
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 1 deletion.
34 changes: 34 additions & 0 deletions tools/lint-hotkeys
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ from zulipterminal.config.keys import (
HELP_CATEGORIES,
HELP_CONTEXTS,
KEY_BINDINGS,
PARENT_CONTEXTS,
display_keys_for_command,
)

Expand Down Expand Up @@ -93,6 +94,7 @@ def lint_hotkeys_file() -> None:
error_flag |= lint_help_groups("category")
error_flag |= lint_help_groups("context")
error_flag |= lint_help_text()
error_flag |= lint_parent_contexts()

if error_flag:
print(f"Rerun this command after resolving errors in config/{KEYS_FILE_NAME}")
Expand Down Expand Up @@ -178,6 +180,38 @@ def lint_help_groups(group: Group) -> bool:
return error_flag


def lint_parent_contexts() -> bool:
"""
Lint for any typos in the PARENT_CONTEXTS dict
"""
key_typos = []
value_typos = []

for key, value_list in PARENT_CONTEXTS.items():
if key not in HELP_CONTEXTS:
key_typos.append(key)
for value in value_list:
if value not in HELP_CONTEXTS:
value_typos.append(value)

error_message = ""
if key_typos:
error_message += (
f"Invalid contexts in parent context keys: {', '.join(key_typos)}.\n"
)
if value_typos:
error_message += (
f"Invalid contexts in parent context values: {', '.join(value_typos)}.\n"
)

if error_message:
error_message += f" Choose a context from:\n{', '.join(HELP_CONTEXTS.keys())}\n"
print(error_message)
return True

return False


def generate_hotkeys_file(group: Group) -> None:
"""
Generate output file based on help text description and
Expand Down
18 changes: 18 additions & 0 deletions zulipterminal/config/keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -563,6 +563,24 @@ class KeyBinding(TypedDict):
"search_box": "Search box",
}

PARENT_CONTEXTS: Dict[str, List[str]] = {
"global": [],
"general": ["global"],
"editor": ["global"],
"compose_box": ["editor", "global"],
"stream": ["general", "global", "button"],
"topic": ["general", "global", "button"],
"user": ["general", "global", "button"],
"message": ["general", "global"],
"stream_info": ["global", "popup"],
"msg_info": ["global", "popup"],
"emoji_list": ["global", "popup"],
"about": ["global", "popup"],
"search_box": ["global", "editor"],
"popup": ["global"],
"button": ["global"],
}

ZT_TO_URWID_CMD_MAPPING = {
"GO_UP": CURSOR_UP,
"GO_DOWN": CURSOR_DOWN,
Expand Down
8 changes: 7 additions & 1 deletion zulipterminal/ui_tools/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from zulipterminal.config.keys import (
HELP_CATEGORIES,
KEY_BINDINGS,
PARENT_CONTEXTS,
display_key_for_urwid_key,
display_keys_for_command,
is_command_key,
Expand Down Expand Up @@ -1240,12 +1241,17 @@ def __init__(
self, controller: Any, title: str, context: Optional[str] = None
) -> None:
help_menu_content = []
if context:
valid_contexts = PARENT_CONTEXTS[context] + [context]
for category in HELP_CATEGORIES:
keys_in_category = (
binding
for binding in KEY_BINDINGS.values()
if binding["key_category"] == category
and (not context or context in binding["key_contexts"])
and (
not context
or bool(set(binding["key_contexts"]) & set(valid_contexts))
)
)
key_bindings = [
(
Expand Down

0 comments on commit 016ddf0

Please sign in to comment.