diff --git a/rtv/helpers.py b/rtv/helpers.py index 058d8cb..36b1689 100644 --- a/rtv/helpers.py +++ b/rtv/helpers.py @@ -1,5 +1,7 @@ import sys import os +import curses +import webbrowser import subprocess from datetime import datetime from tempfile import NamedTemporaryFile @@ -28,10 +30,12 @@ def open_editor(data=''): fp.flush() editor = os.getenv('RTV_EDITOR') or os.getenv('EDITOR') or 'nano' + curses.endwin() try: subprocess.Popen([editor, fp.name]).wait() except OSError as e: raise ProgramError(editor) + curses.doupdate() # Open a second file object to read. This appears to be necessary in # order to read the changes made by some editors (gedit). w+ mode does @@ -45,16 +49,44 @@ def open_editor(data=''): def open_browser(url): """ - Call webbrowser.open_new_tab(url) and redirect stdout/stderr to devnull. + Open the given url using the default webbrowser. The preferred browser can + specified with the $BROWSER environment variable. If not specified, python + webbrowser will try to determine the default to use based on your system. - This is a workaround to stop firefox from spewing warning messages to the - console. See http://bugs.python.org/issue22277 for a better description - of the problem. + For browsers requiring an X display, we call webbrowser.open_new_tab(url) + and redirect stdout/stderr to devnull. This is a workaround to stop firefox + from spewing warning messages to the console. See + http://bugs.python.org/issue22277 for a better description of the problem. + + For console browsers (e.g. w3m), RTV will suspend and display the browser + window within the same terminal. This mode is triggered either when + 1. $BROWSER is set to a known console browser, or + 2. $DISPLAY is undefined, indicating that the terminal is running headless + + There may be other cases where console browsers are opened (xdg-open?) but + are not detected here. """ - command = "import webbrowser; webbrowser.open_new_tab('%s')" % url - args = [sys.executable, '-c', command] - with open(os.devnull, 'ab+', 0) as null: - subprocess.check_call(args, stdout=null, stderr=null) + + console_browsers = ['www-browser', 'links', 'links2', 'elinks', 'lynx', 'w3m'] + + display = bool(os.environ.get("DISPLAY")) + + # Use the convention defined here to parse $BROWSER + # https://docs.python.org/2/library/webbrowser.html + if "BROWSER" in os.environ: + user_browser = os.environ["BROWSER"].split(os.pathsep)[0] + if user_browser in console_browsers: + display = False + + if display: + command = "import webbrowser; webbrowser.open_new_tab('%s')" % url + args = [sys.executable, '-c', command] + with open(os.devnull, 'ab+', 0) as null: + subprocess.check_call(args, stdout=null, stderr=null) + else: + curses.endwin() + webbrowser.open_new_tab(url) + curses.doupdate() def clean(string, n_cols=None): diff --git a/rtv/page.py b/rtv/page.py index 006ce25..fce6fc2 100644 --- a/rtv/page.py +++ b/rtv/page.py @@ -398,9 +398,7 @@ class BasePage(object): curses.flash() return - curses.endwin() text = open_editor(info) - curses.doupdate() if text == content: show_notification(self.stdscr, ['Aborted']) return diff --git a/rtv/submission.py b/rtv/submission.py index 310aa25..bf23ecb 100644 --- a/rtv/submission.py +++ b/rtv/submission.py @@ -108,9 +108,7 @@ class SubmissionPage(BasePage): type=data['type'].lower(), content=content) - curses.endwin() comment_text = open_editor(comment_info) - curses.doupdate() if not comment_text: show_notification(self.stdscr, ['Aborted']) return