Skip to content

Commit fa3c6e1

Browse files
rsashankneiljp
authored andcommitted
messages: Implement logic to display poll widgets.
This also indirectly enables the content of polls to be live-updated, through the existing mechanism used for todo widgets, which is currently independent of the type of the widget.
1 parent eb93a28 commit fa3c6e1

File tree

1 file changed

+38
-1
lines changed

1 file changed

+38
-1
lines changed

zulipterminal/ui_tools/messages.py

+38-1
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:
@@ -753,6 +757,39 @@ def main_view(self) -> List[Any]:
753757
# though it's not very useful.
754758
self.message["content"] = todo_widget
755759

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

0 commit comments

Comments
 (0)