Esempio n. 1
0
    def init_str(self, text):
        # """The input is a string"""

        # we must break very long lines into multiple ones. Note that this
        # will also remove spurious whitespace.
        justify_kwargs = self._justify_kwargs or {}
        width = 23
        justify_kwargs["width"] = width
        justify_kwargs["align"] = "l"
        justify_kwargs["indent"] = 0

        lines = []
        for line in text.split("\n"):
            if len(line) > width:
                lines.extend(justify(line, **justify_kwargs).split("\n"))
            else:
                lines.append(line)

        page_height = 15

        self._data = [
            "\n".join(lines[i:i + page_height])
            for i in range(0, len(lines), page_height)
        ]
        self._npages = len(self._data)
Esempio n. 2
0
def msg_cinematic(text, borders=True):
    """
    Display a text as a 'cinematic' - centered and
    surrounded by borders.

    Args:
        text (str): Text to format.
        borders (bool, optional): Put borders above and below text.
    Returns:
        text (str): Pretty-formatted text.

    """
    text = text.strip()
    text = justify(text, align="c", indent=1)
    if borders:
        text = add_msg_borders(text)
    return text
Esempio n. 3
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 = caller.ndb._eveditor

        linebuffer = self.linebuffer
        lstart, lend = self.lstart, self.lend
        cmd = self.cmdstring
        echo_mode = self.editor._echo_mode

        if cmd == ":":
            # Echo buffer
            if self.linerange:
                buf = linebuffer[lstart:lend]
                editor.display_buffer(buf=buf, offset=lstart)
            else:
                editor.display_buffer()
        elif cmd == "::":
            # Echo buffer without the line numbers and syntax parsing
            if self.linerange:
                buf = linebuffer[lstart:lend]
                editor.display_buffer(buf=buf,
                                      offset=lstart,
                                      linenums=False,
                                      options={"raw": True})
            else:
                editor.display_buffer(linenums=False, options={"raw": True})
        elif cmd == ":::":
            # Insert single colon alone on a line
            editor.update_buffer([":"] if lstart == 0 else linebuffer + [":"])
            if echo_mode:
                caller.msg("Single ':' added to buffer.")
        elif cmd == ":h":
            # help entry
            editor.display_help()
        elif cmd == ":w":
            # save without quitting
            editor.save_buffer()
        elif cmd == ":wq":
            # save and quit
            editor.save_buffer()
            editor.quit()
        elif cmd == ":q":
            # quit. If not saved, will ask
            if self.editor._unsaved:
                caller.cmdset.add(SaveYesNoCmdSet)
                caller.msg(
                    "Save before quitting? |lcyes|lt[Y]|le/|lcno|ltN|le")
            else:
                editor.quit()
        elif cmd == ":q!":
            # force quit, not checking saving
            editor.quit()
        elif cmd == ":u":
            # undo
            editor.update_undo(-1)
        elif cmd == ":uu":
            # redo
            editor.update_undo(1)
        elif cmd == ":UU":
            # reset buffer
            editor.update_buffer(editor._pristine_buffer)
            caller.msg(
                "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)
            caller.msg("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:
                caller.msg("You must give a search word to delete.")
            else:
                if not self.linerange:
                    lstart = 0
                    lend = self.cline + 1
                    caller.msg("Removed %s for lines %i-%i." %
                               (self.arg1, lstart + 1, lend + 1))
                else:
                    caller.msg("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("")

            # Reset indentation level to 0
            if editor._codefunc:
                if editor._indent >= 0:
                    editor._indent = 0
                    if editor._persistent:
                        caller.attributes.add("_eveditor_indent", 0)
            caller.msg("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
            caller.msg("%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)
            caller.msg("%s, %s cut." % (self.lstr.capitalize(), cbuf))
        elif cmd == ":p":
            # :p <l> paste line(s) from copy buffer
            if not editor._copy_buffer:
                caller.msg("Copy buffer is empty.")
            else:
                buf = linebuffer[:lstart] + editor._copy_buffer + linebuffer[
                    lstart:]
                editor.update_buffer(buf)
                caller.msg("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:
                caller.msg(
                    "You need to enter a new line and where to insert it.")
            else:
                buf = linebuffer[:lstart] + new_lines + linebuffer[lstart:]
                editor.update_buffer(buf)
                caller.msg("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:
                caller.msg("You need to enter a replacement string.")
            else:
                buf = linebuffer[:lstart] + new_lines + linebuffer[lend:]
                editor.update_buffer(buf)
                caller.msg("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.raw_string and not editor._codefunc:
                caller.msg("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)
                caller.msg("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:
                caller.msg("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)
                caller.msg("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:
                caller.msg(
                    "You must give a search word and something to replace it with."
                )
            else:
                if not self.linerange:
                    lstart = 0
                    lend = self.cline + 1
                    caller.msg("Search-replaced %s -> %s for lines %i-%i." %
                               (self.arg1, self.arg2, lstart + 1, lend))
                else:
                    caller.msg("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 = _DEFAULT_WIDTH
            if not self.linerange:
                lstart = 0
                lend = self.cline + 1
                caller.msg("Flood filled lines %i-%i." % (lstart + 1, lend))
            else:
                caller.msg("Flood filled %s." % self.lstr)
            fbuf = "\n".join(linebuffer[lstart:lend])
            fbuf = fill(fbuf, width=width)
            buf = linebuffer[:lstart] + fbuf.split("\n") + linebuffer[lend:]
            editor.update_buffer(buf)
        elif cmd == ":j":
            # :f <l> <w>  justify buffer of <l> with <w> as align (one of
            # f(ull), c(enter), r(ight) or l(left). Default is full.
            align_map = {
                "full": "f",
                "f": "f",
                "center": "c",
                "c": "c",
                "right": "r",
                "r": "r",
                "left": "l",
                "l": "l"
            }
            align_name = {
                "f": "Full",
                "c": "Center",
                "l": "Left",
                "r": "Right"
            }
            width = _DEFAULT_WIDTH
            if self.arg1 and self.arg1.lower() not in align_map:
                self.caller.msg(
                    "Valid justifications are [f]ull (default), [c]enter, [r]right or [l]eft"
                )
                return
            align = align_map[self.arg1.lower()] if self.arg1 else 'f'
            if not self.linerange:
                lstart = 0
                lend = self.cline + 1
                self.caller.msg("%s-justified lines %i-%i." %
                                (align_name[align], lstart + 1, lend))
            else:
                self.caller.msg("%s-justified %s." %
                                (align_name[align], self.lstr))
            jbuf = "\n".join(linebuffer[lstart:lend])
            jbuf = justify(jbuf, width=width, align=align)
            buf = linebuffer[:lstart] + jbuf.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
                caller.msg("Indented lines %i-%i." % (lstart + 1, lend))
            else:
                caller.msg("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
                caller.msg("Removed left margin (dedented) lines %i-%i." %
                           (lstart + 1, lend))
            else:
                caller.msg("Removed left margin (dedented) %s." % self.lstr)
            fbuf = "\n".join(linebuffer[lstart:lend])
            fbuf = 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
            caller.msg("Echo mode set to %s" % editor._echo_mode)
        elif cmd == ":!":
            if editor._codefunc:
                editor._codefunc(caller, editor._buffer)
            else:
                caller.msg(
                    "This command is only available in code editor mode.")
        elif cmd == ":<":
            # :<
            if editor._codefunc:
                editor.decrease_indent()
                indent = editor._indent
                if indent >= 0:
                    caller.msg(
                        "Decreased indentation: new indentation is {}.".format(
                            indent))
                else:
                    caller.msg(
                        "|rManual indentation is OFF.|n Use := to turn it on.")
            else:
                caller.msg(
                    "This command is only available in code editor mode.")
        elif cmd == ":>":
            # :>
            if editor._codefunc:
                editor.increase_indent()
                indent = editor._indent
                if indent >= 0:
                    caller.msg(
                        "Increased indentation: new indentation is {}.".format(
                            indent))
                else:
                    caller.msg(
                        "|rManual indentation is OFF.|n Use := to turn it on.")
            else:
                caller.msg(
                    "This command is only available in code editor mode.")
        elif cmd == ":=":
            # :=
            if editor._codefunc:
                editor.swap_autoindent()
                indent = editor._indent
                if indent >= 0:
                    caller.msg("Auto-indentation turned on.")
                else:
                    caller.msg("Auto-indentation turned off.")
            else:
                caller.msg(
                    "This command is only available in code editor mode.")
Esempio n. 4
0
 def nodetext_formatter(self, text):
     return justify(text.strip("\n").rstrip(), align='c', indent=1)
Esempio n. 5
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 = caller.ndb._eveditor

        linebuffer = self.linebuffer
        lstart, lend = self.lstart, self.lend
        cmd = self.cmdstring
        echo_mode = self.editor._echo_mode

        if cmd == ":":
            # Echo buffer
            if self.linerange:
                buf = linebuffer[lstart:lend]
                editor.display_buffer(buf=buf, offset=lstart)
            else:
                editor.display_buffer()
        elif cmd == "::":
            # Echo buffer without the line numbers and syntax parsing
            if self.linerange:
                buf = linebuffer[lstart:lend]
                editor.display_buffer(buf=buf,
                                      offset=lstart,
                                      linenums=False, options={"raw": True})
            else:
                editor.display_buffer(linenums=False, options={"raw": True})
        elif cmd == ":::":
            # Insert single colon alone on a line
            editor.update_buffer([":"] if lstart == 0 else linebuffer + [":"])
            if echo_mode:
                caller.msg("Single ':' added to buffer.")
        elif cmd == ":h":
            # help entry
            editor.display_help()
        elif cmd == ":w":
            # save without quitting
            editor.save_buffer()
        elif cmd == ":wq":
            # save and quit
            editor.save_buffer()
            editor.quit()
        elif cmd == ":q":
            # quit. If not saved, will ask
            if self.editor._unsaved:
                caller.cmdset.add(SaveYesNoCmdSet)
                caller.msg("Save before quitting? |lcyes|lt[Y]|le/|lcno|ltN|le")
            else:
                editor.quit()
        elif cmd == ":q!":
            # force quit, not checking saving
            editor.quit()
        elif cmd == ":u":
            # undo
            editor.update_undo(-1)
        elif cmd == ":uu":
            # redo
            editor.update_undo(1)
        elif cmd == ":UU":
            # reset buffer
            editor.update_buffer(editor._pristine_buffer)
            caller.msg("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)
            caller.msg("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:
                caller.msg("You must give a search word to delete.")
            else:
                if not self.linerange:
                    lstart = 0
                    lend = self.cline + 1
                    caller.msg("Removed %s for lines %i-%i." % (self.arg1, lstart + 1, lend + 1))
                else:
                    caller.msg("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("")

            # Reset indentation level to 0
            if editor._codefunc:
                if editor._indent >= 0:
                    editor._indent = 0
                    if editor._persistent:
                        caller.attributes.add("_eveditor_indent", 0)
            caller.msg("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
            caller.msg("%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)
            caller.msg("%s, %s cut." % (self.lstr.capitalize(), cbuf))
        elif cmd == ":p":
            # :p <l> paste line(s) from copy buffer
            if not editor._copy_buffer:
                caller.msg("Copy buffer is empty.")
            else:
                buf = linebuffer[:lstart] + editor._copy_buffer + linebuffer[lstart:]
                editor.update_buffer(buf)
                caller.msg("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:
                caller.msg("You need to enter a new line and where to insert it.")
            else:
                buf = linebuffer[:lstart] + new_lines + linebuffer[lstart:]
                editor.update_buffer(buf)
                caller.msg("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:
                caller.msg("You need to enter a replacement string.")
            else:
                buf = linebuffer[:lstart] + new_lines + linebuffer[lend:]
                editor.update_buffer(buf)
                caller.msg("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.raw_string and not editor._codefunc:
                caller.msg("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)
                caller.msg("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:
                caller.msg("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)
                caller.msg("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:
                caller.msg("You must give a search word and something to replace it with.")
            else:
                if not self.linerange:
                    lstart = 0
                    lend = self.cline + 1
                    caller.msg("Search-replaced %s -> %s for lines %i-%i." % (self.arg1, self.arg2, lstart + 1, lend))
                else:
                    caller.msg("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 = _DEFAULT_WIDTH
            if not self.linerange:
                lstart = 0
                lend = self.cline + 1
                caller.msg("Flood filled lines %i-%i." % (lstart + 1, lend))
            else:
                caller.msg("Flood filled %s." % self.lstr)
            fbuf = "\n".join(linebuffer[lstart:lend])
            fbuf = fill(fbuf, width=width)
            buf = linebuffer[:lstart] + fbuf.split("\n") + linebuffer[lend:]
            editor.update_buffer(buf)
        elif cmd == ":j":
            # :f <l> <w>  justify buffer of <l> with <w> as align (one of
            # f(ull), c(enter), r(ight) or l(left). Default is full.
            align_map = {"full": "f", "f": "f", "center": "c", "c": "c",
                         "right": "r", "r": "r", "left": "l", "l": "l"}
            align_name = {"f": "Full", "c": "Center", "l": "Left", "r": "Right"}
            width = _DEFAULT_WIDTH
            if self.arg1 and self.arg1.lower() not in align_map:
                self.caller.msg("Valid justifications are [f]ull (default), [c]enter, [r]right or [l]eft")
                return
            align = align_map[self.arg1.lower()] if self.arg1 else 'f'
            if not self.linerange:
                lstart = 0
                lend = self.cline + 1
                self.caller.msg("%s-justified lines %i-%i." % (align_name[align], lstart + 1, lend))
            else:
                self.caller.msg("%s-justified %s." % (align_name[align], self.lstr))
            jbuf = "\n".join(linebuffer[lstart:lend])
            jbuf = justify(jbuf, width=width, align=align)
            buf = linebuffer[:lstart] + jbuf.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
                caller.msg("Indented lines %i-%i." % (lstart + 1, lend))
            else:
                caller.msg("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
                caller.msg("Removed left margin (dedented) lines %i-%i." % (lstart + 1, lend))
            else:
                caller.msg("Removed left margin (dedented) %s." % self.lstr)
            fbuf = "\n".join(linebuffer[lstart:lend])
            fbuf = 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
            caller.msg("Echo mode set to %s" % editor._echo_mode)
        elif cmd == ":!":
            if editor._codefunc:
                editor._codefunc(caller, editor._buffer)
            else:
                caller.msg("This command is only available in code editor mode.")
        elif cmd == ":<":
            # :<
            if editor._codefunc:
                editor.decrease_indent()
                indent = editor._indent
                if indent >= 0:
                    caller.msg("Decreased indentation: new indentation is {}.".format(
                        indent))
                else:
                    caller.msg("|rManual indentation is OFF.|n Use := to turn it on.")
            else:
                caller.msg("This command is only available in code editor mode.")
        elif cmd == ":>":
            # :>
            if editor._codefunc:
                editor.increase_indent()
                indent = editor._indent
                if indent >= 0:
                    caller.msg("Increased indentation: new indentation is {}.".format(
                        indent))
                else:
                    caller.msg("|rManual indentation is OFF.|n Use := to turn it on.")
            else:
                caller.msg("This command is only available in code editor mode.")
        elif cmd == ":=":
            # :=
            if editor._codefunc:
                editor.swap_autoindent()
                indent = editor._indent
                if indent >= 0:
                    caller.msg("Auto-indentation turned on.")
                else:
                    caller.msg("Auto-indentation turned off.")
            else:
                caller.msg("This command is only available in code editor mode.")