From: Jeremy Stanley Date: Mon, 13 May 2019 20:26:09 +0000 (+0000) Subject: Improve preferences display X-Git-Tag: 0.0.1~6 X-Git-Url: https://mudpy.org/gitweb?a=commitdiff_plain;h=bf6ed46991ffdc587d4e70362a4bd20d6f84fcee;p=mudpy.git Improve preferences display When displaying preferences, color-code the base and admin preferences similar to command help. Also show a placeholder when there is no existing value set for a preference. Add a hyphen separator between preference names and values. Update tests to accommodate the new formatting, and while we're there add a test to make sure non-admins can't set values for admin-only preferences. --- diff --git a/mudpy/command.py b/mudpy/command.py index 20b448c..0ffff97 100644 --- a/mudpy/command.py +++ b/mudpy/command.py @@ -280,22 +280,31 @@ def preferences(actor, parameters): message = "" arguments = parameters.split() allowed_prefs = set() + base_prefs = [] user_config = actor.universe.contents.get("mudpy.user") if user_config: - allowed_prefs.update(user_config.get("pref_allow", [])) + base_prefs = user_config.get("pref_allow", []) + allowed_prefs.update(base_prefs) if actor.owner.account.get("administrator"): allowed_prefs.update(user_config.get("pref_admin", [])) if not arguments: message += "These are your current preferences:" - for pref in allowed_prefs: - message += ("$(eol) $(red)%s $(grn)%s$(nrm)" - % (pref, actor.owner.account.get(pref))) + + # color-code base and admin prefs + for pref in sorted(allowed_prefs): + if pref in base_prefs: + color = "grn" + else: + color = "red" + message += ("$(eol) $(%s)%s$(nrm) - %s" % ( + color, pref, actor.owner.account.get(pref, ""))) + elif arguments[0] not in allowed_prefs: message += ( 'Preference "%s" does not exist. Try the `preferences` command by ' "itself for a list of valid preferences." % arguments[0]) elif len(arguments) == 1: - message += "%s" % actor.owner.account.get(arguments[0]) + message += "%s" % actor.owner.account.get(arguments[0], "") else: pref = arguments[0] value = " ".join(arguments[1:]) diff --git a/mudpy/tests/selftest.py b/mudpy/tests/selftest.py index 6755195..512ead1 100644 --- a/mudpy/tests/selftest.py +++ b/mudpy/tests/selftest.py @@ -165,13 +165,14 @@ test_admin_setup = ( test_preferences = ( (0, "> ", "preferences"), - (0, r"prompt \x1b\[32m.*> ", "preferences prompt $(foo)"), + (0, r"\[32mprompt\x1b\[0m - .*> ", "preferences prompt $(foo)"), (0, r"\$\(foo\) ", "preferences prompt"), (0, r"\$\(foo\).*\$\(foo\) ", "preferences prompt $(time)>"), - (0, "[0-9]> ", "preferences prompt >"), + (0, "[0-9]> ", "preferences loglevel 0"), + (0, "does not exist.*> ", "preferences prompt >"), (2, "> ", "preferences loglevel 0"), (2, "> ", "preferences"), - (2, r"loglevel \x1b\[32m0\x1b\[0m.*> ", "preferences loglevel zero"), + (2, r"\[31mloglevel\x1b\[0m - 0.*> ", "preferences loglevel zero"), (2, r'''cannot be set to type ""\..*> ''', ""), )