Skip to content

Commit

Permalink
core/helper: Suppress stderr and stdout for view_in_browser.
Browse files Browse the repository at this point in the history
  • Loading branch information
punchagan authored and preetmishra committed Jun 19, 2020
1 parent 3c9cfb1 commit 7efdafd
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
8 changes: 6 additions & 2 deletions zulipterminal/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
import zulip

from zulipterminal.config.themes import ThemeSpec
from zulipterminal.helper import MACOS, WSL, Message, asynch, near_message_url
from zulipterminal.helper import (
MACOS, WSL, Message, asynch, near_message_url, suppress_output,
)
from zulipterminal.model import Model
from zulipterminal.ui import Screen, View
from zulipterminal.ui_tools.utils import create_msg_box_list
Expand Down Expand Up @@ -281,7 +283,9 @@ def view_in_browser(self, message: Message) -> None:
self.message_url = near_message_url(self.model.server_url[:-1],
message)

webbrowser.open(self.message_url)
with suppress_output():
# Suppress anything on stdout & stderr when opening the browser.
webbrowser.open(self.message_url)

def _finalize_show(self, w_list: List[Any]) -> None:
focus_position = self.model.get_focus_in_current_narrow()
Expand Down
24 changes: 22 additions & 2 deletions zulipterminal/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
import time
import urllib
from collections import OrderedDict, defaultdict
from contextlib import contextmanager
from functools import wraps
from itertools import chain, combinations
from re import ASCII, match
from threading import Thread
from typing import (
Any, Callable, DefaultDict, Dict, FrozenSet, Iterable, List, Set, Tuple,
TypeVar, Union,
Any, Callable, DefaultDict, Dict, FrozenSet, Iterable, Iterator, List, Set,
Tuple, TypeVar, Union,
)

import lxml.html
Expand Down Expand Up @@ -650,3 +651,22 @@ def near_message_url(server_url: str, message: Message) -> str:
else:
url = near_pm_message_url(server_url, message)
return url


@contextmanager
def suppress_output() -> Iterator[None]:
"""
Context manager to redirect stdout and stderr to /dev/null.
Adapted from https://stackoverflow.com/a/2323563.
"""
out = os.dup(1)
err = os.dup(2)
os.close(1)
os.close(2)
os.open(os.devnull, os.O_RDWR)
try:
yield
finally:
os.dup2(out, 1)
os.dup2(err, 2)

0 comments on commit 7efdafd

Please sign in to comment.