Patchwork D3650: serve: add an option to open in the default browser

login
register
mail settings
Submitter phabricator
Date May 24, 2018, 1:35 p.m.
Message ID <differential-rev-PHID-DREV-icpxxkllxsogchtmeb7k-req@phab.mercurial-scm.org>
Download mbox | patch
Permalink /patch/31832/
State New
Headers show

Comments

phabricator - May 24, 2018, 1:35 p.m.
nspanti-logilab created this revision.
Herald added a subscriber: mercurial-devel.
Herald added a reviewer: hg-reviewers.

REVISION SUMMARY
  It is inspired by a similar functionality in pydoc.

REPOSITORY
  rHG Mercurial

BRANCH
  default

REVISION DETAIL
  https://phab.mercurial-scm.org/D3650

AFFECTED FILES
  mercurial/commands.py
  mercurial/hgweb/__init__.py
  tests/test-completion.t

CHANGE DETAILS




To: nspanti-logilab, #hg-reviewers
Cc: mercurial-devel
phabricator - May 31, 2018, 7:15 p.m.
durin42 added a comment.


  I don't love the flag name, and am -0 on the feature in general. All terminal emulators I've used in the last 10 years have URL detection logic and make clicking the link trivial...

REPOSITORY
  rHG Mercurial

REVISION DETAIL
  https://phab.mercurial-scm.org/D3650

To: nspanti-logilab, #hg-reviewers
Cc: durin42, mercurial-devel
phabricator - June 2, 2018, 1:04 p.m.
av6 added a comment.


  https://phab.mercurial-scm.org/D3649 mentions `xdg-open "$(hg serve --print-url)"`, which shows that --print-url can nearly replace --graphical and is more versatile.

REPOSITORY
  rHG Mercurial

REVISION DETAIL
  https://phab.mercurial-scm.org/D3650

To: nspanti-logilab, #hg-reviewers
Cc: av6, durin42, mercurial-devel
phabricator - June 14, 2018, 7:47 p.m.
pulkit added a comment.


  Do we need this patch any more since https://phab.mercurial-scm.org/D3649 has landed and we can do `xdg-open "$(hg serve --print-url)"`?

REPOSITORY
  rHG Mercurial

REVISION DETAIL
  https://phab.mercurial-scm.org/D3650

To: nspanti-logilab, #hg-reviewers
Cc: pulkit, av6, durin42, mercurial-devel
phabricator - June 15, 2018, 8:40 a.m.
nspanti-logilab added a comment.


  For laziness,  this patch could be also useful. It is also better for text shell beginners, because they may not know that they can combine commands like that (even if it is basic). Moreover, you can still use `hg serve` in foreground or use it in background and give the result of `--print-url` to a non web browser application (but I imagine that there is a way to duplicate stdout to multiple programs). These are the advantages of this patch that I see over `--print-url` of https://phab.mercurial-scm.org/D3649. It is better for convenience and require less knowledge for the user, but it is not a need. https://phab.mercurial-scm.org/D3649 was precisely suggested to me because it can fulfill the functionality of the current patch and is more versatile.

REPOSITORY
  rHG Mercurial

REVISION DETAIL
  https://phab.mercurial-scm.org/D3650

To: nspanti-logilab, #hg-reviewers
Cc: pulkit, av6, durin42, mercurial-devel
Yuya Nishihara - June 15, 2018, 1:01 p.m.
On Fri, 15 Jun 2018 08:40:33 +0000, nspanti-logilab (Nicola Spanti) wrote:
>   For laziness,  this patch could be also useful. It is also better for text shell beginners, because they may not know that they can combine commands like that (even if it is basic). Moreover, you can still use `hg serve` in foreground or use it in background and give the result of `--print-url` to a non web browser application (but I imagine that there is a way to duplicate stdout to multiple programs). These are the advantages of this patch that I see over `--print-url` of https://phab.mercurial-scm.org/D3649. It is better for convenience and require less knowledge for the user, but it is not a need. https://phab.mercurial-scm.org/D3649 was precisely suggested to me because it can fulfill the functionality of the current patch and is more versatile.

For laziness, we can add it to `[alias]`. I'm not a fan of adding lots of
one-off options since that's the source of man page bloat.

How about document the xdg-open example in `hg help serve -v`?
Yuya Nishihara - June 16, 2018, 1:21 p.m.
(Resend because I forgot to remove the "someone wrote:" line again, sorry.)

>   For laziness,  this patch could be also useful. It is also better for text shell beginners, because they may not know that they can combine commands like that (even if it is basic).

For laziness, we can add it to `[alias]`. I'm not a fan of adding lots of
one-off options since that's the source of man page bloat.

How about document the xdg-open example in `hg help serve -v`?
phabricator - June 16, 2018, 1:21 p.m.
yuja added a comment.


  (Resend because I forgot to remove the "someone wrote:" line again, sorry.)
  
  >   For laziness,  this patch could be also useful. It is also better for text shell beginners, because they may not know that they can combine commands like that (even if it is basic).
  
  For laziness, we can add it to `[alias]`. I'm not a fan of adding lots of
  one-off options since that's the source of man page bloat.
  
  How about document the xdg-open example in `hg help serve -v`?

REPOSITORY
  rHG Mercurial

REVISION DETAIL
  https://phab.mercurial-scm.org/D3650

To: nspanti-logilab, #hg-reviewers
Cc: yuja, pulkit, av6, durin42, mercurial-devel

Patch

diff --git a/tests/test-completion.t b/tests/test-completion.t
--- a/tests/test-completion.t
+++ b/tests/test-completion.t
@@ -239,7 +239,7 @@ 
   pull: update, force, rev, bookmark, branch, ssh, remotecmd, insecure
   push: force, rev, bookmark, branch, new-branch, pushvars, ssh, remotecmd, insecure
   remove: after, force, subrepos, include, exclude, dry-run
-  serve: accesslog, daemon, daemon-postexec, errorlog, port, address, prefix, name, web-conf, webdir-conf, pid-file, stdio, cmdserver, templates, style, ipv6, certificate, subrepos
+  serve: accesslog, daemon, daemon-postexec, graphical, errorlog, port, address, prefix, name, web-conf, webdir-conf, pid-file, stdio, cmdserver, templates, style, ipv6, certificate, subrepos
   status: all, modified, added, removed, deleted, clean, unknown, ignored, no-status, terse, copies, print0, rev, change, include, exclude, subrepos, template
   summary: remote
   update: clean, check, merge, date, rev, tool
diff --git a/mercurial/hgweb/__init__.py b/mercurial/hgweb/__init__.py
--- a/mercurial/hgweb/__init__.py
+++ b/mercurial/hgweb/__init__.py
@@ -9,6 +9,7 @@ 
 from __future__ import absolute_import
 
 import os
+import webbrowser
 
 from ..i18n import _
 
@@ -60,6 +61,25 @@ 
         if self.opts['port'] and not self.ui.verbose:
             return
 
+        bindaddr = self._getbindaddr()
+
+        if self.opts['port']:
+            write = self.ui.status
+        else:
+            write = self.ui.write
+        write(_('listening at %s (bound to %s:%d)\n') %
+              (self._geturl(), pycompat.sysbytes(bindaddr), self.httpd.port))
+        self.ui.flush()  # avoid buffering of status message
+
+    def _getbindaddr(self):
+        bindaddr = self.httpd.addr
+        if bindaddr == r'0.0.0.0':
+            bindaddr = r'*'
+        elif r':' in bindaddr: # IPv6
+            bindaddr = r'[%s]' % bindaddr
+        return bindaddr
+
+    def _geturl(self):
         if self.httpd.prefix:
             prefix = self.httpd.prefix.strip('/') + '/'
         else:
@@ -69,25 +89,18 @@ 
         if port == r':80':
             port = r''
 
-        bindaddr = self.httpd.addr
-        if bindaddr == r'0.0.0.0':
-            bindaddr = r'*'
-        elif r':' in bindaddr: # IPv6
-            bindaddr = r'[%s]' % bindaddr
+        bindaddr = self._getbindaddr()
 
         fqaddr = self.httpd.fqaddr
         if r':' in fqaddr:
             fqaddr = r'[%s]' % fqaddr
-        if self.opts['port']:
-            write = self.ui.status
-        else:
-            write = self.ui.write
-        write(_('listening at http://%s%s/%s (bound to %s:%d)\n') %
-              (pycompat.sysbytes(fqaddr), pycompat.sysbytes(port),
-               prefix, pycompat.sysbytes(bindaddr), self.httpd.port))
-        self.ui.flush()  # avoid buffering of status message
+
+        return 'http://%s%s/%s' % (
+            pycompat.sysbytes(fqaddr), pycompat.sysbytes(port), prefix)
 
     def run(self):
+        if self.opts.pop('gui', None):
+            webbrowser.open(self._geturl())
         self.httpd.serve_forever()
 
 def createapp(baseui, repo, webconf):
diff --git a/mercurial/commands.py b/mercurial/commands.py
--- a/mercurial/commands.py
+++ b/mercurial/commands.py
@@ -4747,6 +4747,7 @@ 
      _('FILE')),
     ('d', 'daemon', None, _('run server in background')),
     ('', 'daemon-postexec', [], _('used internally by daemon mode')),
+    ('g', 'gui', None, _('open in default web browser')),
     ('E', 'errorlog', '', _('name of error log file to write to'), _('FILE')),
     # use string type, then we can check if something was passed
     ('p', 'port', '', _('port to listen on (default: 8000)'), _('PORT')),