Skip to content

Commit 610dfd0

Browse files
committed
messages: Implement logic to display polls on ZT.
1 parent 33fe3dd commit 610dfd0

File tree

1 file changed

+37
-1
lines changed

1 file changed

+37
-1
lines changed

zulipterminal/ui_tools/messages.py

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,11 @@
3333
from zulipterminal.server_url import near_message_url
3434
from zulipterminal.ui_tools.tables import render_table
3535
from zulipterminal.urwid_types import urwid_MarkupTuple, urwid_Size
36-
from zulipterminal.widget import find_widget_type, process_todo_widget
36+
from zulipterminal.widget import (
37+
find_widget_type,
38+
process_poll_widget,
39+
process_todo_widget,
40+
)
3741

3842

3943
if typing.TYPE_CHECKING:
@@ -755,6 +759,38 @@ def main_view(self) -> List[Any]:
755759
# though it's not very useful.
756760
self.message["content"] = todo_widget
757761

762+
elif widget_type == "poll":
763+
poll_question, poll_options = process_poll_widget(
764+
self.message.get("submessages", [])
765+
)
766+
767+
# TODO: ZT doesn't yet support adding poll questions after the
768+
# creation of the poll. So, if the poll question is not provided,
769+
# we show a message to add one via the web app.
770+
if not poll_question:
771+
poll_question = (
772+
"No poll question is provided. Please add one via the web app."
773+
)
774+
775+
poll_widget = f"<strong>Poll\n{poll_question}</strong>"
776+
777+
if poll_options:
778+
max_votes_len = max(
779+
len(str(len(option["votes"])))
780+
for option in poll_options.values()
781+
)
782+
783+
for option_info in poll_options.values():
784+
padded_votes = f"{len(option_info['votes']):>{max_votes_len}}"
785+
poll_widget += f"\n[ {padded_votes} ] {option_info['option']}"
786+
else:
787+
poll_widget += "\nNo options provided."
788+
"Please add them via the web app."
789+
790+
# Update the message content with the latest poll_widget,
791+
# similar to the todo_widget above.
792+
self.message["content"] = poll_widget
793+
758794
# Transform raw message content into markup (As needed by urwid.Text)
759795
content, self.message_links, self.time_mentions = self.transform_content(
760796
self.message["content"], self.model.server_url

0 commit comments

Comments
 (0)