Skip to content

Commit 1be52c5

Browse files
preetmishraneiljp
authored andcommitted
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 e9badd4 commit 1be52c5

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
@@ -2028,41 +2028,46 @@ def test_reactions_view(self, message_fixture, to_vary_in_each_message):
20282028
]
20292029

20302030
@pytest.mark.parametrize(['message_links', 'expected_text',
2031-
'expected_attrib'], [
2031+
'expected_attrib', 'expected_footlinks_width'], [
20322032
(OrderedDict([
20332033
('https://github.com/zulip/zulip-terminal/pull/1', ('#T1', 1,
20342034
True)),
20352035
]),
20362036
'1: https://github.com/zulip/zulip-terminal/pull/1',
2037-
[('msg_link_index', 2), (None, 1), ('msg_link', 46)]),
2037+
[('msg_link_index', 2), (None, 1), ('msg_link', 46)],
2038+
49),
20382039
(OrderedDict([
20392040
('https://foo.com', ('Foo!', 1, True)),
20402041
('https://bar.com', ('Bar!', 2, True)),
20412042
]),
20422043
'1: https://foo.com\n2: https://bar.com',
20432044
[('msg_link_index', 2), (None, 1), ('msg_link', 15), (None, 1),
2044-
('msg_link_index', 2), (None, 1), ('msg_link', 15)]),
2045+
('msg_link_index', 2), (None, 1), ('msg_link', 15)],
2046+
18),
20452047
(OrderedDict([
20462048
('https://example.com', ('https://example.com', 1, False)),
20472049
('http://example.com', ('http://example.com', 2, False)),
20482050
]),
20492051
None,
2050-
None),
2052+
None,
2053+
0),
20512054
(OrderedDict([
20522055
('https://foo.com', ('https://foo.com, Text', 1, True)),
20532056
('https://bar.com', ('Text, https://bar.com', 2, True)),
20542057
]),
20552058
'1: https://foo.com\n2: https://bar.com',
20562059
[('msg_link_index', 2), (None, 1), ('msg_link', 15), (None, 1),
2057-
('msg_link_index', 2), (None, 1), ('msg_link', 15)]),
2060+
('msg_link_index', 2), (None, 1), ('msg_link', 15)],
2061+
18),
20582062
(OrderedDict([
20592063
('https://foo.com', ('Foo!', 1, True)),
20602064
('http://example.com', ('example.com', 2, False)),
20612065
('https://bar.com', ('Bar!', 3, True)),
20622066
]),
20632067
'1: https://foo.com\n3: https://bar.com',
20642068
[('msg_link_index', 2), (None, 1), ('msg_link', 15), (None, 1),
2065-
('msg_link_index', 2), (None, 1), ('msg_link', 15)]),
2069+
('msg_link_index', 2), (None, 1), ('msg_link', 15)],
2070+
18),
20662071
],
20672072
ids=[
20682073
'one_footlink',
@@ -2073,15 +2078,18 @@ def test_reactions_view(self, message_fixture, to_vary_in_each_message):
20732078
]
20742079
)
20752080
def test_footlinks_view(self, message_links, expected_text,
2076-
expected_attrib):
2077-
footlinks = MessageBox.footlinks_view(
2081+
expected_attrib, expected_footlinks_width):
2082+
footlinks, footlinks_width = MessageBox.footlinks_view(
20782083
message_links,
20792084
maximum_footlinks=3,
2085+
padded=True,
2086+
wrap='ellipsis',
20802087
)
20812088

20822089
if expected_text:
20832090
assert footlinks.original_widget.text == expected_text
20842091
assert footlinks.original_widget.attrib == expected_attrib
2092+
assert footlinks_width == expected_footlinks_width
20852093
else:
20862094
assert footlinks is None
20872095
assert not hasattr(footlinks, 'original_widget')
@@ -2096,9 +2104,11 @@ def test_footlinks_limit(self, maximum_footlinks, expected_instance):
20962104
('https://github.com/zulip/zulip-terminal', ('ZT', 1, True)),
20972105
])
20982106

2099-
footlinks = MessageBox.footlinks_view(
2107+
footlinks, _ = MessageBox.footlinks_view(
21002108
message_links,
21012109
maximum_footlinks=maximum_footlinks,
2110+
padded=True,
2111+
wrap='ellipsis',
21022112
)
21032113

21042114
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,
910
EditHistoryView,
@@ -626,6 +627,31 @@ def test_markup_descrption(self, rendered_description, expected_markup,
626627

627628
assert stream_info_view.markup_desc == expected_markup
628629

630+
@pytest.mark.parametrize(['message_links', 'expected_text',
631+
'expected_attrib', 'expected_footlinks_width'], [
632+
(OrderedDict([
633+
('https://example.com', ('Example', 1, True)),
634+
('https://generic.com', ('Generic', 2, True)),
635+
]),
636+
'1: https://example.com\n2: https://generic.com',
637+
[('msg_link_index', 2), (None, 1), ('msg_link', 19), (None, 1),
638+
('msg_link_index', 2), (None, 1), ('msg_link', 19)],
639+
22),
640+
],
641+
)
642+
def test_footlinks(self, message_links, expected_text, expected_attrib,
643+
expected_footlinks_width):
644+
footlinks, footlinks_width = MessageBox.footlinks_view(
645+
message_links,
646+
maximum_footlinks=10,
647+
padded=False,
648+
wrap='space',
649+
)
650+
651+
assert footlinks.text == expected_text
652+
assert footlinks.attrib == expected_attrib
653+
assert footlinks_width == expected_footlinks_width
654+
629655
@pytest.mark.parametrize('key', {*keys_for_command('GO_BACK'),
630656
*keys_for_command('STREAM_DESC')})
631657
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
@@ -858,34 +858,50 @@ def footlinks_view(
858858
message_links: 'OrderedDict[str, Tuple[str, int, bool]]',
859859
*,
860860
maximum_footlinks: int,
861-
) -> Any:
861+
padded: bool,
862+
wrap: str
863+
) -> Tuple[Any, int]:
864+
"""
865+
Returns a Tuple that consists footlinks view (widget) and its required
866+
width.
867+
"""
862868
# Return if footlinks are disabled by the user.
863869
if maximum_footlinks == 0:
864-
return None
870+
return None, 0
865871

866872
footlinks = []
867873
counter = 0
874+
footlinks_width = 0
868875
for link, (text, index, show_footlink) in message_links.items():
869876
if counter == maximum_footlinks:
870877
break
871878
if not show_footlink:
872879
continue
873880

874881
counter += 1
875-
footlinks.extend([
882+
styled_footlink = [
876883
('msg_link_index', f"{index}:"),
877-
' ',
884+
(None, " "),
878885
('msg_link', link),
879-
'\n',
880-
])
886+
]
887+
footlinks_width = max(
888+
footlinks_width,
889+
sum([len(text) for style, text in styled_footlink])
890+
)
891+
footlinks.extend([*styled_footlink, '\n'])
881892

882893
if not footlinks:
883-
return None
894+
return None, 0
884895

885896
footlinks[-1] = footlinks[-1][:-1] # Remove the last newline.
886-
return urwid.Padding(urwid.Text(footlinks, wrap='ellipsis'),
887-
align='left', left=8, width=('relative', 100),
888-
min_width=10, right=2)
897+
898+
text_widget = urwid.Text(footlinks, wrap=wrap)
899+
if padded:
900+
return urwid.Padding(text_widget, align='left', left=8,
901+
width=('relative', 100), min_width=10,
902+
right=2), footlinks_width
903+
else:
904+
return text_widget, footlinks_width
889905

890906
@classmethod
891907
def soup2markup(cls, soup: Any, metadata: Dict[str, Any],
@@ -1229,9 +1245,11 @@ def main_view(self) -> List[Any]:
12291245
reactions = self.reactions_view(self.message['reactions'])
12301246

12311247
# Footlinks.
1232-
footlinks = self.footlinks_view(
1248+
footlinks, _ = self.footlinks_view(
12331249
self.message_links,
12341250
maximum_footlinks=self.model.controller.maximum_footlinks,
1251+
padded=True,
1252+
wrap='ellipsis',
12351253
)
12361254

12371255
# Build parts together and return

zulipterminal/ui_tools/views.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1107,7 +1107,7 @@ def __init__(self, controller: Any, stream_id: int) -> None:
11071107
else STREAM_MARKER_PUBLIC)
11081108
title = f"{stream_marker} {stream['name']}"
11091109
rendered_desc = stream['rendered_description']
1110-
self.markup_desc, *_ = MessageBox.transform_content(
1110+
self.markup_desc, message_links, _ = MessageBox.transform_content(
11111111
rendered_desc,
11121112
self.controller.model.server_url,
11131113
)
@@ -1137,16 +1137,28 @@ def __init__(self, controller: Any, stream_id: int) -> None:
11371137
urwid.connect_signal(pinned_setting, 'change',
11381138
self.toggle_pinned_status)
11391139

1140+
footlinks, footlinks_width = MessageBox.footlinks_view(
1141+
message_links=message_links,
1142+
maximum_footlinks=10, # Show 'all', as no other way to add them
1143+
padded=False,
1144+
wrap='space',
1145+
)
1146+
11401147
# Manual because calculate_table_widths does not support checkboxes.
11411148
# Add 4 to checkbox label to accommodate the checkbox itself.
11421149
popup_width = max(popup_width, len(muted_setting.label) + 4,
1143-
len(pinned_setting.label) + 4, desc.pack()[0])
1150+
len(pinned_setting.label) + 4, desc.pack()[0],
1151+
footlinks_width)
11441152
self.widgets = self.make_table_with_categories(stream_info_content,
11451153
column_widths)
11461154

11471155
# Stream description.
11481156
self.widgets.insert(0, desc)
1149-
self.widgets.insert(1, urwid.Text('')) # Add a newline.
1157+
desc_newline = 1
1158+
if footlinks:
1159+
self.widgets.insert(1, footlinks)
1160+
desc_newline = 2
1161+
self.widgets.insert(desc_newline, urwid.Text('')) # Add a newline.
11501162

11511163
self.widgets.append(muted_setting)
11521164
self.widgets.append(pinned_setting)

0 commit comments

Comments
 (0)