Skip to content

Commit 0deaca8

Browse files
committed
boxes/views: Generalize footlinks_view() for StreamInfoView.
This modifies footlinks_view() to accept a range of configurable parameters, return footlinks_width and makes the method static to facilitate its reusability. While the commit only has changes for StreamInfoView, this will also be useful for rendering spoiler content later. Tests amended.
1 parent da89e1f commit 0deaca8

File tree

4 files changed

+66
-30
lines changed

4 files changed

+66
-30
lines changed

tests/core/test_core.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
class TestController:
1616
@pytest.fixture(autouse=True)
1717
def mock_external_classes(self, mocker: Any) -> None:
18-
mocker.patch('zulipterminal.ui_tools.boxes.MessageBox.footlinks_view')
18+
mocker.patch('zulipterminal.ui_tools.boxes.MessageBox.main_view')
1919
self.client = mocker.patch('zulip.Client')
2020
# Patch init only, in general, allowing specific patching elsewhere
2121
self.model = mocker.patch(CORE + '.Model.__init__', return_value=None)

tests/ui/test_ui_tools.py

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2319,41 +2319,46 @@ def test_reactions_view(self, message_fixture, to_vary_in_each_message):
23192319
]
23202320

23212321
@pytest.mark.parametrize(['message_links', 'expected_text',
2322-
'expected_attrib'], [
2322+
'expected_attrib', 'expected_footlinks_width'], [
23232323
(OrderedDict([
23242324
('https://github.com/zulip/zulip-terminal/pull/1', ('#T1', 1,
23252325
True)),
23262326
]),
23272327
'1: https://github.com/zulip/zulip-terminal/pull/1',
2328-
[('msg_link_index', 2), (None, 1), ('msg_link', 46)]),
2328+
[('msg_link_index', 2), (None, 1), ('msg_link', 46)],
2329+
49),
23292330
(OrderedDict([
23302331
('https://foo.com', ('Foo!', 1, True)),
23312332
('https://bar.com', ('Bar!', 2, True)),
23322333
]),
23332334
'1: https://foo.com\n2: https://bar.com',
23342335
[('msg_link_index', 2), (None, 1), ('msg_link', 15), (None, 1),
2335-
('msg_link_index', 2), (None, 1), ('msg_link', 15)]),
2336+
('msg_link_index', 2), (None, 1), ('msg_link', 15)],
2337+
18),
23362338
(OrderedDict([
23372339
('https://example.com', ('https://example.com', 1, False)),
23382340
('http://example.com', ('http://example.com', 2, False)),
23392341
]),
23402342
None,
2341-
None),
2343+
None,
2344+
0),
23422345
(OrderedDict([
23432346
('https://foo.com', ('https://foo.com, Text', 1, True)),
23442347
('https://bar.com', ('Text, https://bar.com', 2, True)),
23452348
]),
23462349
'1: https://foo.com\n2: https://bar.com',
23472350
[('msg_link_index', 2), (None, 1), ('msg_link', 15), (None, 1),
2348-
('msg_link_index', 2), (None, 1), ('msg_link', 15)]),
2351+
('msg_link_index', 2), (None, 1), ('msg_link', 15)],
2352+
18),
23492353
(OrderedDict([
23502354
('https://foo.com', ('Foo!', 1, True)),
23512355
('http://example.com', ('example.com', 2, False)),
23522356
('https://bar.com', ('Bar!', 3, True)),
23532357
]),
23542358
'1: https://foo.com\n3: https://bar.com',
23552359
[('msg_link_index', 2), (None, 1), ('msg_link', 15), (None, 1),
2356-
('msg_link_index', 2), (None, 1), ('msg_link', 15)]),
2360+
('msg_link_index', 2), (None, 1), ('msg_link', 15)],
2361+
18),
23572362
],
23582363
ids=[
23592364
'one_footlink',
@@ -2363,16 +2368,19 @@ def test_reactions_view(self, message_fixture, to_vary_in_each_message):
23632368
'http_default_scheme',
23642369
]
23652370
)
2366-
def test_footlinks_view(self, message_fixture, message_links,
2367-
expected_text, expected_attrib):
2368-
self.model.controller.footlinks_enabled = True
2369-
msg_box = MessageBox(message_fixture, self.model, None)
2370-
2371-
footlinks = msg_box.footlinks_view(message_links)
2371+
def test_footlinks_view(self, message_links, expected_text,
2372+
expected_attrib, expected_footlinks_width):
2373+
footlinks, footlinks_width = MessageBox.footlinks_view(
2374+
message_links,
2375+
footlinks_enabled=True,
2376+
padded=True,
2377+
wrap='ellipsis',
2378+
)
23722379

23732380
if expected_text:
23742381
assert footlinks.original_widget.text == expected_text
23752382
assert footlinks.original_widget.attrib == expected_attrib
2383+
assert footlinks_width == expected_footlinks_width
23762384
else:
23772385
assert footlinks is None
23782386
assert not hasattr(footlinks, 'original_widget')
@@ -2381,15 +2389,17 @@ def test_footlinks_view(self, message_fixture, message_links,
23812389
(False, type(None)),
23822390
(True, Padding),
23832391
])
2384-
def test_footlinks_enabled(self, message_fixture, footlinks_enabled,
2385-
expected_instance):
2392+
def test_footlinks_enabled(self, footlinks_enabled, expected_instance):
23862393
message_links = OrderedDict([
23872394
('https://github.com/zulip/zulip-terminal', ('ZT', 1, True)),
23882395
])
2389-
self.model.controller.footlinks_enabled = footlinks_enabled
2390-
msg_box = MessageBox(message_fixture, self.model, None)
23912396

2392-
footlinks = msg_box.footlinks_view(message_links)
2397+
footlinks, _ = MessageBox.footlinks_view(
2398+
message_links,
2399+
footlinks_enabled,
2400+
padded=True,
2401+
wrap='ellipsis',
2402+
)
23932403

23942404
assert isinstance(footlinks, expected_instance)
23952405

zulipterminal/ui_tools/boxes.py

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -596,18 +596,23 @@ def reactions_view(self, reactions: List[Dict[str, Any]]) -> Any:
596596

597597
# Use quotes as a workaround for OrderedDict typing issue.
598598
# See https://github.com/python/mypy/issues/6904.
599+
@staticmethod
599600
def footlinks_view(
600-
self, message_links: 'OrderedDict[str, Tuple[str, int, bool]]',
601-
) -> Any:
601+
message_links: 'OrderedDict[str, Tuple[str, int, bool]]',
602+
footlinks_enabled: bool, padded: bool, wrap: str,
603+
) -> Tuple[Any, int]:
602604
# Return if footlinks are disabled by the user.
603-
if not self.model.controller.footlinks_enabled:
604-
return None
605+
if not footlinks_enabled:
606+
return None, 0
605607

606608
footlinks = []
609+
footlinks_width = 0
607610
for link, (text, index, show_footlink) in message_links.items():
608611
if not show_footlink:
609612
continue
610613

614+
footlinks_width = max(footlinks_width,
615+
len('{}: {}'.format(index, link)))
611616
footlinks.extend([
612617
('msg_link_index', '{}:'.format(index)),
613618
' ',
@@ -616,12 +621,17 @@ def footlinks_view(
616621
])
617622

618623
if not footlinks:
619-
return None
624+
return None, 0
620625

621626
footlinks[-1] = footlinks[-1][:-1] # Remove the last newline.
622-
return urwid.Padding(urwid.Text(footlinks, wrap='ellipsis'),
623-
align='left', left=8, width=('relative', 100),
624-
min_width=10, right=2)
627+
628+
text_widget = urwid.Text(footlinks, wrap=wrap)
629+
if padded:
630+
return urwid.Padding(text_widget, align='left', left=8,
631+
width=('relative', 100), min_width=10,
632+
right=2), footlinks_width
633+
else:
634+
return text_widget, footlinks_width
625635

626636
@classmethod
627637
def soup2markup(cls, soup: Any, server_url: str, metadata: Dict[str, Any],
@@ -974,7 +984,12 @@ def main_view(self) -> List[Any]:
974984
reactions = self.reactions_view(self.message['reactions'])
975985

976986
# Footlinks.
977-
footlinks = self.footlinks_view(self.message_links)
987+
footlinks, _ = self.footlinks_view(
988+
self.message_links,
989+
self.model.controller.footlinks_enabled,
990+
padded=True,
991+
wrap='ellipsis',
992+
)
978993

979994
# Build parts together and return
980995
parts = [

zulipterminal/ui_tools/views.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1065,22 +1065,33 @@ def __init__(self, controller: Any, stream_id: int) -> None:
10651065
urwid.connect_signal(pinned_setting, 'change',
10661066
self.toggle_pinned_status)
10671067

1068-
markup_desc, *_ = MessageBox.transform_content(
1068+
markup_desc, message_links, _ = MessageBox.transform_content(
10691069
rendered_desc,
10701070
self.controller.model.server_url,
10711071
)
10721072
desc = urwid.Text(markup_desc)
1073+
footlinks, footlinks_width = MessageBox.footlinks_view(
1074+
message_links=message_links,
1075+
footlinks_enabled=True,
1076+
padded=False,
1077+
wrap='space',
1078+
)
10731079

10741080
# Manual because calculate_table_widths does not support checkboxes.
10751081
# Add 4 to checkbox label to accomodate the checkbox itself.
10761082
popup_width = max(popup_width, len(muted_setting.label) + 4,
1077-
len(pinned_setting.label) + 4, desc.pack()[0])
1083+
len(pinned_setting.label) + 4, desc.pack()[0],
1084+
footlinks_width)
10781085
self.widgets = self.make_table_with_categories(stream_info_content,
10791086
column_widths)
10801087

10811088
# Stream description.
10821089
self.widgets.insert(0, desc)
1083-
self.widgets.insert(1, urwid.Text('')) # Add a newline.
1090+
desc_newline = 1
1091+
if footlinks:
1092+
self.widgets.insert(1, footlinks)
1093+
desc_newline = 2
1094+
self.widgets.insert(desc_newline, urwid.Text('')) # Add a newline.
10841095

10851096
self.widgets.append(muted_setting)
10861097
self.widgets.append(pinned_setting)

0 commit comments

Comments
 (0)