Skip to content

Commit 175e6ff

Browse files
committed
boxes/views: Generalize footlinks_view() for StreamInfoView.
This modifies footlinks_view() to accept a range of configurable parameters and return footlinks_width. Tests amended.
1 parent ee7167e commit 175e6ff

File tree

4 files changed

+89
-23
lines changed

4 files changed

+89
-23
lines changed

tests/ui/test_ui_tools.py

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2015,41 +2015,46 @@ def test_reactions_view(self, message_fixture, to_vary_in_each_message):
20152015
]
20162016

20172017
@pytest.mark.parametrize(['message_links', 'expected_text',
2018-
'expected_attrib'], [
2018+
'expected_attrib', 'expected_footlinks_width'], [
20192019
(OrderedDict([
20202020
('https://github.com/zulip/zulip-terminal/pull/1', ('#T1', 1,
20212021
True)),
20222022
]),
20232023
'1: https://github.com/zulip/zulip-terminal/pull/1',
2024-
[('msg_link_index', 2), (None, 1), ('msg_link', 46)]),
2024+
[('msg_link_index', 2), (None, 1), ('msg_link', 46)],
2025+
49),
20252026
(OrderedDict([
20262027
('https://foo.com', ('Foo!', 1, True)),
20272028
('https://bar.com', ('Bar!', 2, True)),
20282029
]),
20292030
'1: https://foo.com\n2: https://bar.com',
20302031
[('msg_link_index', 2), (None, 1), ('msg_link', 15), (None, 1),
2031-
('msg_link_index', 2), (None, 1), ('msg_link', 15)]),
2032+
('msg_link_index', 2), (None, 1), ('msg_link', 15)],
2033+
18),
20322034
(OrderedDict([
20332035
('https://example.com', ('https://example.com', 1, False)),
20342036
('http://example.com', ('http://example.com', 2, False)),
20352037
]),
20362038
None,
2037-
None),
2039+
None,
2040+
0),
20382041
(OrderedDict([
20392042
('https://foo.com', ('https://foo.com, Text', 1, True)),
20402043
('https://bar.com', ('Text, https://bar.com', 2, True)),
20412044
]),
20422045
'1: https://foo.com\n2: https://bar.com',
20432046
[('msg_link_index', 2), (None, 1), ('msg_link', 15), (None, 1),
2044-
('msg_link_index', 2), (None, 1), ('msg_link', 15)]),
2047+
('msg_link_index', 2), (None, 1), ('msg_link', 15)],
2048+
18),
20452049
(OrderedDict([
20462050
('https://foo.com', ('Foo!', 1, True)),
20472051
('http://example.com', ('example.com', 2, False)),
20482052
('https://bar.com', ('Bar!', 3, True)),
20492053
]),
20502054
'1: https://foo.com\n3: https://bar.com',
20512055
[('msg_link_index', 2), (None, 1), ('msg_link', 15), (None, 1),
2052-
('msg_link_index', 2), (None, 1), ('msg_link', 15)]),
2056+
('msg_link_index', 2), (None, 1), ('msg_link', 15)],
2057+
18),
20532058
],
20542059
ids=[
20552060
'one_footlink',
@@ -2060,15 +2065,18 @@ def test_reactions_view(self, message_fixture, to_vary_in_each_message):
20602065
]
20612066
)
20622067
def test_footlinks_view(self, message_links, expected_text,
2063-
expected_attrib):
2064-
footlinks = MessageBox.footlinks_view(
2068+
expected_attrib, expected_footlinks_width):
2069+
footlinks, footlinks_width = MessageBox.footlinks_view(
20652070
message_links,
20662071
footlinks_enabled=True,
2072+
padded=True,
2073+
wrap='ellipsis',
20672074
)
20682075

20692076
if expected_text:
20702077
assert footlinks.original_widget.text == expected_text
20712078
assert footlinks.original_widget.attrib == expected_attrib
2079+
assert footlinks_width == expected_footlinks_width
20722080
else:
20732081
assert footlinks is None
20742082
assert not hasattr(footlinks, 'original_widget')
@@ -2082,9 +2090,11 @@ def test_footlinks_enabled(self, footlinks_enabled, expected_instance):
20822090
('https://github.com/zulip/zulip-terminal', ('ZT', 1, True)),
20832091
])
20842092

2085-
footlinks = MessageBox.footlinks_view(
2093+
footlinks, _ = MessageBox.footlinks_view(
20862094
message_links,
20872095
footlinks_enabled=footlinks_enabled,
2096+
padded=True,
2097+
wrap='ellipsis',
20882098
)
20892099

20902100
assert isinstance(footlinks, expected_instance)

tests/ui_tools/test_popups.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from urwid import Columns, Text
55

66
from zulipterminal.config.keys import is_command_key, keys_for_command
7+
from zulipterminal.ui_tools.boxes import MessageBox
78
from zulipterminal.ui_tools.views import (
89
AboutView, EditHistoryView, EditModeView, HelpView, MsgInfoView,
910
PopUpConfirmationView, PopUpView, StreamInfoView, StreamMembersView,
@@ -613,6 +614,31 @@ def test_markup_descrption(self, rendered_description, expected_markup,
613614

614615
assert stream_info_view.markup_desc == expected_markup
615616

617+
@pytest.mark.parametrize(['message_links', 'expected_text',
618+
'expected_attrib', 'expected_footlinks_width'], [
619+
(OrderedDict([
620+
('https://example.com', ('Example', 1, True)),
621+
('https://generic.com', ('Generic', 2, True)),
622+
]),
623+
'1: https://example.com\n2: https://generic.com',
624+
[('msg_link_index', 2), (None, 1), ('msg_link', 19), (None, 1),
625+
('msg_link_index', 2), (None, 1), ('msg_link', 19)],
626+
22),
627+
],
628+
)
629+
def test_footlinks(self, message_links, expected_text, expected_attrib,
630+
expected_footlinks_width):
631+
footlinks, footlinks_width = MessageBox.footlinks_view(
632+
message_links,
633+
footlinks_enabled=True,
634+
padded=False,
635+
wrap='space',
636+
)
637+
638+
assert footlinks.text == expected_text
639+
assert footlinks.attrib == expected_attrib
640+
assert footlinks_width == expected_footlinks_width
641+
616642
@pytest.mark.parametrize('key', {*keys_for_command('GO_BACK'),
617643
*keys_for_command('STREAM_DESC')})
618644
def test_keypress_exit_popup(self, key, widget_size):

zulipterminal/ui_tools/boxes.py

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -713,30 +713,46 @@ def footlinks_view(
713713
message_links: 'OrderedDict[str, Tuple[str, int, bool]]',
714714
*,
715715
footlinks_enabled: bool,
716-
) -> Any:
716+
padded: bool,
717+
wrap: str
718+
) -> Tuple[Any, int]:
719+
"""
720+
Returns a Tuple that consists footlinks view (widget) and its required
721+
width.
722+
"""
717723
# Return if footlinks are disabled by the user.
718724
if not footlinks_enabled:
719-
return None
725+
return None, 0
720726

721727
footlinks = []
728+
footlinks_width = 0
722729
for link, (text, index, show_footlink) in message_links.items():
723730
if not show_footlink:
724731
continue
725732

726-
footlinks.extend([
733+
styled_footlink = [
727734
('msg_link_index', '{}:'.format(index)),
728-
' ',
735+
(None, ' '),
729736
('msg_link', link),
730-
'\n',
731-
])
737+
]
738+
footlinks_width = max(
739+
footlinks_width,
740+
sum([len(text) for style, text in styled_footlink])
741+
)
742+
footlinks.extend([*styled_footlink, '\n'])
732743

733744
if not footlinks:
734-
return None
745+
return None, 0
735746

736747
footlinks[-1] = footlinks[-1][:-1] # Remove the last newline.
737-
return urwid.Padding(urwid.Text(footlinks, wrap='ellipsis'),
738-
align='left', left=8, width=('relative', 100),
739-
min_width=10, right=2)
748+
749+
text_widget = urwid.Text(footlinks, wrap=wrap)
750+
if padded:
751+
return urwid.Padding(text_widget, align='left', left=8,
752+
width=('relative', 100), min_width=10,
753+
right=2), footlinks_width
754+
else:
755+
return text_widget, footlinks_width
740756

741757
@classmethod
742758
def soup2markup(cls, soup: Any, metadata: Dict[str, Any],
@@ -1080,9 +1096,11 @@ def main_view(self) -> List[Any]:
10801096
reactions = self.reactions_view(self.message['reactions'])
10811097

10821098
# Footlinks.
1083-
footlinks = self.footlinks_view(
1099+
footlinks, _ = self.footlinks_view(
10841100
self.message_links,
10851101
footlinks_enabled=self.model.controller.footlinks_enabled,
1102+
padded=True,
1103+
wrap='ellipsis',
10861104
)
10871105

10881106
# Build parts together and return

zulipterminal/ui_tools/views.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1088,7 +1088,7 @@ def __init__(self, controller: Any, stream_id: int) -> None:
10881088
else STREAM_MARKER_PUBLIC)
10891089
title = '{} {}'.format(stream_marker, stream['name'])
10901090
rendered_desc = stream['rendered_description']
1091-
self.markup_desc, *_ = MessageBox.transform_content(
1091+
self.markup_desc, message_links, _ = MessageBox.transform_content(
10921092
rendered_desc,
10931093
self.controller.model.server_url,
10941094
)
@@ -1118,16 +1118,28 @@ def __init__(self, controller: Any, stream_id: int) -> None:
11181118
urwid.connect_signal(pinned_setting, 'change',
11191119
self.toggle_pinned_status)
11201120

1121+
footlinks, footlinks_width = MessageBox.footlinks_view(
1122+
message_links=message_links,
1123+
footlinks_enabled=True,
1124+
padded=False,
1125+
wrap='space',
1126+
)
1127+
11211128
# Manual because calculate_table_widths does not support checkboxes.
11221129
# Add 4 to checkbox label to accomodate the checkbox itself.
11231130
popup_width = max(popup_width, len(muted_setting.label) + 4,
1124-
len(pinned_setting.label) + 4, desc.pack()[0])
1131+
len(pinned_setting.label) + 4, desc.pack()[0],
1132+
footlinks_width)
11251133
self.widgets = self.make_table_with_categories(stream_info_content,
11261134
column_widths)
11271135

11281136
# Stream description.
11291137
self.widgets.insert(0, desc)
1130-
self.widgets.insert(1, urwid.Text('')) # Add a newline.
1138+
desc_newline = 1
1139+
if footlinks:
1140+
self.widgets.insert(1, footlinks)
1141+
desc_newline = 2
1142+
self.widgets.insert(desc_newline, urwid.Text('')) # Add a newline.
11311143

11321144
self.widgets.append(muted_setting)
11331145
self.widgets.append(pinned_setting)

0 commit comments

Comments
 (0)