Markup, whitespace and code refactor

This commit is contained in:
BlauFeuer 2017-02-20 02:07:38 -05:00 committed by GitHub
parent f33b562cf7
commit 7f6db31bd6

View file

@ -60,14 +60,13 @@ _RE_GROUP = re.compile(r"\".*?\"|\'.*?\'|\S*")
# use NAWS in the future? # use NAWS in the future?
_DEFAULT_WIDTH = settings.CLIENT_DEFAULT_WIDTH _DEFAULT_WIDTH = settings.CLIENT_DEFAULT_WIDTH
#------------------------------------------------------------ # -------------------------------------------------------------
# #
# texts # texts
# #
#------------------------------------------------------------ # -------------------------------------------------------------
_HELP_TEXT = \ _HELP_TEXT = """
"""
<txt> - any non-command is appended to the end of the buffer. <txt> - any non-command is appended to the end of the buffer.
: <l> - view buffer or only line(s) <l> : <l> - view buffer or only line(s) <l>
:: <l> - raw-view buffer or only line(s) <l> :: <l> - raw-view buffer or only line(s) <l>
@ -105,31 +104,27 @@ _HELP_TEXT = \
:echo - turn echoing of the input on/off (helpful for some clients) :echo - turn echoing of the input on/off (helpful for some clients)
""" """
_HELP_LEGEND = \ _HELP_LEGEND = """
"""
Legend: Legend:
<l> - line number, like '5' or range, like '3:7'. <l> - line number, like '5' or range, like '3:7'.
<w> - a single word, or multiple words with quotes around them. <w> - a single word, or multiple words with quotes around them.
<txt> - longer string, usually not needing quotes. <txt> - longer string, usually not needing quotes.
""" """
_HELP_CODE = \ _HELP_CODE = """
"""
:! - Execute code buffer without saving :! - Execute code buffer without saving
:< - Decrease the level of automatic indentation for the next lines :< - Decrease the level of automatic indentation for the next lines
:> - Increase the level of automatic indentation for the next lines :> - Increase the level of automatic indentation for the next lines
:= - Switch automatic indentation on/off := - Switch automatic indentation on/off
""".lstrip("\n") """.lstrip("\n")
_ERROR_LOADFUNC = \ _ERROR_LOADFUNC = """
"""
{error} {error}
|rBuffer load function error. Could not load initial data.|n |rBuffer load function error. Could not load initial data.|n
""" """
_ERROR_SAVEFUNC = \ _ERROR_SAVEFUNC = """
"""
{error} {error}
|rSave function returned an error. Buffer not saved.|n |rSave function returned an error. Buffer not saved.|n
@ -140,15 +135,13 @@ _ERROR_NO_SAVEFUNC = "|rNo save function defined. Buffer cannot be saved.|n"
_MSG_SAVE_NO_CHANGE = "No changes need saving" _MSG_SAVE_NO_CHANGE = "No changes need saving"
_DEFAULT_NO_QUITFUNC = "Exited editor." _DEFAULT_NO_QUITFUNC = "Exited editor."
_ERROR_QUITFUNC = \ _ERROR_QUITFUNC = """
"""
{error} {error}
|rQuit function gave an error. Skipping.|n |rQuit function gave an error. Skipping.|n
""" """
_ERROR_PERSISTENT_SAVING = \ _ERROR_PERSISTENT_SAVING = """
"""
{error} {error}
|rThe editor state could not be saved for persistent mode. Switching |rThe editor state could not be saved for persistent mode. Switching
@ -167,11 +160,12 @@ _MSG_NO_REDO = "Nothing to redo."
_MSG_UNDO = "Undid one step." _MSG_UNDO = "Undid one step."
_MSG_REDO = "Redid one step." _MSG_REDO = "Redid one step."
#------------------------------------------------------------ # -------------------------------------------------------------
# #
# Handle yes/no quit question # Handle yes/no quit question
# #
#------------------------------------------------------------ # -------------------------------------------------------------
class CmdSaveYesNo(Command): class CmdSaveYesNo(Command):
""" """
@ -185,7 +179,7 @@ class CmdSaveYesNo(Command):
help_cateogory = "LineEditor" help_cateogory = "LineEditor"
def func(self): def func(self):
"Implement the yes/no choice." """Implement the yes/no choice."""
# this is only called from inside the lineeditor # this is only called from inside the lineeditor
# so caller.ndb._lineditor must be set. # so caller.ndb._lineditor must be set.
@ -200,21 +194,21 @@ class CmdSaveYesNo(Command):
class SaveYesNoCmdSet(CmdSet): class SaveYesNoCmdSet(CmdSet):
"Stores the yesno question" """Stores the yesno question"""
key = "quitsave_yesno" key = "quitsave_yesno"
priority = 1 priority = 1
mergetype = "Replace" mergetype = "Replace"
def at_cmdset_creation(self): def at_cmdset_creation(self):
"at cmdset creation" """at cmdset creation"""
self.add(CmdSaveYesNo()) self.add(CmdSaveYesNo())
#------------------------------------------------------------ # -------------------------------------------------------------
# #
# Editor commands # Editor commands
# #
#------------------------------------------------------------ # -------------------------------------------------------------
class CmdEditorBase(Command): class CmdEditorBase(Command):
""" """
@ -239,7 +233,6 @@ class CmdEditorBase(Command):
txt - extra text (string), could be encased in quotes. txt - extra text (string), could be encased in quotes.
""" """
linebuffer = []
editor = self.caller.ndb._eveditor editor = self.caller.ndb._eveditor
if not editor: if not editor:
# this will completely replace the editor # this will completely replace the editor
@ -297,11 +290,7 @@ class CmdEditorBase(Command):
arglist = arglist[1:] arglist = arglist[1:]
# nicer output formatting of the line range. # nicer output formatting of the line range.
lstr = "" lstr = "line %i" % (lstart + 1) if not linerange or lstart + 1 == lend else "lines %i-%i" % (lstart + 1, lend)
if not linerange or lstart + 1 == lend:
lstr = "line %i" % (lstart + 1)
else:
lstr = "lines %i-%i" % (lstart + 1, lend)
# arg1 and arg2 is whatever arguments. Line numbers or -ranges are # arg1 and arg2 is whatever arguments. Line numbers or -ranges are
# never included here. # never included here.
@ -369,25 +358,14 @@ class CmdLineInput(CmdEditorBase):
""" """
caller = self.caller caller = self.caller
editor = caller.ndb._eveditor editor = caller.ndb._eveditor
buf = editor.get_buffer() buf = editor.get_buffer()
# add a line of text to buffer # add a line of text to buffer
line = self.raw_string.strip("\r\n") line = self.raw_string.strip("\r\n")
if not editor._codefunc: if editor._codefunc and editor._indent >= 0:
if not buf:
buf = line
else:
buf = buf + "\n%s" % line
else:
# if automatic indentation is active, add spaces # if automatic indentation is active, add spaces
if editor._indent >= 0:
line = editor.deduce_indent(line, buf) line = editor.deduce_indent(line, buf)
buf = line if not buf else buf + "\n%s" % line
if not buf:
buf = line
else:
buf = buf + "\n%s" % line
self.editor.update_buffer(buf) self.editor.update_buffer(buf)
if self.editor._echo_mode: if self.editor._echo_mode:
# need to do it here or we will be off one line # need to do it here or we will be off one line
@ -398,11 +376,10 @@ class CmdLineInput(CmdEditorBase):
if indent < 0: if indent < 0:
indent = "off" indent = "off"
self.caller.msg("{b%02i|{n ({g%s{n) %s" % ( self.caller.msg("|b%02i|||n (|g%s|n) %s" % (
cline, indent, line)) cline, indent, line))
else: else:
self.caller.msg("{b%02i|{n %s" % (cline, self.args)) self.caller.msg("|b%02i|||n %s" % (cline, self.args))
class CmdEditorGroup(CmdEditorBase): class CmdEditorGroup(CmdEditorBase):
@ -485,7 +462,7 @@ class CmdEditorGroup(CmdEditorBase):
# :dd <l> - delete line <l> # :dd <l> - delete line <l>
buf = linebuffer[:lstart] + linebuffer[lend:] buf = linebuffer[:lstart] + linebuffer[lend:]
editor.update_buffer(buf) editor.update_buffer(buf)
caller.msg("Deleted %s." % (self.lstr)) caller.msg("Deleted %s." % self.lstr)
elif cmd == ":dw": elif cmd == ":dw":
# :dw <w> - delete word in entire buffer # :dw <w> - delete word in entire buffer
# :dw <l> <w> delete word only on line(s) <l> # :dw <l> <w> delete word only on line(s) <l>
@ -556,7 +533,8 @@ class CmdEditorGroup(CmdEditorBase):
if not self.raw_string and not editor._codefunc: if not self.raw_string and not editor._codefunc:
caller.msg("You need to enter text to insert.") caller.msg("You need to enter text to insert.")
else: else:
buf = linebuffer[:lstart] + ["%s%s" % (self.args, line) for line in linebuffer[lstart:lend]] + linebuffer[lend:] buf = linebuffer[:lstart] + ["%s%s" % (self.args, line)
for line in linebuffer[lstart:lend]] + linebuffer[lend:]
editor.update_buffer(buf) editor.update_buffer(buf)
caller.msg("Inserted text at beginning of %s." % self.lstr) caller.msg("Inserted text at beginning of %s." % self.lstr)
elif cmd == ":A": elif cmd == ":A":
@ -564,7 +542,8 @@ class CmdEditorGroup(CmdEditorBase):
if not self.args: if not self.args:
caller.msg("You need to enter text to append.") caller.msg("You need to enter text to append.")
else: else:
buf = linebuffer[:lstart] + ["%s%s" % (line, self.args) for line in linebuffer[lstart:lend]] + linebuffer[lend:] buf = linebuffer[:lstart] + ["%s%s" % (line, self.args)
for line in linebuffer[lstart:lend]] + linebuffer[lend:]
editor.update_buffer(buf) editor.update_buffer(buf)
caller.msg("Appended text to end of %s." % self.lstr) caller.msg("Appended text to end of %s." % self.lstr)
elif cmd == ":s": elif cmd == ":s":
@ -585,7 +564,8 @@ class CmdEditorGroup(CmdEditorBase):
regarg = self.arg1.strip("\'").strip('\"') regarg = self.arg1.strip("\'").strip('\"')
if " " in regarg: if " " in regarg:
regarg = regarg.replace(" ", " +") regarg = regarg.replace(" ", " +")
sarea = re.sub(regex % (regarg, regarg, regarg, regarg, regarg), self.arg2.strip("\'").strip('\"'), sarea, re.MULTILINE) sarea = re.sub(regex % (regarg, regarg, regarg, regarg, regarg), self.arg2.strip("\'").strip('\"'),
sarea, re.MULTILINE)
buf = linebuffer[:lstart] + sarea.split("\n") + linebuffer[lend:] buf = linebuffer[:lstart] + sarea.split("\n") + linebuffer[lend:]
editor.update_buffer(buf) editor.update_buffer(buf)
elif cmd == ":f": elif cmd == ":f":
@ -693,18 +673,20 @@ class CmdEditorGroup(CmdEditorBase):
class EvEditorCmdSet(CmdSet): class EvEditorCmdSet(CmdSet):
"CmdSet for the editor commands" """CmdSet for the editor commands"""
key = "editorcmdset" key = "editorcmdset"
mergetype = "Replace" mergetype = "Replace"
def at_cmdset_creation(self): def at_cmdset_creation(self):
self.add(CmdLineInput()) self.add(CmdLineInput())
self.add(CmdEditorGroup()) self.add(CmdEditorGroup())
#------------------------------------------------------------ # -------------------------------------------------------------
# #
# Main Editor object # Main Editor object
# #
#------------------------------------------------------------ # -------------------------------------------------------------
class EvEditor(object): class EvEditor(object):
""" """
@ -791,11 +773,9 @@ class EvEditor(object):
# save in tuple {kwargs, other options} # save in tuple {kwargs, other options}
try: try:
caller.attributes.add("_eveditor_saved", ( caller.attributes.add("_eveditor_saved", (
{"loadfunc":loadfunc, "savefunc": savefunc, dict(loadfunc=loadfunc, savefunc=savefunc, quitfunc=quitfunc,
"quitfunc": quitfunc, "codefunc": codefunc, codefunc=codefunc, key=key, persistent=persistent),
"key": key, "persistent": persistent}, dict(_pristine_buffer=self._pristine_buffer, _sep=self._sep)))
{"_pristine_buffer": self._pristine_buffer,
"_sep": self._sep}))
caller.attributes.add("_eveditor_buffer_temp", (self._buffer, self._undo_buffer)) caller.attributes.add("_eveditor_buffer_temp", (self._buffer, self._undo_buffer))
caller.attributes.add("_eveditor_unsaved", False) caller.attributes.add("_eveditor_unsaved", False)
caller.attributes.add("_eveditor_indent", 0) caller.attributes.add("_eveditor_indent", 0)
@ -933,7 +913,7 @@ class EvEditor(object):
`offset` should define the actual starting line number, to `offset` should define the actual starting line number, to
get the linenum display right. get the linenum display right.
linenums (bool, optional): Show line numbers in buffer. linenums (bool, optional): Show line numbers in buffer.
raw (bool, optional): Tell protocol to not parse options: raw (bool, optional): Tell protocol to not parse
formatting information. formatting information.
""" """
@ -949,10 +929,10 @@ class EvEditor(object):
sep = self._sep sep = self._sep
header = "|n" + sep * 10 + "Line Editor [%s]" % self._key + sep * (_DEFAULT_WIDTH-20-len(self._key)) header = "|n" + sep * 10 + "Line Editor [%s]" % self._key + sep * (_DEFAULT_WIDTH-20-len(self._key))
footer = "|n" + sep * 10 + "[l:%02i w:%03i c:%04i]" % (nlines, nwords, nchars) \ footer = "|n" + sep * 10 +\
+ sep * 12 + "(:h for help)" + sep * 28 "[l:%02i w:%03i c:%04i]" % (nlines, nwords, nchars) + sep * 12 + "(:h for help)" + sep * 28
if linenums: if linenums:
main = "\n".join("{b%02i|{n %s" % (iline + 1 + offset, line) for iline, line in enumerate(lines)) main = "\n".join("|b%02i|||n %s" % (iline + 1 + offset, line) for iline, line in enumerate(lines))
else: else:
main = "\n".join(lines) main = "\n".join(lines)
string = "%s\n%s\n%s" % (header, main, footer) string = "%s\n%s\n%s" % (header, main, footer)
@ -1018,6 +998,7 @@ class EvEditor(object):
self._indent += 1 self._indent += 1
if self._persistent: if self._persistent:
self._caller.attributes.add("_eveditor_indent", self._indent) self._caller.attributes.add("_eveditor_indent", self._indent)
def swap_autoindent(self): def swap_autoindent(self):
"""Swap automatic indentation on or off.""" """Swap automatic indentation on or off."""
if self._codefunc: if self._codefunc: