From: Jeremy Stanley Date: Mon, 13 May 2019 03:06:42 +0000 (+0000) Subject: Show possible abbreviations in help list X-Git-Tag: 0.0.1~11 X-Git-Url: https://mudpy.org/gitweb?p=mudpy.git;a=commitdiff_plain;h=a93dc77b0d77e622834f524e2060d516b47bbc58 Show possible abbreviations in help list When listing available commands via the help command, show which parts of a given command word are optional to type. Also test it. --- diff --git a/mudpy/command.py b/mudpy/command.py index 5ba3d28..2b20b1a 100644 --- a/mudpy/command.py +++ b/mudpy/command.py @@ -195,23 +195,53 @@ def help(actor, parameters): # no specific command word was indicated else: - # give a sorted list of commands with descriptions if provided - output = "These are the commands available to you:$(eol)$(eol)" - sorted_commands = list(actor.universe.groups["command"].keys()) - sorted_commands.sort() - for item in sorted_commands: - command = actor.universe.groups["command"][item] + # preamble text + output = ("These are the commands available to you [brackets indicate " + "optional portion]:$(eol)$(eol)") + + # list command names in alphabetical order + for command_name, command in sorted( + actor.universe.groups["command"].items()): + + # skip over disallowed commands if actor.can_run(command): - description = command.get("description") - if not description: - description = "(no short description provided)" + + # start incrementing substrings + for position in range(1, len(command_name) + 1): + + # we've found our shortest possible abbreviation + candidate = mudpy.misc.find_command( + command_name[:position]) + try: + if candidate.subkey == command_name: + break + except AttributeError: + pass + + # use square brackets to indicate optional part of command name + if position < len(command_name): + abbrev = "%s[%s]" % ( + command_name[:position], command_name[position:]) + else: + abbrev = command_name + + # supply a useful default if the short description is missing + description = command.get( + "description", "(no short description provided)") + + # administrative command names are in red, others in green if command.get("administrative"): - output += " $(red)" + color = "red" else: - output += " $(grn)" - output += item + "$(nrm) - " + description + "$(eol)" - output += ('$(eol)Enter "help COMMAND" for help on a command ' - 'named "COMMAND".') + color = "grn" + + # format the entry for this command + output = "%s $(%s)%s$(nrm) - %s$(eol)" % ( + output, color, abbrev, description) + + # add a footer with instructions on getting additional information + output = ('%s $(eol)Enter "help COMMAND" for help on a command named ' + '"COMMAND".' % output) # send the accumulated output to the user actor.send(output) diff --git a/mudpy/tests/selftest.py b/mudpy/tests/selftest.py index ddb09f0..56549da 100644 --- a/mudpy/tests/selftest.py +++ b/mudpy/tests/selftest.py @@ -216,9 +216,11 @@ test_admin_help = ( ) test_abbrev = ( - (0, "> ", "help mov"), - (0, r"Move in a specific direction\.", "mov north"), - (0, r"You exit to the north\.", ""), + (0, "> ", "h"), + (0, r"h\[elp\].*m\[ove\].*> ", "he mo"), + (0, r"Move in a specific direction\..*> ", "mov north"), + (0, r"You exit to the north\..*> ", "m south"), + (0, r"You exit to the south\..*> ", ""), ) test_reload = (