Skip to content

Commit 9990420

Browse files
committed
Opens message links popup menu
Pressing enter on one of the buttons in links popup menu does one of the following, depending on the link: - narrow to stream - narrow to user - open image in system's default viewer - open link in system's default browser
1 parent 0060a36 commit 9990420

File tree

5 files changed

+97
-11
lines changed

5 files changed

+97
-11
lines changed

zulipterminal/config/keys.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,10 @@
184184
'keys': {'ctrl l'},
185185
'help_text': 'Clear message',
186186
}),
187+
('MSG_LINKS', {
188+
'keys': {'v'},
189+
'help_text': 'View all links in the current message',
190+
}),
187191
]) # type: OrderedDict[str, KeyBinding]
188192

189193

zulipterminal/core.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from zulipterminal.model import Model, GetMessagesArgs, ServerConnectionFailure
1414
from zulipterminal.ui import View, Screen
1515
from zulipterminal.ui_tools.utils import create_msg_box_list
16-
from zulipterminal.ui_tools.views import HelpView
16+
from zulipterminal.ui_tools.views import HelpView, MsgLinksView
1717
from zulipterminal.config.themes import ThemeSpec
1818

1919

@@ -84,27 +84,34 @@ def update_screen(self) -> None:
8484
def draw_screen(self, *args: Any, **kwargs: Any) -> None:
8585
self.loop.draw_screen()
8686

87-
def show_help(self) -> None:
87+
def show_pop_up(self, to_show: Any, title: str) -> None:
8888
double_lines = dict(tlcorner='╔', tline='═', trcorner='╗',
8989
rline='║', lline='║',
9090
blcorner='╚', bline='═', brcorner='╝')
9191
cols, rows = self.loop.screen.get_cols_rows()
92-
help_view = HelpView(self)
9392
self.loop.widget = urwid.Overlay(
94-
urwid.LineBox(help_view,
95-
title="Help Menu (up/down scrolls)",
93+
urwid.LineBox(to_show,
94+
title,
9695
**double_lines),
9796
self.view,
9897
align='center',
9998
valign='middle',
10099
# +2 to both of the following, due to LineBox
101-
width=help_view.width+2,
102-
height=min(3*rows//4, help_view.number_of_actions)+2
100+
width=to_show.width+2,
101+
height=min(3*rows//4, to_show.height)+2
103102
)
104103

105-
def exit_help(self) -> None:
104+
def show_help(self) -> None:
105+
help_view = HelpView(self)
106+
self.show_pop_up(help_view, "Help Menu (up/down scrolls)")
107+
108+
def exit_pop_up(self) -> None:
106109
self.loop.widget = self.view
107110

111+
def show_msg_links(self, msg: Any):
112+
msg_links_view = MsgLinksView(self, msg)
113+
self.show_pop_up(msg_links_view, "Message Links (up/down scrolls)")
114+
108115
def search_messages(self, text: str) -> None:
109116
# Search for a text in messages
110117
self.model.set_narrow(search=text)

zulipterminal/ui_tools/boxes.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ def __init__(self, message: Dict[str, Any], model: Any,
139139
last_message: Any) -> None:
140140
self.model = model
141141
self.message = message
142+
self.message['links'] = dict() # type: Dict
142143
self.caption = ''
143144
self.stream_id = None # type: Union[int, None]
144145
self.title = ''
@@ -373,6 +374,7 @@ def soup2markup(self, soup: Any) -> List[Any]:
373374
'user-group-mention' in element.attrs.get('class', [])):
374375
# USER MENTIONS & USER-GROUP MENTIONS
375376
markup.append(('span', element.text))
377+
self.message['links'][element.text] = element.text
376378
elif element.name == 'a':
377379
# LINKS
378380
link = element.attrs['href']
@@ -383,12 +385,15 @@ def soup2markup(self, soup: Any) -> List[Any]:
383385
# a link then just display the link
384386
markup.append(text)
385387
else:
388+
if link.startswith('user_uploads/'):
389+
link = self.model.server_url + link
386390
if link.startswith('/user_uploads/'):
387391
# Append org url to before user_uploads to convert it
388392
# into a link.
389-
link = self.model.server_url + link
393+
link = self.model.server_url + link[1:]
390394
markup.append(
391395
('link', '[' + text + ']' + '(' + link + ')'))
396+
self.message['links'][text] = link
392397
elif element.name == 'blockquote':
393398
# BLOCKQUOTE TEXT
394399
markup.append((
@@ -680,6 +685,8 @@ def keypress(self, size: Tuple[int, int], key: str) -> str:
680685
write_box.msg_write_box.set_edit_text(msg)
681686
write_box.msg_write_box.set_edit_pos(len(msg))
682687
self.model.controller.view.middle_column.set_focus('footer')
688+
elif is_command_key('MSG_LINKS', key):
689+
self.model.controller.show_msg_links(self.message)
683690
return key
684691

685692

zulipterminal/ui_tools/buttons.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,14 @@ def keypress(self, size: Tuple[int, int], key: str) -> str:
7272
return super().keypress(size, key)
7373

7474

75+
class MsgLinkButton(TopButton):
76+
def __init__(self, controller: Any, width: int, count: int,
77+
caption: str, function: Callable) -> None:
78+
super().__init__(controller, caption,
79+
function, count=count,
80+
width=width)
81+
82+
7583
class HomeButton(TopButton):
7684
def __init__(self, controller: Any, width: int, count: int=0) -> None:
7785
super().__init__(controller, 'All messages',

zulipterminal/ui_tools/views.py

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
)
1818
from zulipterminal.ui_tools.utils import create_msg_box_list
1919
from zulipterminal.ui_tools.boxes import UserSearchBox, StreamSearchBox
20+
from zulipterminal.ui_tools.buttons import MsgLinkButton
2021

2122

2223
class ModListWalker(urwid.SimpleFocusListWalker):
@@ -649,11 +650,70 @@ def __init__(self, controller: Any) -> None:
649650
None if index % 2 else 'help')
650651
for index, binding in enumerate(KEY_BINDINGS.values())])
651652

652-
self.number_of_actions = len(self.log)
653+
self.height = len(self.log)
653654

654655
super(HelpView, self).__init__(self.log)
655656

656657
def keypress(self, size: Tuple[int, int], key: str) -> str:
657658
if is_command_key('GO_BACK', key) or is_command_key('HELP', key):
658-
self.controller.exit_help()
659+
self.controller.exit_pop_up()
659660
return super(HelpView, self).keypress(size, key)
661+
662+
class MsgLinksView(urwid.ListBox):
663+
def __init__(self, controller: Any, msg: Any):
664+
self.controller = controller
665+
self.msg = msg
666+
667+
self.width = 70
668+
self.height = 10
669+
670+
self.log = []
671+
for url in msg['links']:
672+
if not url.startswith('/user_uploads'):
673+
self.log.append(MsgLinkButton(self.controller,
674+
self.width,
675+
0,
676+
url,
677+
self.perform_action))
678+
679+
super(MsgLinksView, self).__init__(self.log)
680+
681+
def perform_action(self, button):
682+
# If stream
683+
if self.msg['links'][button.caption].startswith('/#narrow'):
684+
for stream_details in self.controller.model.stream_dict.values():
685+
# Check mentioned stream with all subscribed streams
686+
if stream_details['name'] == button.caption[1:]:
687+
# Prepare stream button to call function to narrow to stream
688+
req_details = ['name', 'stream_id', 'color', 'invite_only']
689+
stream = [stream_details[key] for key in req_details]
690+
btn = StreamButton(stream,
691+
controller=self.controller,
692+
view=self.controller.view,
693+
width=self.width,
694+
count=0)
695+
self.controller.narrow_to_stream(btn)
696+
break
697+
# If user
698+
elif self.msg['links'][button.caption].startswith('@'):
699+
# print(self.controller.model.users, "\n", button.caption, "\n", flush=True)
700+
for user in self.controller.model.users:
701+
if user['full_name'] == button.caption[1:]:
702+
btn = UserButton(user,
703+
controller=self.controller,
704+
view=self.controller.view,
705+
width=self.width,
706+
color=user['status'],
707+
count=0)
708+
self.controller.narrow_to_user(btn)
709+
break
710+
711+
else:
712+
# Will open images and links in default image viewer and browser
713+
pass
714+
715+
716+
def keypress(self, size: Tuple[int, int], key: str) -> str:
717+
if is_command_key('GO_BACK', key) or is_command_key('MSG_LINKS', key):
718+
self.controller.exit_pop_up()
719+
return super(MsgLinksView, self).keypress(size, key)

0 commit comments

Comments
 (0)