jarun / ddgr

:duck: DuckDuckGo from the terminal

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Flag for search url: return or open in Browser

mcint opened this issue · comments

I appreciate your project, and the fluid ease it provides for working on the command line.

I'm hoping to enable a slightly different workflow, by having ddgr open the search page in a browser, so that multi-tab, history, saving pages, and ~browser-things can be done from a dedicated browser. I'm specifically interested in using a terminal browser, and it seems that the behavior I hope for is not quite supported by any of this almost-promising subset:

  -j, --ducky           open the first result in a web browser; implies --np
  -w SITE, --site SITE  search sites using DuckDuckGo
  -x, --expand          Show complete url in search results
  --json                output in JSON format; implies --np
  --gb, --gui-browser   open a bang directly in gui browser
  --np, --noprompt      perform search and exit, do not prompt
  --url-handler UTIL    custom script or cli utility to open results

or others, but serves a shared goal of getting to the results you want, more declaratively, and in fewer interactive steps. I'm curious about use of --url-handler as I hold out some hope that might suffice by itself.

I'm looking into what the PR would require. Interested in feedback on the idea, and thoughts or help on implementation and complexities.

Most of ddgr code is to parse & display the results. Surfraw or S might be worth a try, though they don't seem to have as many options for search parameters as ddgr has.


To stay with ddgr:
You could use a Bang with the --noprompt option: ddgr --np '!html' your-search-term
Caveats: This does a redirect (or two/three), and many other options are ignored. 🙁

To make it work nicely in ddgr:
A new cmdline option is required in parse_args.
Then an update in main to check the option and call open_url.

@@ -2008,6 +2008,7 @@ def parse_args(args=None, namespace=None):
     addarg('--json', action='store_true', help='output in JSON format; implies --np')
     addarg('--gb', '--gui-browser', dest='gui_browser', action='store_true', help='open a bang directly in gui browser')
     addarg('--np', '--noprompt', dest='noninteractive', action='store_true', help='perform search and exit, do not prompt')
+    addarg('-o', '--offload', dest='offload', action='store_true', help='open results page directly in browser')
     addarg('--url-handler', metavar='UTIL', help='custom script or cli utility to open results')
     addarg('--show-browser-logs', action='store_true', help='do not suppress browser output (stdout and stderr)')
     addarg('-v', '--version', action='version', version=_VERSION_)
@@ -2066,9 +2067,15 @@ def main():
     try:
         repl = DdgCmd(opts, '' if opts.noua else USER_AGENT)
 
-        if opts.json or opts.ducky or opts.noninteractive:
+        if opts.json or opts.ducky or opts.noninteractive or opts.offload:
+            # Offload search to browser
+            if opts.offload:
+              url = 'https://html.duckduckgo.com/html/?'
+              query = { k:v for k,v in repl._ddg_url.query().items() if v != '' }
+              url += urllib.parse.urlencode(query)
+              open_url(url)
             # Non-interactive mode
-            if repl.keywords and (
+            elif repl.keywords and (
                     repl.keywords[0][0] == '!' or
                     (len(repl.keywords[0]) > 1 and repl.keywords[0][1] == '!')
             ):

It's a start, though not everyone will want the html only page. Perhaps the option should be adjusted to accept a value, html / lite / js. Or use js version only when --gui-browser is present?

commented

by having ddgr open the search page in a browser

Somehow I missed it. Just set the environment variable BROWSER to your preferred browser. See https://github.com/jarun/ddgr#text-based-browser-integration

Note that BROWSER can be set to non-text browsers as well.

commented

The other option is --url-handler UTIL custom script or cli utility to open results as you have noted.