Add escaping = as \= in nicks, add colors. Resolves #1551.

This commit is contained in:
Griatch 2018-01-09 18:09:56 +01:00
parent d2b89b7613
commit dec5dbbf3a

View file

@ -1,6 +1,7 @@
""" """
General Character commands usually available to all characters General Character commands usually available to all characters
""" """
import re
from django.conf import settings from django.conf import settings
from evennia.utils import utils, evtable from evennia.utils import utils, evtable
from evennia.typeclasses.attributes import NickTemplateInvalid from evennia.typeclasses.attributes import NickTemplateInvalid
@ -75,13 +76,14 @@ class CmdLook(COMMAND_DEFAULT_CLASS):
class CmdNick(COMMAND_DEFAULT_CLASS): class CmdNick(COMMAND_DEFAULT_CLASS):
""" """
define a personal alias/nick define a personal alias/nick by defining a string to
match and replace it with another on the fly
Usage: Usage:
nick[/switches] <string> [= [replacement_string]] nick[/switches] <string> [= [replacement_string]]
nick[/switches] <template> = <replacement_template> nick[/switches] <template> = <replacement_template>
nick/delete <string> or number nick/delete <string> or number
nick/test <test string> nicks
Switches: Switches:
inputline - replace on the inputline (default) inputline - replace on the inputline (default)
@ -90,22 +92,24 @@ class CmdNick(COMMAND_DEFAULT_CLASS):
delete - remove nick by name or by index given by /list delete - remove nick by name or by index given by /list
clearall - clear all nicks clearall - clear all nicks
list - show all defined aliases (also "nicks" works) list - show all defined aliases (also "nicks" works)
test - test input to see what it matches with
Examples: Examples:
nick hi = say Hello, I'm Sarah! nick hi = say Hello, I'm Sarah!
nick/object tom = the tall man nick/object tom = the tall man
nick build $1 $2 = @create/drop $1;$2 - (template) nick build $1 $2 = @create/drop $1;$2
nick tell $1 $2=@page $1=$2 - (template) nick tell $1 $2=@page $1=$2
nick tm?$1=@page tallman=$1
nick tm\=$1=@page tallman=$1
A 'nick' is a personal string replacement. Use $1, $2, ... to catch arguments. A 'nick' is a personal string replacement. Use $1, $2, ... to catch arguments.
Put the last $-marker without an ending space to catch all remaining text. You Put the last $-marker without an ending space to catch all remaining text. You
can also use unix-glob matching: can also use unix-glob matching for the left-hand side:
* - matches everything * - matches everything
? - matches a single character ? - matches 0 or 1 single characters
[seq] - matches all chars in sequence [abcd] - matches these chars in any order
[!seq] - matches everything not in sequence [!abcd] - matches everything not among these chars
\= - use to have '=' in your matching string
Note that no objects are actually renamed or changed by this command - your nicks Note that no objects are actually renamed or changed by this command - your nicks
are only available to you. If you want to permanently add keywords to an object are only available to you. If you want to permanently add keywords to an object
@ -116,9 +120,27 @@ class CmdNick(COMMAND_DEFAULT_CLASS):
aliases = ["nickname", "nicks", "alias"] aliases = ["nickname", "nicks", "alias"]
locks = "cmd:all()" locks = "cmd:all()"
def parse(self):
"""
Support escaping of = with \=
"""
args = self.args
super(CmdNick, self).parse()
parts = re.split(r"(?<!\\)=", args, 1)
self.rhs = None
if len(parts) < 2:
self.lhs = parts[0].strip()
else:
self.lhs, self.rhs = [part.strip() for part in parts]
self.lhs = self.lhs.replace("\=", "=")
def func(self): def func(self):
"""Create the nickname""" """Create the nickname"""
def _cy(string):
"add color to the special markers"
return re.sub(r"(\$[0-9]+|\*|\?|\[.+?\])", r"|Y\1|n", string)
caller = self.caller caller = self.caller
switches = self.switches switches = self.switches
nicktypes = [switch for switch in switches if switch in ("object", "account", "inputline")] or ["inputline"] nicktypes = [switch for switch in switches if switch in ("object", "account", "inputline")] or ["inputline"]
@ -133,7 +155,7 @@ class CmdNick(COMMAND_DEFAULT_CLASS):
table = evtable.EvTable("#", "Type", "Nick match", "Replacement") table = evtable.EvTable("#", "Type", "Nick match", "Replacement")
for inum, nickobj in enumerate(nicklist): for inum, nickobj in enumerate(nicklist):
_, _, nickvalue, replacement = nickobj.value _, _, nickvalue, replacement = nickobj.value
table.add_row(str(inum + 1), nickobj.db_category, nickvalue, replacement) table.add_row(str(inum + 1), nickobj.db_category, _cy(nickvalue), _cy(replacement))
string = "|wDefined Nicks:|n\n%s" % table string = "|wDefined Nicks:|n\n%s" % table
caller.msg(string) caller.msg(string)
return return
@ -182,11 +204,14 @@ class CmdNick(COMMAND_DEFAULT_CLASS):
caller.nicks.remove(old_nickstring, category=nicktype) caller.nicks.remove(old_nickstring, category=nicktype)
string += "\nNick removed: '|w%s|n' -> |w%s|n." % (old_nickstring, old_replstring) string += "\nNick removed: '|w%s|n' -> |w%s|n." % (old_nickstring, old_replstring)
else: else:
errstring += "\nNick '|w%s|n' was not deleted." % old_nickstring errstring += "\nNick '|w%s|n' was not deleted." % (old_nickstring)
elif replstring: elif replstring:
# creating new nick # creating new nick
errstring = "" errstring = ""
if oldnick: if oldnick:
if replstring == old_replstring:
string += "\nIdentical nick already set."
else:
string += "\nNick '|w%s|n' updated to map to '|w%s|n'." % (old_nickstring, replstring) string += "\nNick '|w%s|n' updated to map to '|w%s|n'." % (old_nickstring, replstring)
else: else:
string += "\nNick '|w%s|n' mapped to '|w%s|n'." % (nickstring, replstring) string += "\nNick '|w%s|n' mapped to '|w%s|n'." % (nickstring, replstring)
@ -200,7 +225,7 @@ class CmdNick(COMMAND_DEFAULT_CLASS):
string += "\nNick '|w%s|n' maps to '|w%s|n'." % (old_nickstring, old_replstring) string += "\nNick '|w%s|n' maps to '|w%s|n'." % (old_nickstring, old_replstring)
errstring = "" errstring = ""
string = errstring if errstring else string string = errstring if errstring else string
caller.msg(string) caller.msg(_cy(string))
class CmdInventory(COMMAND_DEFAULT_CLASS): class CmdInventory(COMMAND_DEFAULT_CLASS):