Skip to content

Commit 90c68a2

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 76c02be commit 90c68a2

File tree

4 files changed

+84
-23
lines changed

4 files changed

+84
-23
lines changed

tests/ui/test_ui_tools.py

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

20142014
@pytest.mark.parametrize(['message_links', 'expected_text',
2015-
'expected_attrib'], [
2015+
'expected_attrib', 'expected_footlinks_width'], [
20162016
(OrderedDict([
20172017
('https://github.com/zulip/zulip-terminal/pull/1', ('#T1', 1,
20182018
True)),
20192019
]),
20202020
'1: https://github.com/zulip/zulip-terminal/pull/1',
2021-
[('msg_link_index', 2), (None, 1), ('msg_link', 46)]),
2021+
[('msg_link_index', 2), (None, 1), ('msg_link', 46)],
2022+
49),
20222023
(OrderedDict([
20232024
('https://foo.com', ('Foo!', 1, True)),
20242025
('https://bar.com', ('Bar!', 2, True)),
20252026
]),
20262027
'1: https://foo.com\n2: https://bar.com',
20272028
[('msg_link_index', 2), (None, 1), ('msg_link', 15), (None, 1),
2028-
('msg_link_index', 2), (None, 1), ('msg_link', 15)]),
2029+
('msg_link_index', 2), (None, 1), ('msg_link', 15)],
2030+
18),
20292031
(OrderedDict([
20302032
('https://example.com', ('https://example.com', 1, False)),
20312033
('http://example.com', ('http://example.com', 2, False)),
20322034
]),
20332035
None,
2034-
None),
2036+
None,
2037+
0),
20352038
(OrderedDict([
20362039
('https://foo.com', ('https://foo.com, Text', 1, True)),
20372040
('https://bar.com', ('Text, https://bar.com', 2, True)),
20382041
]),
20392042
'1: https://foo.com\n2: https://bar.com',
20402043
[('msg_link_index', 2), (None, 1), ('msg_link', 15), (None, 1),
2041-
('msg_link_index', 2), (None, 1), ('msg_link', 15)]),
2044+
('msg_link_index', 2), (None, 1), ('msg_link', 15)],
2045+
18),
20422046
(OrderedDict([
20432047
('https://foo.com', ('Foo!', 1, True)),
20442048
('http://example.com', ('example.com', 2, False)),
20452049
('https://bar.com', ('Bar!', 3, True)),
20462050
]),
20472051
'1: https://foo.com\n3: https://bar.com',
20482052
[('msg_link_index', 2), (None, 1), ('msg_link', 15), (None, 1),
2049-
('msg_link_index', 2), (None, 1), ('msg_link', 15)]),
2053+
('msg_link_index', 2), (None, 1), ('msg_link', 15)],
2054+
18),
20502055
],
20512056
ids=[
20522057
'one_footlink',
@@ -2057,15 +2062,18 @@ def test_reactions_view(self, message_fixture, to_vary_in_each_message):
20572062
]
20582063
)
20592064
def test_footlinks_view(self, message_links, expected_text,
2060-
expected_attrib):
2061-
footlinks = MessageBox.footlinks_view(
2065+
expected_attrib, expected_footlinks_width):
2066+
footlinks, footlinks_width = MessageBox.footlinks_view(
20622067
message_links,
20632068
footlinks_enabled=True,
2069+
padded=True,
2070+
wrap='ellipsis',
20642071
)
20652072

20662073
if expected_text:
20672074
assert footlinks.original_widget.text == expected_text
20682075
assert footlinks.original_widget.attrib == expected_attrib
2076+
assert footlinks_width == expected_footlinks_width
20692077
else:
20702078
assert footlinks is None
20712079
assert not hasattr(footlinks, 'original_widget')
@@ -2079,9 +2087,11 @@ def test_footlinks_enabled(self, footlinks_enabled, expected_instance):
20792087
('https://github.com/zulip/zulip-terminal', ('ZT', 1, True)),
20802088
])
20812089

2082-
footlinks = MessageBox.footlinks_view(
2090+
footlinks, _ = MessageBox.footlinks_view(
20832091
message_links,
20842092
footlinks_enabled=footlinks_enabled,
2093+
padded=True,
2094+
wrap='ellipsis',
20852095
)
20862096

20872097
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,
@@ -595,6 +596,31 @@ def test_markup_descrption(self, rendered_description, expected_markup,
595596

596597
assert stream_info_view.markup_desc == expected_markup
597598

599+
@pytest.mark.parametrize(['message_links', 'expected_text',
600+
'expected_attrib', 'expected_footlinks_width'], [
601+
(OrderedDict([
602+
('https://example.com', ('Example', 1, True)),
603+
('https://generic.com', ('Generic', 2, True)),
604+
]),
605+
'1: https://example.com\n2: https://generic.com',
606+
[('msg_link_index', 2), (None, 1), ('msg_link', 19), (None, 1),
607+
('msg_link_index', 2), (None, 1), ('msg_link', 19)],
608+
22),
609+
],
610+
)
611+
def test_footlinks(self, message_links, expected_text, expected_attrib,
612+
expected_footlinks_width):
613+
footlinks, footlinks_width = MessageBox.footlinks_view(
614+
message_links,
615+
footlinks_enabled=True,
616+
padded=False,
617+
wrap='space',
618+
)
619+
620+
assert footlinks.text == expected_text
621+
assert footlinks.attrib == expected_attrib
622+
assert footlinks_width == expected_footlinks_width
623+
598624
@pytest.mark.parametrize('key', {*keys_for_command('GO_BACK'),
599625
*keys_for_command('STREAM_DESC')})
600626
def test_keypress_exit_popup(self, key, widget_size):

zulipterminal/ui_tools/boxes.py

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -672,30 +672,42 @@ def reactions_view(self, reactions: List[Dict[str, Any]]) -> Any:
672672
def footlinks_view(
673673
message_links: 'OrderedDict[str, Tuple[str, int, bool]]',
674674
footlinks_enabled: bool,
675-
) -> Any:
675+
padded: bool,
676+
wrap: str,
677+
) -> Tuple[Any, int]:
676678
# Return if footlinks are disabled by the user.
677679
if not footlinks_enabled:
678-
return None
680+
return None, 0
679681

680682
footlinks = []
683+
footlinks_width = 0
681684
for link, (text, index, show_footlink) in message_links.items():
682685
if not show_footlink:
683686
continue
684687

685-
footlinks.extend([
688+
styled_footlink = [
686689
('msg_link_index', '{}:'.format(index)),
687-
' ',
690+
(None, ' '),
688691
('msg_link', link),
689-
'\n',
690-
])
692+
]
693+
footlinks_width = max(
694+
footlinks_width,
695+
sum([len(text) for style, text in styled_footlink])
696+
)
697+
footlinks.extend([*styled_footlink, '\n'])
691698

692699
if not footlinks:
693-
return None
700+
return None, 0
694701

695702
footlinks[-1] = footlinks[-1][:-1] # Remove the last newline.
696-
return urwid.Padding(urwid.Text(footlinks, wrap='ellipsis'),
697-
align='left', left=8, width=('relative', 100),
698-
min_width=10, right=2)
703+
704+
text_widget = urwid.Text(footlinks, wrap=wrap)
705+
if padded:
706+
return urwid.Padding(text_widget, align='left', left=8,
707+
width=('relative', 100), min_width=10,
708+
right=2), footlinks_width
709+
else:
710+
return text_widget, footlinks_width
699711

700712
@classmethod
701713
def soup2markup(cls, soup: Any, server_url: str, metadata: Dict[str, Any],
@@ -1048,9 +1060,11 @@ def main_view(self) -> List[Any]:
10481060
reactions = self.reactions_view(self.message['reactions'])
10491061

10501062
# Footlinks.
1051-
footlinks = self.footlinks_view(
1063+
footlinks, _ = self.footlinks_view(
10521064
self.message_links,
10531065
self.model.controller.footlinks_enabled,
1066+
padded=True,
1067+
wrap='ellipsis',
10541068
)
10551069

10561070
# Build parts together and return

zulipterminal/ui_tools/views.py

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

1101-
self.markup_desc, *_ = MessageBox.transform_content(
1101+
self.markup_desc, message_links, _ = MessageBox.transform_content(
11021102
rendered_desc,
11031103
self.controller.model.server_url,
11041104
)
11051105
desc = urwid.Text(self.markup_desc)
1106+
footlinks, footlinks_width = MessageBox.footlinks_view(
1107+
message_links=message_links,
1108+
footlinks_enabled=True,
1109+
padded=False,
1110+
wrap='space',
1111+
)
11061112

11071113
# Manual because calculate_table_widths does not support checkboxes.
11081114
# Add 4 to checkbox label to accomodate the checkbox itself.
11091115
popup_width = max(popup_width, len(muted_setting.label) + 4,
1110-
len(pinned_setting.label) + 4, desc.pack()[0])
1116+
len(pinned_setting.label) + 4, desc.pack()[0],
1117+
footlinks_width)
11111118
self.widgets = self.make_table_with_categories(stream_info_content,
11121119
column_widths)
11131120

11141121
# Stream description.
11151122
self.widgets.insert(0, desc)
1116-
self.widgets.insert(1, urwid.Text('')) # Add a newline.
1123+
desc_newline = 1
1124+
if footlinks:
1125+
self.widgets.insert(1, footlinks)
1126+
desc_newline = 2
1127+
self.widgets.insert(desc_newline, urwid.Text('')) # Add a newline.
11171128

11181129
self.widgets.append(muted_setting)
11191130
self.widgets.append(pinned_setting)

0 commit comments

Comments
 (0)