Skip to content

Commit

Permalink
helper/model/ui/boxes/buttons/views: Refine UI display of hotkeys.
Browse files Browse the repository at this point in the history
Update all the files that display hotkeys to the user,
to use the new display functions for consistent output.

The internal usage (mainly for keypresses)
is left to use the default Urwid representation.

Tests updated.

Co-authored-by: Parth Shah <[email protected]>
  • Loading branch information
2 people authored and neiljp committed Apr 11, 2024
1 parent 48f32d1 commit a2e616e
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 36 deletions.
4 changes: 2 additions & 2 deletions tests/helper/test_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from pytest_mock import MockerFixture

from zulipterminal.api_types import Composition
from zulipterminal.config.keys import primary_key_for_command
from zulipterminal.config.keys import primary_display_key_for_command
from zulipterminal.helper import (
Index,
canonicalize_color,
Expand Down Expand Up @@ -469,7 +469,7 @@ def test_notify_if_message_sent_outside_narrow(
notify_if_message_sent_outside_narrow(req, controller)

if footer_updated:
key = primary_key_for_command("NARROW_MESSAGE_RECIPIENT")
key = primary_display_key_for_command("NARROW_MESSAGE_RECIPIENT")
report_success.assert_called_once_with(
[
"Message is sent outside of current narrow."
Expand Down
17 changes: 12 additions & 5 deletions tests/ui_tools/test_boxes.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@
TYPING_STARTED_WAIT_PERIOD,
TYPING_STOPPED_WAIT_PERIOD,
)
from zulipterminal.config.keys import keys_for_command, primary_key_for_command
from zulipterminal.config.keys import (
keys_for_command,
primary_display_key_for_command,
primary_key_for_command,
)
from zulipterminal.config.symbols import (
INVALID_MARKER,
STREAM_MARKER_PRIVATE,
Expand Down Expand Up @@ -379,9 +383,12 @@ def test_footer_notification_on_invalid_recipients(
expected_lines = [
"Invalid recipient(s) - " + invalid_recipients,
" - Use ",
("footer_contrast", primary_key_for_command("AUTOCOMPLETE")),
("footer_contrast", primary_display_key_for_command("AUTOCOMPLETE")),
" or ",
("footer_contrast", primary_key_for_command("AUTOCOMPLETE_REVERSE")),
(
"footer_contrast",
primary_display_key_for_command("AUTOCOMPLETE_REVERSE"),
),
" to autocomplete.",
]

Expand Down Expand Up @@ -1760,8 +1767,8 @@ class TestPanelSearchBox:

@pytest.fixture
def panel_search_box(self, mocker: MockerFixture) -> PanelSearchBox:
# X is the return from keys_for_command("UNTESTED_TOKEN")
mocker.patch(MODULE + ".keys_for_command", return_value="X")
# X is the return from display_keys_for_command("UNTESTED_TOKEN")
mocker.patch(MODULE + ".display_keys_for_command", return_value="X")
panel_view = mocker.Mock()
update_func = mocker.Mock()
return PanelSearchBox(panel_view, "UNTESTED_TOKEN", update_func)
Expand Down
4 changes: 2 additions & 2 deletions zulipterminal/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
from typing_extensions import Literal, ParamSpec, TypedDict

from zulipterminal.api_types import Composition, EmojiType, Message
from zulipterminal.config.keys import primary_key_for_command
from zulipterminal.config.keys import primary_display_key_for_command
from zulipterminal.config.regexes import (
REGEX_COLOR_3_DIGIT,
REGEX_COLOR_6_DIGIT,
Expand Down Expand Up @@ -700,7 +700,7 @@ def check_narrow_and_notify(
and current_narrow != outer_narrow
and current_narrow != inner_narrow
):
key = primary_key_for_command("NARROW_MESSAGE_RECIPIENT")
key = primary_display_key_for_command("NARROW_MESSAGE_RECIPIENT")

controller.report_success(
[
Expand Down
4 changes: 2 additions & 2 deletions zulipterminal/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
UpdateMessageContentEvent,
UpdateMessagesLocationEvent,
)
from zulipterminal.config.keys import primary_key_for_command
from zulipterminal.config.keys import primary_display_key_for_command
from zulipterminal.config.symbols import STREAM_TOPIC_SEPARATOR
from zulipterminal.config.ui_mappings import EDIT_TOPIC_POLICY, ROLE_BY_ID, STATE_ICON
from zulipterminal.helper import (
Expand Down Expand Up @@ -1671,7 +1671,7 @@ def _handle_message_event(self, event: Event) -> None:
"Press '{}' to close this window."
)
notice = notice_template.format(
failed_command, primary_key_for_command("GO_BACK")
failed_command, primary_display_key_for_command("GO_BACK")
)
self.controller.popup_with_message(notice, width=50)
self.controller.update_screen()
Expand Down
13 changes: 10 additions & 3 deletions zulipterminal/ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@

import urwid

from zulipterminal.config.keys import commands_for_random_tips, is_command_key
from zulipterminal.config.keys import (
commands_for_random_tips,
display_key_for_urwid_key,
is_command_key,
)
from zulipterminal.config.symbols import (
APPLICATION_TITLE_BAR_LINE,
AUTOHIDE_TAB_LEFT_ARROW,
Expand Down Expand Up @@ -102,10 +106,13 @@ def get_random_help(self) -> List[Any]:
if not allowed_commands:
return ["Help(?): "]
random_command = random.choice(allowed_commands)
random_command_display_keys = ", ".join(
[display_key_for_urwid_key(key) for key in random_command["keys"]]
)
return [
"Help(?): ",
("footer_contrast", " " + ", ".join(random_command["keys"]) + " "),
" " + random_command["help_text"],
("footer_contrast", f" {random_command_display_keys} "),
f" {random_command['help_text']}",
]

@asynch
Expand Down
21 changes: 14 additions & 7 deletions zulipterminal/ui_tools/boxes.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@

from zulipterminal.api_types import Composition, PrivateComposition, StreamComposition
from zulipterminal.config.keys import (
display_keys_for_command,
is_command_key,
keys_for_command,
primary_display_key_for_command,
primary_key_for_command,
)
from zulipterminal.config.regexes import (
Expand Down Expand Up @@ -302,11 +303,11 @@ def _tidy_valid_recipients_and_notify_invalid_ones(
invalid_recipients_error = [
"Invalid recipient(s) - " + ", ".join(invalid_recipients),
" - Use ",
("footer_contrast", primary_key_for_command("AUTOCOMPLETE")),
("footer_contrast", primary_display_key_for_command("AUTOCOMPLETE")),
" or ",
(
"footer_contrast",
primary_key_for_command("AUTOCOMPLETE_REVERSE"),
primary_display_key_for_command("AUTOCOMPLETE_REVERSE"),
),
" to autocomplete.",
]
Expand Down Expand Up @@ -850,8 +851,10 @@ def keypress(self, size: urwid_Size, key: str) -> Optional[str]:
invalid_stream_error = (
"Invalid stream name."
" Use {} or {} to autocomplete.".format(
primary_key_for_command("AUTOCOMPLETE"),
primary_key_for_command("AUTOCOMPLETE_REVERSE"),
primary_display_key_for_command("AUTOCOMPLETE"),
primary_display_key_for_command(
"AUTOCOMPLETE_REVERSE"
),
)
)
self.view.controller.report_error([invalid_stream_error])
Expand Down Expand Up @@ -918,7 +921,9 @@ def __init__(self, controller: Any) -> None:
super().__init__(self.main_view())

def main_view(self) -> Any:
search_text = f"Search [{', '.join(keys_for_command('SEARCH_MESSAGES'))}]: "
search_text = (
f"Search [{', '.join(display_keys_for_command('SEARCH_MESSAGES'))}]: "
)
self.text_box = ReadlineEdit(f"{search_text} ")
# Add some text so that when packing,
# urwid doesn't hide the widget.
Expand Down Expand Up @@ -967,7 +972,9 @@ def __init__(
) -> None:
self.panel_view = panel_view
self.search_command = search_command
self.search_text = f" Search [{', '.join(keys_for_command(search_command))}]: "
self.search_text = (
f" Search [{', '.join(display_keys_for_command(search_command))}]: "
)
self.search_error = urwid.AttrMap(
urwid.Text([" ", INVALID_MARKER, " No Results"]), "search_error"
)
Expand Down
20 changes: 15 additions & 5 deletions zulipterminal/ui_tools/buttons.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@
from typing_extensions import TypedDict

from zulipterminal.api_types import RESOLVED_TOPIC_PREFIX, EditPropagateMode, Message
from zulipterminal.config.keys import is_command_key, primary_key_for_command
from zulipterminal.config.keys import (
is_command_key,
primary_display_key_for_command,
primary_key_for_command,
)
from zulipterminal.config.regexes import REGEX_INTERNAL_LINK_STREAM_ID
from zulipterminal.config.symbols import (
ALL_MESSAGES_MARKER,
Expand Down Expand Up @@ -125,7 +129,9 @@ def keypress(self, size: urwid_Size, key: str) -> Optional[str]:

class HomeButton(TopButton):
def __init__(self, *, controller: Any, count: int) -> None:
button_text = f"All messages [{primary_key_for_command('ALL_MESSAGES')}]"
button_text = (
f"All messages [{primary_display_key_for_command('ALL_MESSAGES')}]"
)

super().__init__(
controller=controller,
Expand All @@ -139,7 +145,7 @@ def __init__(self, *, controller: Any, count: int) -> None:

class PMButton(TopButton):
def __init__(self, *, controller: Any, count: int) -> None:
button_text = f"Direct messages [{primary_key_for_command('ALL_PM')}]"
button_text = f"Direct messages [{primary_display_key_for_command('ALL_PM')}]"

super().__init__(
controller=controller,
Expand All @@ -153,7 +159,9 @@ def __init__(self, *, controller: Any, count: int) -> None:

class MentionedButton(TopButton):
def __init__(self, *, controller: Any, count: int) -> None:
button_text = f"Mentions [{primary_key_for_command('ALL_MENTIONS')}]"
button_text = (
f"Mentions [{primary_display_key_for_command('ALL_MENTIONS')}]"
)

super().__init__(
controller=controller,
Expand All @@ -167,7 +175,9 @@ def __init__(self, *, controller: Any, count: int) -> None:

class StarredButton(TopButton):
def __init__(self, *, controller: Any, count: int) -> None:
button_text = f"Starred messages [{primary_key_for_command('ALL_STARRED')}]"
button_text = (
f"Starred messages [{primary_display_key_for_command('ALL_STARRED')}]"
)

super().__init__(
controller=controller,
Expand Down
30 changes: 20 additions & 10 deletions zulipterminal/ui_tools/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@
from zulipterminal.config.keys import (
HELP_CATEGORIES,
KEY_BINDINGS,
display_key_for_urwid_key,
display_keys_for_command,
is_command_key,
keys_for_command,
primary_key_for_command,
)
from zulipterminal.config.markdown_examples import MARKDOWN_ELEMENTS
Expand Down Expand Up @@ -1225,9 +1226,14 @@ def __init__(self, controller: Any, title: str) -> None:
for binding in KEY_BINDINGS.values()
if binding["key_category"] == category
)
key_bindings = []
for binding in keys_in_category:
key_bindings.append((binding["help_text"], ", ".join(binding["keys"])))
key_bindings = [
(
binding["help_text"],
", ".join(map(display_key_for_urwid_key, binding["keys"])),
)
for binding in keys_in_category
]

help_menu_content.append((HELP_CATEGORIES[category], key_bindings))

popup_width, column_widths = self.calculate_table_widths(
Expand Down Expand Up @@ -1379,15 +1385,17 @@ def __init__(self, controller: Any, stream_id: int) -> None:
if stream["history_public_to_subscribers"]
else "Not Public to Users"
)
member_keys = ", ".join(map(repr, keys_for_command("STREAM_MEMBERS")))
member_keys = ", ".join(map(repr, display_keys_for_command("STREAM_MEMBERS")))

# FIXME: This field was removed from the subscription data in Zulip 7.5 / ZFL226
# We should use the new /streams/{stream_id}/email_address endpoint instead
self._stream_email = stream.get("email_address", None)
if self._stream_email is None:
stream_copy_text = "< Stream email is unavailable >"
else:
email_keys = ", ".join(map(repr, keys_for_command("COPY_STREAM_EMAIL")))
email_keys = ", ".join(
map(repr, display_keys_for_command("COPY_STREAM_EMAIL"))
)
stream_copy_text = f"Press {email_keys} to copy Stream email address"

weekly_traffic = stream["stream_weekly_traffic"]
Expand Down Expand Up @@ -1562,14 +1570,14 @@ def __init__(
msg["timestamp"], show_seconds=True, show_year=True
)
view_in_browser_keys = "[{}]".format(
", ".join(map(str, keys_for_command("VIEW_IN_BROWSER")))
", ".join(map(str, display_keys_for_command("VIEW_IN_BROWSER")))
)

full_rendered_message_keys = "[{}]".format(
", ".join(map(str, keys_for_command("FULL_RENDERED_MESSAGE")))
", ".join(map(str, display_keys_for_command("FULL_RENDERED_MESSAGE")))
)
full_raw_message_keys = "[{}]".format(
", ".join(map(str, keys_for_command("FULL_RAW_MESSAGE")))
", ".join(map(str, display_keys_for_command("FULL_RAW_MESSAGE")))
)
msg_info = [
(
Expand Down Expand Up @@ -1601,7 +1609,9 @@ def __init__(
if self.show_edit_history_label:
msg_info[0][1][0] = ("Date & Time (Original)", date_and_time)

keys = "[{}]".format(", ".join(map(str, keys_for_command("EDIT_HISTORY"))))
keys = "[{}]".format(
", ".join(map(str, display_keys_for_command("EDIT_HISTORY")))
)
msg_info[1][1].append(("Edit History", keys))
# Render the category using the existing table methods if links exist.
if message_links:
Expand Down

0 comments on commit a2e616e

Please sign in to comment.