Exemplo n.º 1
0
    def func(self):
        """
        This command handles all the in-editor :-style commands. Since each command
        is small and very limited, this makes for a more efficient presentation.
        """
        caller = self.caller
        editor = self.editor
        linebuffer = self.linebuffer
        lstart, lend = self.lstart, self.lend
        cmd = self.cmdstring
        echo_mode = self.editor.echo_mode
        string = ""

        if cmd == ":":
            # Echo buffer
            if self.linerange:
                buf = linebuffer[lstart:lend]
                string = editor.display_buffer(buf=buf, offset=lstart)
            else:
                string = editor.display_buffer()
        elif cmd == "::":
            # Echo buffer without the line numbers and syntax parsing
            if self.linerange:
                buf = linebuffer[lstart:lend]
                string = editor.display_buffer(buf=buf,
                                               offset=lstart,
                                               linenums=False)
            else:
                string = editor.display_buffer(linenums=False)
            self.caller.msg(string, data={"raw": True})
            return
        elif cmd == ":::":
            # Insert single colon alone on a line
            editor.update_buffer(editor.buffer + "\n:")
            if echo_mode:
                string = "Single ':' added to buffer."
        elif cmd == ":h":
            # help entry
            string = editor.display_help()
        elif cmd == ":w":
            # save without quitting
            string = editor.save_buffer(quitting=False)
        elif cmd == ":wq":
            # save and quit
            string = editor.save_buffer(quitting=True)
            string += " " + editor.quit()
        elif cmd == ":q":
            # quit. If not saved, will ask
            if self.editor.unsaved:
                prompt_yesno(
                    caller,
                    "Save before quitting?",
                    yescode=
                    "self.caller.ndb._lineeditor.save_buffer(quitting=True)\nself.caller.ndb._lineeditor.quit()",
                    nocode=
                    "self.caller.msg(self.caller.ndb._lineeditor.quit())",
                    default="Y")
            else:
                string = editor.quit()
        elif cmd == ":q!":
            # force quit, not checking saving
            string = editor.quit()
        elif cmd == ":u":
            # undo
            string = editor.update_undo(-1)
        elif cmd == ":uu":
            # redo
            string = editor.update_undo(1)
        elif cmd == ":UU":
            # reset buffer
            editor.update_buffer(editor.pristine_buffer)
            string = "Reverted all changes to the buffer back to original state."
        elif cmd == ":dd":
            # :dd <l> - delete line <l>
            buf = linebuffer[:lstart] + linebuffer[lend:]
            editor.update_buffer(buf)
            string = "Deleted %s." % (self.lstr)
        elif cmd == ":dw":
            # :dw <w> - delete word in entire buffer
            # :dw <l> <w> delete word only on line(s) <l>
            if not self.arg1:
                string = "You must give a search word to delete."
            else:
                if not self.linerange:
                    lstart = 0
                    lend = self.cline + 1
                    string = "Removed %s for lines %i-%i." % (
                        self.arg1, lstart + 1, lend + 1)
                else:
                    string = "Removed %s for %s." % (self.arg1, self.lstr)
                sarea = "\n".join(linebuffer[lstart:lend])
                sarea = re.sub(r"%s" % self.arg1.strip("\'").strip('\"'), "",
                               sarea, re.MULTILINE)
                buf = linebuffer[:lstart] + sarea.split(
                    "\n") + linebuffer[lend:]
                editor.update_buffer(buf)
        elif cmd == ":DD":
            # clear buffer
            editor.update_buffer("")
            string = "Cleared %i lines from buffer." % self.nlines
        elif cmd == ":y":
            # :y <l> - yank line(s) to copy buffer
            cbuf = linebuffer[lstart:lend]
            editor.copy_buffer = cbuf
            string = "%s, %s yanked." % (self.lstr.capitalize(), cbuf)
        elif cmd == ":x":
            # :x <l> - cut line to copy buffer
            cbuf = linebuffer[lstart:lend]
            editor.copy_buffer = cbuf
            buf = linebuffer[:lstart] + linebuffer[lend:]
            editor.update_buffer(buf)
            string = "%s, %s cut." % (self.lstr.capitalize(), cbuf)
        elif cmd == ":p":
            # :p <l> paste line(s) from copy buffer
            if not editor.copy_buffer:
                string = "Copy buffer is empty."
            else:
                buf = linebuffer[:lstart] + editor.copy_buffer + linebuffer[
                    lstart:]
                editor.update_buffer(buf)
                string = "Copied buffer %s to %s." % (editor.copy_buffer,
                                                      self.lstr)
        elif cmd == ":i":
            # :i <l> <txt> - insert new line
            new_lines = self.args.split('\n')
            if not new_lines:
                string = "You need to enter a new line and where to insert it."
            else:
                buf = linebuffer[:lstart] + new_lines + linebuffer[lstart:]
                editor.update_buffer(buf)
                string = "Inserted %i new line(s) at %s." % (len(new_lines),
                                                             self.lstr)
        elif cmd == ":r":
            # :r <l> <txt> - replace lines
            new_lines = self.args.split('\n')
            if not new_lines:
                string = "You need to enter a replacement string."
            else:
                buf = linebuffer[:lstart] + new_lines + linebuffer[lend:]
                editor.update_buffer(buf)
                string = "Replaced %i line(s) at %s." % (len(new_lines),
                                                         self.lstr)
        elif cmd == ":I":
            # :I <l> <txt> - insert text at beginning of line(s) <l>
            if not self.args:
                string = "You need to enter text to insert."
            else:
                buf = linebuffer[:lstart] + [
                    "%s%s" % (self.args, line)
                    for line in linebuffer[lstart:lend]
                ] + linebuffer[lend:]
                editor.update_buffer(buf)
                string = "Inserted text at beginning of %s." % self.lstr
        elif cmd == ":A":
            # :A <l> <txt> - append text after end of line(s)
            if not self.args:
                string = "You need to enter text to append."
            else:
                buf = linebuffer[:lstart] + [
                    "%s%s" % (line, self.args)
                    for line in linebuffer[lstart:lend]
                ] + linebuffer[lend:]
                editor.update_buffer(buf)
                string = "Appended text to end of %s." % self.lstr
        elif cmd == ":s":
            # :s <li> <w> <txt> - search and replace words in entire buffer or on certain lines
            if not self.arg1 or not self.arg2:
                string = "You must give a search word and something to replace it with."
            else:
                if not self.linerange:
                    lstart = 0
                    lend = self.cline + 1
                    string = "Search-replaced %s -> %s for lines %i-%i." % (
                        self.arg1, self.arg2, lstart + 1, lend)
                else:
                    string = "Search-replaced %s -> %s for %s." % (
                        self.arg1, self.arg2, self.lstr)
                sarea = "\n".join(linebuffer[lstart:lend])

                regex = r"%s|^%s(?=\s)|(?<=\s)%s(?=\s)|^%s$|(?<=\s)%s$"
                regarg = self.arg1.strip("\'").strip('\"')
                if " " in regarg:
                    regarg = regarg.replace(" ", " +")
                sarea = re.sub(
                    regex % (regarg, regarg, regarg, regarg, regarg),
                    self.arg2.strip("\'").strip('\"'), sarea, re.MULTILINE)
                buf = linebuffer[:lstart] + sarea.split(
                    "\n") + linebuffer[lend:]
                editor.update_buffer(buf)
        elif cmd == ":f":
            # :f <l> flood-fill buffer or <l> lines of buffer.
            width = 78
            if not self.linerange:
                lstart = 0
                lend = self.cline + 1
                string = "Flood filled lines %i-%i." % (lstart + 1, lend)
            else:
                string = "Flood filled %s." % self.lstr
            fbuf = "\n".join(linebuffer[lstart:lend])
            fbuf = utils.fill(fbuf, width=width)
            buf = linebuffer[:lstart] + fbuf.split("\n") + linebuffer[lend:]
            editor.update_buffer(buf)
        elif cmd == ":fi":
            # :fi <l> indent buffer or lines <l> of buffer.
            indent = " " * 4
            if not self.linerange:
                lstart = 0
                lend = self.cline + 1
                string = "Indented lines %i-%i." % (lstart + 1, lend)
            else:
                string = "Indented %s." % self.lstr
            fbuf = [indent + line for line in linebuffer[lstart:lend]]
            buf = linebuffer[:lstart] + fbuf + linebuffer[lend:]
            editor.update_buffer(buf)
        elif cmd == ":fd":
            # :fi <l> indent buffer or lines <l> of buffer.
            if not self.linerange:
                lstart = 0
                lend = self.cline + 1
                string = "Removed left margin (dedented) lines %i-%i." % (
                    lstart + 1, lend)
            else:
                string = "Removed left margin (dedented) %s." % self.lstr
            fbuf = "\n".join(linebuffer[lstart:lend])
            fbuf = utils.dedent(fbuf)
            buf = linebuffer[:lstart] + fbuf.split("\n") + linebuffer[lend:]
            editor.update_buffer(buf)
        elif cmd == ":echo":
            # set echoing on/off
            editor.echo_mode = not editor.echo_mode
            string = "Echo mode set to %s" % editor.echo_mode
        caller.msg(string)
Exemplo n.º 2
0
    def func(self):
        """
        This command handles all the in-editor :-style commands. Since
        each command is small and very limited, this makes for a more
        efficient presentation.
        """
        caller = self.caller
        editor = self.editor
        linebuffer = self.linebuffer
        lstart, lend = self.lstart, self.lend
        cmd = self.cmdstring
        echo_mode = self.editor.echo_mode
        string = ""

        if cmd == ":":
            # Echo buffer
            if self.linerange:
                buf = linebuffer[lstart:lend]
                string = editor.display_buffer(buf=buf, offset=lstart)
            else:
                string = editor.display_buffer()
        elif cmd == "::":
            # Echo buffer without the line numbers and syntax parsing
            if self.linerange:
                buf = linebuffer[lstart:lend]
                string = editor.display_buffer(buf=buf,
                                               offset=lstart,
                                               linenums=False)
            else:
                string = editor.display_buffer(linenums=False)
            self.caller.msg(string, raw=True)
            return
        elif cmd == ":::":
            # Insert single colon alone on a line
            editor.update_buffer(editor.buffer + "\n:")
            if echo_mode:
                string = "Single ':' added to buffer."
        elif cmd == ":h":
            # help entry
            string = editor.display_help()
        elif cmd == ":w":
            # save without quitting
            string = editor.save_buffer()
        elif cmd == ":wq":
            # save and quit
            string = editor.save_buffer()
            string += " " + editor.quit()
        elif cmd == ":q":
            # quit. If not saved, will ask
            if self.editor.unsaved:
                prompt_yesno(caller, "Save before quitting?",
                             yescode = "self.caller.ndb._lineeditor.save_buffer(quitting=True)\nself.caller.ndb._lineeditor.quit()",
                             nocode = "self.caller.msg(self.caller.ndb._lineeditor.quit())", default="Y")
            else:
                string = editor.quit()
        elif cmd == ":q!":
            # force quit, not checking saving
            string = editor.quit()
        elif cmd == ":u":
            # undo
            string = editor.update_undo(-1)
        elif cmd == ":uu":
            # redo
            string = editor.update_undo(1)
        elif cmd == ":UU":
            # reset buffer
            editor.update_buffer(editor.pristine_buffer)
            string = "Reverted all changes to the buffer back to original state."
        elif cmd == ":dd":
            # :dd <l> - delete line <l>
            buf = linebuffer[:lstart] + linebuffer[lend:]
            editor.update_buffer(buf)
            string = "Deleted %s." % (self.lstr)
        elif cmd == ":dw":
            # :dw <w> - delete word in entire buffer
            # :dw <l> <w> delete word only on line(s) <l>
            if not self.arg1:
                string = "You must give a search word to delete."
            else:
                if not self.linerange:
                    lstart = 0
                    lend = self.cline + 1
                    string = "Removed %s for lines %i-%i." % (self.arg1, lstart + 1, lend + 1)
                else:
                    string = "Removed %s for %s." % (self.arg1, self.lstr)
                sarea = "\n".join(linebuffer[lstart:lend])
                sarea = re.sub(r"%s" % self.arg1.strip("\'").strip('\"'), "", sarea, re.MULTILINE)
                buf = linebuffer[:lstart] + sarea.split("\n") + linebuffer[lend:]
                editor.update_buffer(buf)
        elif cmd == ":DD":
            # clear buffer
            editor.update_buffer("")
            string = "Cleared %i lines from buffer." % self.nlines
        elif cmd == ":y":
            # :y <l> - yank line(s) to copy buffer
            cbuf = linebuffer[lstart:lend]
            editor.copy_buffer = cbuf
            string = "%s, %s yanked." % (self.lstr.capitalize(), cbuf)
        elif cmd == ":x":
            # :x <l> - cut line to copy buffer
            cbuf = linebuffer[lstart:lend]
            editor.copy_buffer = cbuf
            buf = linebuffer[:lstart] + linebuffer[lend:]
            editor.update_buffer(buf)
            string = "%s, %s cut." % (self.lstr.capitalize(), cbuf)
        elif cmd == ":p":
            # :p <l> paste line(s) from copy buffer
            if not editor.copy_buffer:
                string = "Copy buffer is empty."
            else:
                buf = linebuffer[:lstart] + editor.copy_buffer + linebuffer[lstart:]
                editor.update_buffer(buf)
                string = "Copied buffer %s to %s." % (editor.copy_buffer, self.lstr)
        elif cmd == ":i":
            # :i <l> <txt> - insert new line
            new_lines = self.args.split('\n')
            if not new_lines:
                string = "You need to enter a new line and where to insert it."
            else:
                buf = linebuffer[:lstart] + new_lines + linebuffer[lstart:]
                editor.update_buffer(buf)
                string = "Inserted %i new line(s) at %s." % (len(new_lines), self.lstr)
        elif cmd == ":r":
            # :r <l> <txt> - replace lines
            new_lines = self.args.split('\n')
            if not new_lines:
                string = "You need to enter a replacement string."
            else:
                buf = linebuffer[:lstart] + new_lines + linebuffer[lend:]
                editor.update_buffer(buf)
                string = "Replaced %i line(s) at %s." % (len(new_lines), self.lstr)
        elif cmd == ":I":
            # :I <l> <txt> - insert text at beginning of line(s) <l>
            if not self.args:
                string = "You need to enter text to insert."
            else:
                buf = linebuffer[:lstart] + ["%s%s" % (self.args, line) for line in linebuffer[lstart:lend]] + linebuffer[lend:]
                editor.update_buffer(buf)
                string = "Inserted text at beginning of %s." % self.lstr
        elif cmd == ":A":
            # :A <l> <txt> - append text after end of line(s)
            if not self.args:
                string = "You need to enter text to append."
            else:
                buf = linebuffer[:lstart] + ["%s%s" % (line, self.args) for line in linebuffer[lstart:lend]] + linebuffer[lend:]
                editor.update_buffer(buf)
                string = "Appended text to end of %s." % self.lstr
        elif cmd == ":s":
            # :s <li> <w> <txt> - search and replace words
            # in entire buffer or on certain lines
            if not self.arg1 or not self.arg2:
                string = "You must give a search word and something to replace it with."
            else:
                if not self.linerange:
                    lstart = 0
                    lend = self.cline + 1
                    string = "Search-replaced %s -> %s for lines %i-%i." % (self.arg1, self.arg2, lstart + 1 , lend)
                else:
                    string = "Search-replaced %s -> %s for %s." % (self.arg1, self.arg2, self.lstr)
                sarea = "\n".join(linebuffer[lstart:lend])

                regex = r"%s|^%s(?=\s)|(?<=\s)%s(?=\s)|^%s$|(?<=\s)%s$"
                regarg = self.arg1.strip("\'").strip('\"')
                if " " in regarg:
                    regarg = regarg.replace(" ", " +")
                sarea = re.sub(regex % (regarg, regarg, regarg, regarg, regarg), self.arg2.strip("\'").strip('\"'), sarea, re.MULTILINE)
                buf = linebuffer[:lstart] + sarea.split("\n") + linebuffer[lend:]
                editor.update_buffer(buf)
        elif cmd == ":f":
            # :f <l> flood-fill buffer or <l> lines of buffer.
            width = 78
            if not self.linerange:
                lstart = 0
                lend = self.cline + 1
                string = "Flood filled lines %i-%i." % (lstart + 1 , lend)
            else:
                string = "Flood filled %s." % self.lstr
            fbuf = "\n".join(linebuffer[lstart:lend])
            fbuf = utils.fill(fbuf, width=width)
            buf = linebuffer[:lstart] + fbuf.split("\n") + linebuffer[lend:]
            editor.update_buffer(buf)
        elif cmd == ":fi":
            # :fi <l> indent buffer or lines <l> of buffer.
            indent = " " * 4
            if not self.linerange:
                lstart = 0
                lend = self.cline + 1
                string = "Indented lines %i-%i." % (lstart + 1 , lend)
            else:
                string = "Indented %s." % self.lstr
            fbuf = [indent + line for line in linebuffer[lstart:lend]]
            buf = linebuffer[:lstart] + fbuf + linebuffer[lend:]
            editor.update_buffer(buf)
        elif cmd == ":fd":
            # :fi <l> indent buffer or lines <l> of buffer.
            if not self.linerange:
                lstart = 0
                lend = self.cline + 1
                string = "Removed left margin (dedented) lines %i-%i." % (lstart + 1 , lend)
            else:
                string = "Removed left margin (dedented) %s." % self.lstr
            fbuf = "\n".join(linebuffer[lstart:lend])
            fbuf = utils.dedent(fbuf)
            buf = linebuffer[:lstart] + fbuf.split("\n") + linebuffer[lend:]
            editor.update_buffer(buf)
        elif cmd == ":echo":
            # set echoing on/off
            editor.echo_mode = not editor.echo_mode
            string = "Echo mode set to %s" % editor.echo_mode
        caller.msg(string)
Exemplo n.º 3
0
    def format_output(self, obj, avail_cmdset):
        """
        Helper function that creates a nice report about an object.

        returns a string.
        """

        string = "\n{wName/key{n: {c%s{n (%s)" % (obj.name, obj.dbref)
        if hasattr(obj, "aliases") and obj.aliases.all():
            string += "\n{wAliases{n: %s" % (", ".join(utils.make_iter(str(obj.aliases))))
        if hasattr(obj, "sessid") and obj.sessid:
            string += "\n{wsession{n: %s" % obj.sessid
        elif hasattr(obj, "sessions") and obj.sessions:
            string += "\n{wsession(s){n: %s" % (", ".join(str(sess.sessid) for sess in obj.sessions))
        if hasattr(obj, "has_player") and obj.has_player:
            string += "\n{wPlayer{n: {c%s{n" % obj.player.name
            perms = obj.player.permissions
            if obj.player.is_superuser:
                perms = ["<Superuser>"]
            elif not perms:
                perms = ["<None>"]
            string += "\n{wPlayer Perms{n: %s" % (", ".join(perms))
        string += "\n{wTypeclass{n: %s (%s)" % (obj.typeclass.typename, obj.typeclass_path)
        if hasattr(obj, "location"):
            string += "\n{wLocation{n: %s" % obj.location
            if obj.location:
                string += " (#%s)" % obj.location.id
        if hasattr(obj, "destination"):
            string += "\n{wDestination{n: %s" % obj.destination
            if obj.destination:
                string += " (#%s)" % obj.destination.id
        perms = obj.permissions
        if perms:
            perms_string = (", ".join(perms))
        else:
            perms_string = "Default"
        if obj.is_superuser:
            perms_string += " [Superuser]"

        string += "\n{wPermissions{n: %s" % perms_string
        locks = str(obj.locks)
        if locks:
            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"):
            # list the current cmdsets
            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)
            string += "\n{wCurrent Cmdset(s){n:\n %s" % ("\n ".join("%s (prio %s)" % (cmdset.path, cmdset.priority) for cmdset in all_cmdsets))

            # list the commands available to this object
            avail_cmdset = sorted([cmd.key for cmd in avail_cmdset if cmd.access(obj, "cmd")])

            cmdsetstr = utils.fill(", ".join(avail_cmdset), indent=2)
            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():
            string += "\n{wScripts{n:\n %s" % obj.scripts
        # add the attributes
        string += self.format_attributes(obj)
        # add the contents
        exits = []
        pobjs = []
        things = []
        if hasattr(obj, "contents"):
            for content in obj.contents:
                if content.destination:
                    exits.append(content)
                elif content.player:
                    pobjs.append(content)
                else:
                    things.append(content)
            if exits:
                string += "\n{wExits{n: %s" % ", ".join([exit.name for exit in exits])
            if pobjs:
                string += "\n{wCharacters{n: %s" % ", ".join(["{c%s{n" % pobj.name for pobj in pobjs])
            if things:
                string += "\n{wContents{n: %s" % ", ".join([cont.name for cont in obj.contents
                                                           if cont not in exits and cont not in pobjs])
        separator = "-"*78
        #output info
        return '%s\n%s\n%s' % ( separator, string.strip(), separator )