Details
Details
- Reviewers
Alphare pulkit - Group Reviewers
hg-reviewers - Commits
- rHG65cb924a1430: help: extract logic for listing commands and topics
Diff Detail
Diff Detail
- Repository
- rHG Mercurial
- Lint
Lint Skipped - Unit
Unit Tests Skipped
( )
Alphare | |
pulkit |
hg-reviewers |
Lint Skipped |
Unit Tests Skipped |
Path | Packages | |||
---|---|---|---|---|
M | mercurial/help.py (92 lines) |
Commit | Parents | Author | Summary | Date |
---|---|---|---|---|
f26a94873f75 | 5ed6efedc457 | Ludovic Chabant | Sep 30 2020, 3:33 AM |
Status | Author | Revision | |
---|---|---|---|
Needs Revision | ludovicchabant | ||
Closed | ludovicchabant |
def sub(m): | def sub(m): | ||||
lines = [m.group(1) + s for s in repl.splitlines()] | lines = [m.group(1) + s for s in repl.splitlines()] | ||||
return b'\n'.join(lines) | return b'\n'.join(lines) | ||||
return re.sub(br'( *)%s' % re.escape(marker), sub, doc) | return re.sub(br'( *)%s' % re.escape(marker), sub, doc) | ||||
def _getcategorizedhelpcmds(ui, cmdtable, name, select=None): | |||||
# Category -> list of commands | |||||
cats = {} | |||||
# Command -> short description | |||||
h = {} | |||||
# Command -> string showing synonyms | |||||
syns = {} | |||||
for c, e in pycompat.iteritems(cmdtable): | |||||
fs = cmdutil.parsealiases(c) | |||||
f = fs[0] | |||||
syns[f] = fs | |||||
func = e[0] | |||||
if select and not select(f): | |||||
continue | |||||
doc = pycompat.getdoc(func) | |||||
if filtercmd(ui, f, func, name, doc): | |||||
continue | |||||
doc = gettext(doc) | |||||
if not doc: | |||||
doc = _(b"(no help text available)") | |||||
h[f] = doc.splitlines()[0].rstrip() | |||||
cat = getattr(func, 'helpcategory', None) or ( | |||||
registrar.command.CATEGORY_NONE | |||||
) | |||||
cats.setdefault(cat, []).append(f) | |||||
return cats, h, syns | |||||
def _getcategorizedhelptopics(ui, topictable): | |||||
# Group commands by category. | |||||
topiccats = {} | |||||
syns = {} | |||||
for topic in topictable: | |||||
names, header, doc = topic[0:3] | |||||
if len(topic) > 3 and topic[3]: | |||||
category = topic[3] | |||||
else: | |||||
category = TOPIC_CATEGORY_NONE | |||||
topicname = names[0] | |||||
syns[topicname] = list(names) | |||||
if not filtertopic(ui, topicname): | |||||
topiccats.setdefault(category, []).append( | |||||
(topicname, header) | |||||
) | |||||
return topiccats, syns | |||||
addtopichook(b'config', inserttweakrc) | addtopichook(b'config', inserttweakrc) | ||||
def help_( | def help_( | ||||
ui, | ui, | ||||
commands, | commands, | ||||
name, | name, | ||||
unknowncmd=False, | unknowncmd=False, | ||||
b'\n(some details hidden, use --verbose ' | b'\n(some details hidden, use --verbose ' | ||||
b'to show complete help)' | b'to show complete help)' | ||||
) | ) | ||||
) | ) | ||||
return rst | return rst | ||||
def helplist(select=None, **opts): | def helplist(select=None, **opts): | ||||
# Category -> list of commands | cats, h, syns = _getcategorizedhelpcmds(ui, commands.table, name, select) | ||||
cats = {} | |||||
# Command -> short description | |||||
h = {} | |||||
# Command -> string showing synonyms | |||||
syns = {} | |||||
for c, e in pycompat.iteritems(commands.table): | |||||
fs = cmdutil.parsealiases(c) | |||||
f = fs[0] | |||||
syns[f] = b', '.join(fs) | |||||
func = e[0] | |||||
if select and not select(f): | |||||
continue | |||||
doc = pycompat.getdoc(func) | |||||
if filtercmd(ui, f, func, name, doc): | |||||
continue | |||||
doc = gettext(doc) | |||||
if not doc: | |||||
doc = _(b"(no help text available)") | |||||
h[f] = doc.splitlines()[0].rstrip() | |||||
cat = getattr(func, 'helpcategory', None) or ( | |||||
registrar.command.CATEGORY_NONE | |||||
) | |||||
cats.setdefault(cat, []).append(f) | |||||
rst = [] | rst = [] | ||||
if not h: | if not h: | ||||
if not ui.quiet: | if not ui.quiet: | ||||
rst.append(_(b'no commands defined\n')) | rst.append(_(b'no commands defined\n')) | ||||
return rst | return rst | ||||
# Output top header. | # Output top header. | ||||
if not ui.quiet: | if not ui.quiet: | ||||
if name == b"shortlist": | if name == b"shortlist": | ||||
rst.append(_(b'basic commands:\n\n')) | rst.append(_(b'basic commands:\n\n')) | ||||
elif name == b"debug": | elif name == b"debug": | ||||
rst.append(_(b'debug commands (internal and unsupported):\n\n')) | rst.append(_(b'debug commands (internal and unsupported):\n\n')) | ||||
else: | else: | ||||
rst.append(_(b'list of commands:\n')) | rst.append(_(b'list of commands:\n')) | ||||
def appendcmds(cmds): | def appendcmds(cmds): | ||||
cmds = sorted(cmds) | cmds = sorted(cmds) | ||||
for c in cmds: | for c in cmds: | ||||
if ui.verbose: | if ui.verbose: | ||||
rst.append(b" :%s: %s\n" % (syns[c], h[c])) | rst.append(b" :%s: %s\n" % (b', '.join(syns[c]), h[c])) | ||||
else: | else: | ||||
rst.append(b' :%s: %s\n' % (c, h[c])) | rst.append(b' :%s: %s\n' % (c, h[c])) | ||||
if name in (b'shortlist', b'debug'): | if name in (b'shortlist', b'debug'): | ||||
# List without categories. | # List without categories. | ||||
appendcmds(h) | appendcmds(h) | ||||
else: | else: | ||||
# Check that all categories have an order. | # Check that all categories have an order. | ||||
extensions.enabled(), | extensions.enabled(), | ||||
showdeprecated=ui.verbose, | showdeprecated=ui.verbose, | ||||
) | ) | ||||
if exts: | if exts: | ||||
rst.append(b'\n') | rst.append(b'\n') | ||||
rst.extend(exts) | rst.extend(exts) | ||||
rst.append(_(b"\nadditional help topics:\n")) | rst.append(_(b"\nadditional help topics:\n")) | ||||
# Group commands by category. | topiccats, topicsyns = _getcategorizedhelptopics(ui, helptable) | ||||
topiccats = {} | |||||
for topic in helptable: | |||||
names, header, doc = topic[0:3] | |||||
if len(topic) > 3 and topic[3]: | |||||
category = topic[3] | |||||
else: | |||||
category = TOPIC_CATEGORY_NONE | |||||
topicname = names[0] | |||||
if not filtertopic(ui, topicname): | |||||
topiccats.setdefault(category, []).append( | |||||
(topicname, header) | |||||
) | |||||
# Check that all categories have an order. | # Check that all categories have an order. | ||||
missing_order = set(topiccats.keys()) - set(TOPIC_CATEGORY_ORDER) | missing_order = set(topiccats.keys()) - set(TOPIC_CATEGORY_ORDER) | ||||
if missing_order: | if missing_order: | ||||
ui.develwarn( | ui.develwarn( | ||||
b'help categories missing from TOPIC_CATEGORY_ORDER: %s' | b'help categories missing from TOPIC_CATEGORY_ORDER: %s' | ||||
% missing_order | % missing_order | ||||
) | ) |