Skip to content

Commit

Permalink
buttons/keys/views: Introduce hotkey "c" for copying code snippets.
Browse files Browse the repository at this point in the history
Introduces "c" hotkey to copy code snippets from the Message Information
popup. Updates hotkeys linting to exclude "c".
Tests updated.

Fixes #1123.
  • Loading branch information
rsashank committed Jun 3, 2024
1 parent 61f407d commit a7d74f6
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 5 deletions.
1 change: 1 addition & 0 deletions docs/hotkeys.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
|View current message in browser (from message information)|<kbd>v</kbd>|
|Show/hide full rendered message (from message information)|<kbd>f</kbd>|
|Show/hide full raw message (from message information)|<kbd>r</kbd>|
|Copy code snippet to clipboard (from message information)|<kbd>c</kbd>|

## Stream list actions
|Command|Key Combination|
Expand Down
9 changes: 5 additions & 4 deletions tests/ui_tools/test_popups.py
Original file line number Diff line number Diff line change
Expand Up @@ -1117,13 +1117,14 @@ def test_keypress_view_in_browser(
assert self.controller.open_in_browser.called

def test_height_noreactions(self) -> None:
expected_height = 8
# 6 = 1 (date & time) +1 (sender's name) +1 (sender's email)
expected_height = 9
# 3 = 1 (date & time) +1 (sender's name) +1 (sender's email)
# +1 (display group header)
# +1 (whitespace column)
# +1 (view message in browser)
# +1 (full rendered message)
# +1 (full raw message)
# +1 (copy code snippet)
assert self.msg_info_view.height == expected_height

# FIXME This is the same parametrize as MessageBox:test_reactions_view
Expand Down Expand Up @@ -1192,9 +1193,9 @@ def test_height_reactions(
list(),
list(),
)
# 12 = 7 labels + 2 blank lines + 1 'Reactions' (category)
# 11 = 8 labels + 2 blank lines + 1 'Reactions' (category)
# + 4 reactions (excluding 'Message Links').
expected_height = 14
expected_height = 15
assert self.msg_info_view.height == expected_height

@pytest.mark.parametrize(
Expand Down
2 changes: 1 addition & 1 deletion tools/lint-hotkeys
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ SCRIPT_NAME = PurePath(__file__).name
HELP_TEXT_STYLE = re.compile(r"^[a-zA-Z /()',&@#:_-]*$")

# Exclude keys from duplicate keys checking
KEYS_TO_EXCLUDE = ["q", "e", "m", "r"]
KEYS_TO_EXCLUDE = ["q", "e", "m", "r", "c"]


def main(fix: bool) -> None:
Expand Down
7 changes: 7 additions & 0 deletions zulipterminal/config/keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,13 @@ class KeyBinding(TypedDict):
'help_text': 'Show/hide full raw message (from message information)',
'key_category': 'msg_actions',
},
'COPY_CODE_SNIPPET': {
'keys': ['c'],
'help_text':
'Copy code snippet to clipboard (from message information)',
'excluded_from_random_tips': True,
'key_category': 'msg_actions'
},
}
# fmt: on

Expand Down
7 changes: 7 additions & 0 deletions zulipterminal/ui_tools/buttons.py
Original file line number Diff line number Diff line change
Expand Up @@ -742,6 +742,13 @@ def extract_display_code(self, snippet_list: List[Tuple[str, str]]) -> None:
)
self.display_code = [("pygments:w", self.caption)] + self.display_code

def keypress(self, size: urwid_Size, key: str) -> Optional[str]:
if is_command_key("COPY_CODE_SNIPPET", key):
urwid.emit_signal(
self, "click", lambda button: self.copy_to_clipboard(self.snippet_list)
)
return super().keypress(size, key)


class EditModeButton(urwid.Button):
def __init__(self, *, controller: Any, width: int) -> None:
Expand Down
4 changes: 4 additions & 0 deletions zulipterminal/ui_tools/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -1582,6 +1582,9 @@ def __init__(
full_raw_message_keys = "[{}]".format(
", ".join(map(str, display_keys_for_command("FULL_RAW_MESSAGE")))
)
copy_code_keys = "[{}]".format(
", ".join(map(str, display_keys_for_command("COPY_CODE_SNIPPET")))
)
msg_info = [
(
"",
Expand All @@ -1600,6 +1603,7 @@ def __init__(
("Open in web browser", view_in_browser_keys),
("Full rendered message", full_rendered_message_keys),
("Full raw message", full_raw_message_keys),
("Copy code snippet", copy_code_keys),
],
)
msg_info.append(viewing_actions)
Expand Down

0 comments on commit a7d74f6

Please sign in to comment.