Add confirm to set/edit for non-string Attributes. Resolves #2222
This commit is contained in:
parent
e4013dbdb8
commit
49886dbff3
2 changed files with 29 additions and 13 deletions
|
|
@ -1787,28 +1787,42 @@ class CmdSetAttribute(ObjManipCommand):
|
||||||
"dicts.|n"
|
"dicts.|n"
|
||||||
)
|
)
|
||||||
|
|
||||||
def edit_handler(self, obj, attr):
|
@interactive
|
||||||
|
def edit_handler(self, obj, attr, caller):
|
||||||
"""Activate the line editor"""
|
"""Activate the line editor"""
|
||||||
|
|
||||||
def load(caller):
|
def load(caller):
|
||||||
"""Called for the editor to load the buffer"""
|
"""Called for the editor to load the buffer"""
|
||||||
old_value = obj.attributes.get(attr)
|
|
||||||
if old_value is not None and not isinstance(old_value, str):
|
try:
|
||||||
typ = type(old_value).__name__
|
old_value = obj.attributes.get(attr, raise_exception=True)
|
||||||
self.caller.msg(
|
except AttributeError:
|
||||||
"|RWARNING! Saving this buffer will overwrite the "
|
# we set empty buffer on nonexisting Attribute because otherwise
|
||||||
"current attribute (of type %s) with a string!|n" % typ
|
# we'd always have the string "None" in the buffer to start with
|
||||||
)
|
old_value = ''
|
||||||
return str(old_value)
|
return str(old_value) # we already confirmed we are ok with this
|
||||||
return old_value
|
|
||||||
|
|
||||||
def save(caller, buf):
|
def save(caller, buf):
|
||||||
"""Called when editor saves its buffer."""
|
"""Called when editor saves its buffer."""
|
||||||
obj.attributes.add(attr, buf)
|
obj.attributes.add(attr, buf)
|
||||||
caller.msg("Saved Attribute %s." % attr)
|
caller.msg("Saved Attribute %s." % attr)
|
||||||
|
|
||||||
|
# check non-strings before activating editor
|
||||||
|
try:
|
||||||
|
old_value = obj.attributes.get(attr, raise_exception=True)
|
||||||
|
if not isinstance(old_value, str):
|
||||||
|
answer = yield(
|
||||||
|
f"|rWarning: Attribute |w{attr}|r is of type |w{type(old_value).__name__}|r. "
|
||||||
|
"\nTo continue editing, it must be converted to (and saved as) a string. "
|
||||||
|
"Continue? [Y]/N?")
|
||||||
|
if answer.lower() in ('n', 'no'):
|
||||||
|
self.caller.msg("Aborted edit.")
|
||||||
|
return
|
||||||
|
except AttributeError:
|
||||||
|
pass
|
||||||
|
|
||||||
# start the editor
|
# start the editor
|
||||||
EvEditor(self.caller, load, save, key="%s/%s" % (obj, attr))
|
EvEditor(self.caller, load, save, key=f"{obj}/{attr}")
|
||||||
|
|
||||||
def search_for_obj(self, objname):
|
def search_for_obj(self, objname):
|
||||||
"""
|
"""
|
||||||
|
|
@ -1878,7 +1892,7 @@ class CmdSetAttribute(ObjManipCommand):
|
||||||
"edit the current room description, use `set/edit here/desc` (or "
|
"edit the current room description, use `set/edit here/desc` (or "
|
||||||
"use the `desc` command).")
|
"use the `desc` command).")
|
||||||
return
|
return
|
||||||
self.edit_handler(obj, attrs[0])
|
self.edit_handler(obj, attrs[0], caller)
|
||||||
return
|
return
|
||||||
if not value:
|
if not value:
|
||||||
if self.rhs is None:
|
if self.rhs is None:
|
||||||
|
|
|
||||||
|
|
@ -909,8 +909,10 @@ class EvEditor:
|
||||||
try:
|
try:
|
||||||
self._buffer = self._loadfunc(self._caller)
|
self._buffer = self._loadfunc(self._caller)
|
||||||
if not isinstance(self._buffer, str):
|
if not isinstance(self._buffer, str):
|
||||||
|
self._caller.msg(f"|rBuffer is of type |w{type(self._buffer)})|r. "
|
||||||
|
"Continuing, it is converted to a string "
|
||||||
|
"(and will be saved as such)!|n")
|
||||||
self._buffer = to_str(self._buffer)
|
self._buffer = to_str(self._buffer)
|
||||||
self._caller.msg(_("|rNote: input buffer was converted to a string.|n"))
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
from evennia.utils import logger
|
from evennia.utils import logger
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue