Override text browser with 'O'

This commit is contained in:
Arun Prakash Jana 2018-02-18 22:51:23 +05:30
parent f3ec54692f
commit e43c0ea58d
No known key found for this signature in database
GPG Key ID: A75979F35C080412
3 changed files with 34 additions and 3 deletions

View File

@ -265,6 +265,7 @@ PROMPT KEYS:
append, set, remove (all or specific) tags append, set, remove (all or specific) tags
w [editor|id] edit and add or update a bookmark w [editor|id] edit and add or update a bookmark
c id copy url at search result index to clipboard c id copy url at search result index to clipboard
O toggle try to open in a GUI browser
? show this help ? show this help
q, ^D, double Enter exit buku q, ^D, double Enter exit buku
``` ```

3
buku.1
View File

@ -314,6 +314,9 @@ Edit and add or update a bookmark.
.BI "c id" .BI "c id"
Copy url at search result index to clipboard. Copy url at search result index to clipboard.
.TP .TP
.BI "O"
Toggles ignore text-based browsers. If enabled, tries to open urls in a GUI based browser (even if BROWSER is set).
.TP
.BI "?" .BI "?"
Show help on prompt keys. Show help on prompt keys.
.TP .TP

33
buku.py
View File

@ -76,6 +76,7 @@ COLORMAP = {k: '\x1b[%sm' % v for k, v in {
USER_AGENT = 'Buku/{} (textmode; Linux x86_64; 1024x768)'.format(__version__) USER_AGENT = 'Buku/{} (textmode; Linux x86_64; 1024x768)'.format(__version__)
myheaders = None # Default dictionary of headers myheaders = None # Default dictionary of headers
myproxy = None # Default proxy myproxy = None # Default proxy
text_browsers = ['elinks', 'links', 'links2', 'lynx', 'w3m', 'www-browser']
# Set up logging # Set up logging
logger = logging.getLogger() logger = logging.getLogger()
@ -2436,6 +2437,7 @@ PROMPT KEYS:
append, set, remove (all or specific) tags append, set, remove (all or specific) tags
w [editor|id] edit and add or update a bookmark w [editor|id] edit and add or update a bookmark
c id copy url at search result index to clipboard c id copy url at search result index to clipboard
O toggle try to open in a GUI browser
? show this help ? show this help
q, ^D, double Enter exit buku q, ^D, double Enter exit buku
@ -3065,7 +3067,7 @@ def taglist_subprompt(obj, noninteractive=False):
new_results = False new_results = False
elif nav == 't': elif nav == 't':
new_results = True new_results = True
elif (nav == 'q' or nav == 'd' or nav == '?' or elif (nav == 'q' or nav == 'd' or nav == '?' or nav == 'O' or
nav.startswith(('s ', 'S ', 'r ', 't ', 'o ', 'p ', 'g ', 'w ', 'c ')) or nav == 'w'): nav.startswith(('s ', 'S ', 'r ', 't ', 'o ', 'p ', 'g ', 'w ', 'c ')) or nav == 'w'):
return nav return nav
else: else:
@ -3176,6 +3178,12 @@ def prompt(obj, results, noninteractive=False, deep=False, subprompt=False, sugg
continue continue
# Toggle GUI browser with 'O'
if nav == 'O':
browse.override_text_browser = not browse.override_text_browser
print('text browser override toggled')
continue
# Show help with '?' # Show help with '?'
if nav == '?': if nav == '?':
ExtendedArgumentParser.prompt_help(sys.stdout) ExtendedArgumentParser.prompt_help(sys.stdout)
@ -3482,6 +3490,8 @@ def browse(url):
suppress_browser_output : bool suppress_browser_output : bool
True if a text based browser is detected. True if a text based browser is detected.
Must be initialized (as applicable) to use the API. Must be initialized (as applicable) to use the API.
override_text_browser : bool
If True, tries to open links in a GUI based browser.
""" """
if not parse_url(url).scheme: if not parse_url(url).scheme:
@ -3492,6 +3502,17 @@ def browse(url):
logerr('scheme missing in URI, trying http') logerr('scheme missing in URI, trying http')
url = 'http://' + url url = 'http://' + url
browser = webbrowser.get()
if browse.override_text_browser:
browser_output = browse.suppress_browser_output
for name in [b for b in webbrowser._tryorder if b not in text_browsers]:
browser = webbrowser.get(name)
logdbg(browser)
# Found a GUI browser, suppress browser output
browse.suppress_browser_output = True
break
if browse.suppress_browser_output: if browse.suppress_browser_output:
_stderr = os.dup(2) _stderr = os.dup(2)
os.close(2) os.close(2)
@ -3502,7 +3523,7 @@ def browse(url):
os.dup2(fd, 1) os.dup2(fd, 1)
try: try:
if sys.platform != 'win32': if sys.platform != 'win32':
webbrowser.open(url, new=2) browser.open(url, new=2)
else: else:
# On Windows, the webbrowser module does not fork. # On Windows, the webbrowser module does not fork.
# Use threads instead. # Use threads instead.
@ -3519,6 +3540,9 @@ def browse(url):
os.dup2(_stderr, 2) os.dup2(_stderr, 2)
os.dup2(_stdout, 1) os.dup2(_stdout, 1)
if browse.override_text_browser:
browse.suppress_browser_output = browser_output
def check_upstream_release(): def check_upstream_release():
"""Check and report the latest upstream release version.""" """Check and report the latest upstream release version."""
@ -4250,11 +4274,14 @@ POSITIONAL ARGUMENTS:
bdb.add_rec(url, title_in, tags, desc_in, args.immutable) bdb.add_rec(url, title_in, tags, desc_in, args.immutable)
# Enable browser output in case of a text based browser # Enable browser output in case of a text based browser
if os.getenv('BROWSER') in ['elinks', 'links', 'lynx', 'w3m', 'links2']: if os.getenv('BROWSER') in text_browsers:
browse.suppress_browser_output = False browse.suppress_browser_output = False
else: else:
browse.suppress_browser_output = True browse.suppress_browser_output = True
# Overriding text browsers is disabled by default
browse.override_text_browser = False
# Search record # Search record
search_results = None search_results = None
search_opted = True search_opted = True