-
-
Notifications
You must be signed in to change notification settings - Fork 268
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
buttons/messages/views: Add support for copying code snippets. #1353
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@yuktasarode Thanks for tidying this up further! 👍
It's good to see this passing linting and tests :) It's also working well still, though I've not examined the functionality in great detail yet.
I've left some comments inline, but you may want to first rebase against main
, since I recently merged a PR regarding how the 'Action' keys are shown and grouped, which affects this UI.
My note in the other PR (#1134 (comment)) mention a few other changes; I'm not sure if you're already looking at those aspects, but I've left some details below.
I'd suggest the next step to make this easier to work on and read would be to split this into multiple commits. You'll want to confirm how the code links together yourself, but my take would be that we're generating additional data via the message, which will need to go into a button, both of which are combined into the end result - so 3 commits might be a good starting point.
Moving the behavior into the CodeSnippetButton may be another good step, or at least inheriting via urwid.Button
like the MessageLinkButton does in the first instance, since I believe it should achieve most of what the custom button does already?
My note in the other PR (#1134 (comment)) also notes regarding adding tests for each commit/change; if you wish to start on that, that's fine, but I expect it'll be a lot easier to keep track of when the commit size is smaller, and you can separate which code change/addition is related to which test change/addition.
zulipterminal/ui_tools/views.py
Outdated
urwid.connect_signal(button, "click", self.copy_to_clipboard, copy_code) | ||
display_attr = None if index % 2 else "popup_contrast" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be cleaner if we could have this kind of behavior, and that in a few other methods below, defined in the CodeSnippetButton itself instead, like we do with other buttons - I can't see a reason not to do this.
Note how the create_link_button function is simpler compared to this one.
The CodeSnippetButton does very little that's unique right now - it's very close to being a very simple Button.
zulipterminal/ui_tools/buttons.py
Outdated
@@ -647,6 +647,40 @@ def handle_narrow_link(self) -> None: | |||
self.controller.exit_popup() | |||
|
|||
|
|||
class CodeSnippetButton(urwid.Text): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As mentioned elsewhere, this works, but it would be good to migrate to a button with the specific functionality we want.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did trying inherting urwid.Button and Moving the behavior into the CodeSnippetButton but I could only move the method get_code_from_snippet() from views.py to buttons.py, since all other methods require the instance of controller to be taken into account. Kindly let me know anyother way to do this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right now, all the basic Button functionality currently in CodeSnippetButton
except for get_code_from_snippet
is as per the default in urwid.Button
(you can compare yourself, to wimp.py
in urwid).
Look at the initializer for MessageLinkButton
- that is how we can set up a controller via the button. We may also want to store the code content (snippet information) in there, for example.
Right now you're using the class name to call a method in a class, and explicitly passing the self
value. Normally that'd be a bad idea and often cause an error, but you're not using self
in get_code_from_snippet
here! Instead, we should be creating CodeSnippetButton
s with the relevant data, then using their properties as we need them.
I understand you've inherited this PR, but this (and the UI, using urwid) is using OOP in Python, so you may need to learn more about this if you've not worked on this before.
86b195c
to
e94f337
Compare
@yuktasarode Is this ready for review? If so, please change the labels as per my message in the GSoC 2023 topic in zulip-terminal (ie. remove needing update and add needs review). |
@zulipbot add "PR needs review" |
@zulipbot remove "PR awaiting update" |
@yuktasarode Could you please rebase this after the recent changes we had in You're welcome to take up another issue also, of course 👍 |
Made the button inhert urwid.button and added supporting methods for creating the code snippet button.
Tests updated.
Inherited urwid.button class in create_code_snippet_buttons method. Get the code snippets and copy it on button press. Tests added for create_code_snippet_buttons.
e94f337
to
6d20d74
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@yuktasarode You didn't note this for review, but I saw you had pushed. This looks much closer than before 🎉
display_code, copy_code = CodeSnippetButton.get_code_from_snippet( | ||
self, snippet_list | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Generally the button vs view separation looks much better, but this stands out. The button should be able to calculate this internally :)
Can you move the initialization of the button up here? It gets the display and copy_code, but that is associated with code related to the rendering. So you should be able to pass in the language/snippet and have a button function provide the data you need externally. It should be able to set the caption itself :)
super().__init__("") | ||
|
||
self.update_widget(display_code, display_attr) | ||
urwid.connect_signal(self, "click", self.copy_to_clipboard) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This makes it work for clicks, but not for the keypress. See eg. the StreamButton.
# Set cursor position next to len(caption) to avoid the cursor. | ||
icon = urwid.SelectableIcon(display_code, cursor_position=len(display_code) + 1) | ||
self._w = urwid.AttrMap(icon, display_attr, focus_map="selected") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doesn't appear to be working for this button. I see a flashing cursor on the button while testing.
What does this PR do?
This PR aims to add support for copying code snippets and is based off on the work done by @kingjuno and @neiljp in the PR #1134. This PR mainly fixes the tests and linting issues and bring the PR to a merge-able state.
Tested?
This may fix the core elements of #1123.