Commits

Yuya Nishihara committed 80772b7

cmdui: prettify command-line string for display (closes #1529)

Currently "Output Log" window shows the full command-line arguments, but
it isn't nice for CLI learners.

This patch removes obvious options like --repository from display string,
and escapes common characters.

Comments (0)

Files changed (1)

tortoisehg/hgqt/cmdui.py

             pm.unknown()
 
 
+def _quotecmdarg(arg):
+    # only for display; no use to construct command string for os.system()
+    if not arg or ' ' in arg or '\\' in arg or '"' in arg:
+        return '"%s"' % arg.replace('"', '\\"')
+    else:
+        return arg
+
+def _prettifycmdline(cmdline):
+    r"""Build pretty command-line string for display
+
+    >>> _prettifycmdline(['--repository', 'foo', 'status'])
+    'status'
+    >>> _prettifycmdline(['--cwd', 'foo', 'resolve', '--', '--repository'])
+    'resolve -- --repository'
+    >>> _prettifycmdline(['log', 'foo\\bar', '', 'foo bar', 'foo"bar'])
+    'log "foo\\bar" "" "foo bar" "foo\\"bar"'
+    """
+    try:
+        argcount = cmdline.index('--')
+    except ValueError:
+        argcount = len(cmdline)
+    printables = []
+    pos = 0
+    while pos < argcount:
+        if cmdline[pos] in ('-R', '--repository', '--cwd'):
+            pos += 2
+        else:
+            printables.append(cmdline[pos])
+            pos += 1
+    printables.extend(cmdline[argcount:])
+
+    return ' '.join(_quotecmdarg(e) for e in printables)
+
 class Core(QObject):
     """Core functionality for running Mercurial command.
     Do not attempt to instantiate and use this directly.
             if display:
                 cmd = '%% hg %s\n' % display
             else:
-                cmd = '%% hg %s\n' % ' '.join(cmdline)
+                cmd = '%% hg %s\n' % _prettifycmdline(cmdline)
             self.output.emit(cmd, 'control')
             proc.start(exepath, cmdline, QIODevice.ReadOnly)
 
 
         cmdline = self.queue.pop(0)
 
-        self.thread = thread.CmdThread(cmdline, self.display, self.parent())
+        display = self.display or _prettifycmdline(cmdline)
+        self.thread = thread.CmdThread(cmdline, display, self.parent())
         self.thread.started.connect(self.onCommandStarted)
         self.thread.commandFinished.connect(self.onThreadFinished)
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.