Fixed #346, examine cutting off parts of output due to ANSI coloration.

This commit is contained in:
Kelketek 2013-02-06 15:07:44 -06:00
parent d5129b5065
commit 00f46d36cd
2 changed files with 56 additions and 84 deletions

View file

@ -7,6 +7,7 @@ from django.conf import settings
from src.objects.models import ObjectDB, ObjAttribute from src.objects.models import ObjectDB, ObjAttribute
from src.players.models import PlayerAttribute from src.players.models import PlayerAttribute
from src.utils import create, utils, debug from src.utils import create, utils, debug
from src.utils.ansi import raw
from src.commands.default.muxcommand import MuxCommand from src.commands.default.muxcommand import MuxCommand
from src.commands.cmdhandler import get_and_merge_cmdsets from src.commands.cmdhandler import get_and_merge_cmdsets
@ -1544,7 +1545,6 @@ class CmdExamine(ObjManipCommand):
Switch: Switch:
player - examine a Player (same as adding *) player - examine a Player (same as adding *)
raw - don't parse escape codes for data.
The examine command shows detailed game info about an The examine command shows detailed game info about an
object and optionally a specific attribute on it. object and optionally a specific attribute on it.
@ -1561,19 +1561,23 @@ class CmdExamine(ObjManipCommand):
player_mode = False player_mode = False
def format_attributes(self, obj, attrname=None, crop=True, raw=False): def list_attribute(self, crop, attr, value):
"""
Formats a single attribute line.
"""
if crop and isinstance(value, basestring):
value = utils.crop(value)
value = repr(value)
string = "\n %s = %s" % (attr, value)
string = raw(string)
return string
def format_attributes(self, obj, attrname=None, crop=True):
""" """
Helper function that returns info about attributes and/or Helper function that returns info about attributes and/or
non-persistent data stored on object non-persistent data stored on object
""" """
headers = {"persistent":"\n{wPersistent attributes{n:",
"nonpersistent":"\n{wNon-persistent attributes{n:"}
headers_noansi = {"persistent":"\nPersistent attributes:",
"nonpersistent":"\nNon-persistent attributes:"}
if raw:
headers = headers_noansi
if attrname: if attrname:
db_attr = [(attrname, obj.attr(attrname))] db_attr = [(attrname, obj.attr(attrname))]
try: try:
@ -1591,104 +1595,71 @@ class CmdExamine(ObjManipCommand):
ndb_attr = None ndb_attr = None
string = "" string = ""
if db_attr and db_attr[0]: if db_attr and db_attr[0]:
#self.caller.msg(db_attr) string += "\n{wPersistent attributes{n:"
string += headers["persistent"]
for attr, value in db_attr: for attr, value in db_attr:
if crop and isinstance(value, basestring): string += self.list_attribute(crop, attr, value)
value = utils.crop(value)
string += "\n %s = %s" % (attr, value)
if ndb_attr and ndb_attr[0]: if ndb_attr and ndb_attr[0]:
string += headers["nonpersistent"] string += "\n{wNon-Persistent attributes{n:"
for attr, value in ndb_attr: for attr, value in ndb_attr:
if crop and isinstance(value, basestring): string += self.list_attribute(crop, attr, value)
value = utils.crop(value)
string += "\n %s = %s" % (attr, value)
return string return string
def format_output(self, obj, avail_cmdset, raw=False): def format_output(self, obj, avail_cmdset):
""" """
Helper function that creates a nice report about an object. Helper function that creates a nice report about an object.
returns a string. returns a string.
""" """
headers = {"name":"\n{wName/key{n: {c%s{n (%s)", string = "\n{wName/key{n: {c%s{n (%s)" % (obj.name, obj.dbref)
"aliases":"\n{wAliases{n: %s",
"player":"\n{wPlayer{n: {c%s{n",
"playerperms":"\n{wPlayer Perms{n: %s",
"typeclass":"\n{wTypeclass{n: %s (%s)",
"location":"\n{wLocation{n: %s (#%s)",
"destination":"\n{wDestination{n: %s (#%s)",
"perms":"\n{wPermissions{n: %s",
"locks":"\n{wLocks{n:",
"cmdset":"\n{wCurrent Cmdset(s){n:\n %s",
"cmdset_avail":"\n{wCommands available to %s (all cmdsets + exits and external cmds){n:\n %s",
"scripts":"\n{wScripts{n:\n %s",
"exits":"\n{wExits{n: ",
"characters":"\n{wCharacters{n: ",
"contents":"\n{wContents{n: "}
headers_noansi = {"name":"\nName/key: %s (%s)",
"aliases":"\nAliases: %s",
"player":"\nPlayer: %s",
"playerperms":"\nPlayer Perms: %s",
"typeclass":"\nTypeclass: %s%s",
"location":"\nLocation: %s (#%s)",
"destination":"\nDestination: %s (#%s)",
"perms":"\nPermissions: %s",
"locks":"\nLocks:",
"cmdset":"\nCurrent Cmdset(s):\n %s",
"cmdset_avail":"\nCommands available to %s (all cmdsets + exits and external cmds):\n %s",
"scripts":"\nScripts:\n %s",
"exits":"\nExits: ",
"characters":"\nCharacters: ",
"contents":"\nContents: "}
if raw:
headers = headers_noansi
if hasattr(obj, "has_player") and obj.has_player:
string = headers["name"] % (obj.name, obj.dbref)
else:
string = headers["name"] % (obj.name, obj.dbref)
if hasattr(obj, "aliases") and obj.aliases: if hasattr(obj, "aliases") and obj.aliases:
string += headers["aliases"] % (", ".join(utils.make_iter(obj.aliases))) string += "\n{wAliases{n: %s" % (", ".join(utils.make_iter(obj.aliases)))
if hasattr(obj, "has_player") and obj.has_player: if hasattr(obj, "has_player") and obj.has_player:
string += headers["player"] % obj.player.name string += "\n{wPlayer{n: {c%s{n" % obj.player.name
perms = obj.player.permissions perms = obj.player.permissions
if obj.player.is_superuser: if obj.player.is_superuser:
perms = ["<Superuser>"] perms = ["<Superuser>"]
elif not perms: elif not perms:
perms = ["<None>"] perms = ["<None>"]
string += headers["playerperms"] % (", ".join(perms)) string += "\n{wPlayer Perms{n: %s" % (", ".join(perms))
string += headers["typeclass"] % (obj.typeclass.typename, obj.typeclass_path) string += "\n{wTypeclass{n: %s (%s)" % (obj.typeclass.typename, obj.typeclass_path)
if hasattr(obj, "location"):
if hasattr(obj, "location") and obj.location: string += "\n{wLocation{n: %s" % obj.location
string += headers["location"] % (obj.location, obj.location.id) if obj.location:
if hasattr(obj, "destination") and obj.destination: string += " (#%s)" % obj.location.id
string += headers["destination"] % (obj.destination, obj.destination.id) if hasattr(obj, "destination"):
string += "\n{wDestination{n: %s" % obj.destination
if obj.destination:
string += " (#%s)" % obj.destination.id
perms = obj.permissions perms = obj.permissions
if perms: if perms:
string += headers["perms"] % (", ".join(perms)) perms_string = (", ".join(perms))
else:
perms_string = "Default"
string += "\n{wPermissions{n: %s" % perms_string
locks = str(obj.locks) locks = str(obj.locks)
if locks: if locks:
string += headers["locks"] + utils.fill("; ".join([lock for lock in locks.split(';')]), indent=6) locks_string = utils.fill("; ".join([lock for lock in locks.split(';')]), indent=6)
else:
locks_string = " Default"
string += "\n{wLocks{n:%s" % locks_string
if not (len(obj.cmdset.all()) == 1 and obj.cmdset.current.key == "Empty"): if not (len(obj.cmdset.all()) == 1 and obj.cmdset.current.key == "Empty"):
# list the current cmdsets # list the current cmdsets
all_cmdsets = obj.cmdset.all() + (hasattr(obj, "player") and obj.player and obj.player.cmdset.all() or []) all_cmdsets = obj.cmdset.all() + (hasattr(obj, "player") and obj.player and obj.player.cmdset.all() or [])
all_cmdsets.sort(key=lambda x:x.priority, reverse=True) all_cmdsets.sort(key=lambda x:x.priority, reverse=True)
string += headers["cmdset"] % ("\n ".join("%s (prio %s)" % (cmdset.path, cmdset.priority) for cmdset in all_cmdsets)) string += "\n{wCurrent Cmdset(s){n:\n %s" % ("\n ".join("%s (prio %s)" % (cmdset.path, cmdset.priority) for cmdset in all_cmdsets))
#cmdsetstr = "\n".join([utils.fill(cmdset, indent=2) for cmdset in str(obj.cmdset).split("\n")])
# list the commands available to this object # list the commands available to this object
avail_cmdset = sorted([cmd.key for cmd in avail_cmdset if cmd.access(obj, "cmd")]) avail_cmdset = sorted([cmd.key for cmd in avail_cmdset if cmd.access(obj, "cmd")])
cmdsetstr = utils.fill(", ".join(avail_cmdset), indent=2) cmdsetstr = utils.fill(", ".join(avail_cmdset), indent=2)
string += headers["cmdset_avail"] % (obj.key, cmdsetstr) string += "\n{wCommands available to %s (all cmdsets + exits and external cmds){n:\n %s" % (obj.key, cmdsetstr)
if hasattr(obj, "scripts") and hasattr(obj.scripts, "all") and obj.scripts.all(): if hasattr(obj, "scripts") and hasattr(obj.scripts, "all") and obj.scripts.all():
string += headers["scripts"] % obj.scripts string += "\n{wScripts{n:\n %s" % obj.scripts
# add the attributes # add the attributes
string += self.format_attributes(obj, raw=raw) string += self.format_attributes(obj)
# add the contents # add the contents
exits = [] exits = []
pobjs = [] pobjs = []
@ -1702,23 +1673,20 @@ class CmdExamine(ObjManipCommand):
else: else:
things.append(content) things.append(content)
if exits: if exits:
string += headers["exits"] + ", ".join([exit.name for exit in exits]) string += "\n{wExits{n: %s" % ", ".join([exit.name for exit in exits])
if pobjs: if pobjs:
string += headers["characters"] + ", ".join(["{c%s{n" % pobj.name for pobj in pobjs]) string += "\n{wCharacters{n: %s" % ", ".join(["{c%s{n" % pobj.name for pobj in pobjs])
if things: if things:
string += headers["contents"] + ", ".join([cont.name for cont in obj.contents string += "\n{wContents{n: %s" % ", ".join([cont.name for cont in obj.contents
if cont not in exits and cont not in pobjs]) if cont not in exits and cont not in pobjs])
separater = "="*78
#output info #output info
return "-"*78 + '\n' + string.strip() + "\n" + '-'*78 return '%s\n%s\n%s' % ( separater, string.strip(), separater )
def func(self): def func(self):
"Process command" "Process command"
caller = self.caller caller = self.caller
msgdata = None
if "raw" in self.switches:
msgdata = {"raw":True}
def get_cmdset_callback(cmdset): def get_cmdset_callback(cmdset):
""" """
We make use of the cmdhandeler.get_and_merge_cmdsets below. This We make use of the cmdhandeler.get_and_merge_cmdsets below. This
@ -1728,8 +1696,8 @@ class CmdExamine(ObjManipCommand):
that function finishes. Taking the resulting cmdset, we continue that function finishes. Taking the resulting cmdset, we continue
to format and output the result. to format and output the result.
""" """
string = self.format_output(obj, cmdset, raw=msgdata) string = self.format_output(obj, cmdset)
caller.msg(string.strip(), data=msgdata) caller.msg(string.strip())
if not self.args: if not self.args:
# If no arguments are provided, examine the invoker's location. # If no arguments are provided, examine the invoker's location.
@ -1763,7 +1731,7 @@ class CmdExamine(ObjManipCommand):
if obj_attrs: if obj_attrs:
for attrname in obj_attrs: for attrname in obj_attrs:
# we are only interested in specific attributes # we are only interested in specific attributes
caller.msg(self.format_attributes(obj, attrname, crop=False, raw=msgdata)) caller.msg(self.format_attributes(obj, attrname, crop=False))
else: else:
# using callback to print results whenever function returns. # using callback to print results whenever function returns.
get_and_merge_cmdsets(obj).addCallback(get_cmdset_callback) get_and_merge_cmdsets(obj).addCallback(get_cmdset_callback)

View file

@ -245,4 +245,8 @@ def parse_ansi(string, strip_ansi=False, parser=ANSI_PARSER, xterm256=False):
""" """
return parser.parse_ansi(string, strip_ansi=strip_ansi, xterm256=xterm256) return parser.parse_ansi(string, strip_ansi=strip_ansi, xterm256=xterm256)
def raw(string):
"""
Escapes a string into a form which won't be colorized by the ansi parser.
"""
return string.replace('{','{{').replace('%','%%')