Fixed webbrowser behavior for terminal browsers.
This commit is contained in:
@@ -1,5 +1,7 @@
|
|||||||
import sys
|
import sys
|
||||||
import os
|
import os
|
||||||
|
import curses
|
||||||
|
import webbrowser
|
||||||
import subprocess
|
import subprocess
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from tempfile import NamedTemporaryFile
|
from tempfile import NamedTemporaryFile
|
||||||
@@ -28,10 +30,12 @@ def open_editor(data=''):
|
|||||||
fp.flush()
|
fp.flush()
|
||||||
editor = os.getenv('RTV_EDITOR') or os.getenv('EDITOR') or 'nano'
|
editor = os.getenv('RTV_EDITOR') or os.getenv('EDITOR') or 'nano'
|
||||||
|
|
||||||
|
curses.endwin()
|
||||||
try:
|
try:
|
||||||
subprocess.Popen([editor, fp.name]).wait()
|
subprocess.Popen([editor, fp.name]).wait()
|
||||||
except OSError as e:
|
except OSError as e:
|
||||||
raise ProgramError(editor)
|
raise ProgramError(editor)
|
||||||
|
curses.doupdate()
|
||||||
|
|
||||||
# Open a second file object to read. This appears to be necessary in
|
# 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
|
# 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):
|
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
|
For browsers requiring an X display, we call webbrowser.open_new_tab(url)
|
||||||
console. See http://bugs.python.org/issue22277 for a better description
|
and redirect stdout/stderr to devnull. This is a workaround to stop firefox
|
||||||
of the problem.
|
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]
|
console_browsers = ['www-browser', 'links', 'links2', 'elinks', 'lynx', 'w3m']
|
||||||
with open(os.devnull, 'ab+', 0) as null:
|
|
||||||
subprocess.check_call(args, stdout=null, stderr=null)
|
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):
|
def clean(string, n_cols=None):
|
||||||
|
|||||||
@@ -398,9 +398,7 @@ class BasePage(object):
|
|||||||
curses.flash()
|
curses.flash()
|
||||||
return
|
return
|
||||||
|
|
||||||
curses.endwin()
|
|
||||||
text = open_editor(info)
|
text = open_editor(info)
|
||||||
curses.doupdate()
|
|
||||||
if text == content:
|
if text == content:
|
||||||
show_notification(self.stdscr, ['Aborted'])
|
show_notification(self.stdscr, ['Aborted'])
|
||||||
return
|
return
|
||||||
|
|||||||
@@ -108,9 +108,7 @@ class SubmissionPage(BasePage):
|
|||||||
type=data['type'].lower(),
|
type=data['type'].lower(),
|
||||||
content=content)
|
content=content)
|
||||||
|
|
||||||
curses.endwin()
|
|
||||||
comment_text = open_editor(comment_info)
|
comment_text = open_editor(comment_info)
|
||||||
curses.doupdate()
|
|
||||||
if not comment_text:
|
if not comment_text:
|
||||||
show_notification(self.stdscr, ['Aborted'])
|
show_notification(self.stdscr, ['Aborted'])
|
||||||
return
|
return
|
||||||
|
|||||||
Reference in New Issue
Block a user