diff --git a/tests/core/test_core.py b/tests/core/test_core.py index 2285780f2cf..df75af75510 100644 --- a/tests/core/test_core.py +++ b/tests/core/test_core.py @@ -1,3 +1,4 @@ +import os from platform import platform from typing import Any @@ -229,6 +230,21 @@ def test_show_all_mentions(self, mocker, controller, index_all_mentions): msg_ids = {widget.original_widget.message['id'] for widget in widgets} assert msg_ids == id_list + @pytest.mark.parametrize('server_url', [ + 'https://chat.zulip.org/', + 'https://foo.zulipchat.com/', + ]) + def test_view_in_browser(self, mocker, controller, message_fixture, + server_url): + # Set DISPLAY environ in order to run test in Travis. + os.environ['DISPLAY'] = ':0' + controller.model.server_url = server_url + mocked_open = mocker.patch(CORE + '.webbrowser.open') + + controller.view_in_browser(message_fixture) + + mocked_open.assert_called_once_with(controller.message_url) + def test_main(self, mocker, controller): ret_mock = mocker.Mock() mock_loop = mocker.patch('urwid.MainLoop', return_value=ret_mock) diff --git a/zulipterminal/core.py b/zulipterminal/core.py index 6fba628fa96..6e6aae1f76f 100644 --- a/zulipterminal/core.py +++ b/zulipterminal/core.py @@ -2,6 +2,7 @@ import signal import sys import time +import webbrowser from functools import partial from platform import platform from typing import Any, List, Optional, Tuple @@ -10,7 +11,7 @@ import zulip from zulipterminal.config.themes import ThemeSpec -from zulipterminal.helper import Message, asynch +from zulipterminal.helper import MACOS, WSL, Message, asynch, near_message_url from zulipterminal.model import Model from zulipterminal.ui import Screen, View from zulipterminal.ui_tools.utils import create_msg_box_list @@ -270,6 +271,18 @@ def show_all_mentions(self, button: Any) -> None: anchor=None, mentioned=True) + def view_in_browser(self, message: Message) -> None: + # Don't try to open the browser if the client is running without a GUI. + if (not(MACOS or WSL or os.environ.get('DISPLAY')) + and os.environ.get('TERM')): + return + + # Truncate extra '/' from the server url. + self.message_url = near_message_url(self.model.server_url[:-1], + message) + + webbrowser.open(self.message_url) + def _finalize_show(self, w_list: List[Any]) -> None: focus_position = self.model.get_focus_in_current_narrow() if focus_position == set(): diff --git a/zulipterminal/ui_tools/boxes.py b/zulipterminal/ui_tools/boxes.py index 92b116be1c3..41f203d488a 100644 --- a/zulipterminal/ui_tools/boxes.py +++ b/zulipterminal/ui_tools/boxes.py @@ -842,6 +842,8 @@ def keypress(self, size: urwid_Size, key: str) -> Optional[str]: self.model.controller.view.middle_column.set_focus('footer') elif is_command_key('MSG_INFO', key): self.model.controller.show_msg_info(self.message) + elif is_command_key('VIEW_IN_BROWSER', key): + self.model.controller.view_in_browser(self.message) return key