Example #1
0
def underline_node_formatter(nodetext, optionstext):
    nodetext_width_max = max(m_len(line) for line in nodetext.split("\n"))
    options_width_max = max(m_len(line) for line in optionstext.split("\n"))
    total_width = max(options_width_max, nodetext_width_max)
    separator1 = "_" * total_width + "\n\n" if nodetext_width_max else ""
    separator2 = "\n" + "_" * total_width + "\n\n" if total_width else ""
    return separator1 + nodetext + separator2 + optionstext
Example #2
0
def underline_node_formatter(nodetext, optionstext):
    nodetext_width_max = max(m_len(line) for line in nodetext.split("\n"))
    options_width_max = max(m_len(line) for line in optionstext.split("\n"))
    total_width = max(options_width_max, nodetext_width_max)
    separator1 = "_" * total_width + "\n\n" if nodetext_width_max else ""
    separator2 = "\n" + "_" * total_width + "\n\n" if total_width else ""
    return separator1 + nodetext + separator2 + optionstext
Example #3
0
def underline_node_formatter(nodetext, optionstext, caller=None):
    """
    Draws a node with underlines '_____' around it.
    """
    nodetext_width_max = max(m_len(line) for line in nodetext.split("\n"))
    options_width_max = max(m_len(line) for line in optionstext.split("\n"))
    total_width = max(options_width_max, nodetext_width_max)
    separator1 = "_" * total_width + "\n\n" if nodetext_width_max else ""
    separator2 = "\n" + "_" * total_width + "\n\n" if total_width else ""
    return separator1 + nodetext + separator2 + optionstext
Example #4
0
def underline_node_formatter(nodetext, optionstext, caller=None):
    """
    Draws a node with underlines '_____' around it.
    """
    nodetext_width_max = max(m_len(line) for line in nodetext.split("\n"))
    options_width_max = max(m_len(line) for line in optionstext.split("\n"))
    total_width = max(options_width_max, nodetext_width_max)
    separator1 = "_" * total_width + "\n\n" if nodetext_width_max else ""
    separator2 = "\n" + "_" * total_width + "\n\n" if total_width else ""
    return separator1 + nodetext + separator2 + optionstext
Example #5
0
def evtable_options_formatter(optionlist, caller=None):
    """
    Formats the option list display.
    """
    if not optionlist:
        return ""

    # column separation distance
    colsep = 4

    nlist = len(optionlist)

    # get the widest option line in the table.
    table_width_max = -1
    table = []
    for key, desc in optionlist:
        if not (key or desc):
            continue
        table_width_max = max(table_width_max,
                              max(m_len(p) for p in key.split("\n")) +
                              max(m_len(p) for p in desc.split("\n")) + colsep)
        raw_key = strip_ansi(key)
        if raw_key != key:
            # already decorations in key definition
            table.append(ANSIString(" |lc%s|lt%s|le: %s" % (raw_key, key, desc)))
        else:
            # add a default white color to key
            table.append(ANSIString(" |lc%s|lt|w%s|n|le: %s" % (raw_key, raw_key, desc)))

    ncols = (_MAX_TEXT_WIDTH // table_width_max) + 1 # number of ncols
    nlastcol = nlist % ncols # number of elements left in last row

    # get the amount of rows needed (start with 4 rows)
    nrows = 4
    while nrows * ncols < nlist:
        nrows += 1
    ncols = nlist // nrows # number of full columns
    nlastcol = nlist % nrows # number of elements in last column

    # get the final column count
    ncols = ncols + 1 if nlastcol > 0 else ncols
    if ncols > 1:
        # only extend if longer than one column
        table.extend([" " for i in range(nrows - nlastcol)])

    # build the actual table grid
    table = [table[icol * nrows : (icol * nrows) + nrows] for icol in range(0, ncols)]

    # adjust the width of each column
    for icol in range(len(table)):
        col_width = max(max(m_len(p) for p in part.split("\n")) for part in table[icol]) + colsep
        table[icol] = [pad(part, width=col_width + colsep, align="l") for part in table[icol]]

    # format the table into columns
    return unicode(EvTable(table=table, border="none"))
Example #6
0
def evtable_options_formatter(optionlist, caller=None):
    """
    Formats the option list display.
    """
    if not optionlist:
        return ""

    # column separation distance
    colsep = 4

    nlist = len(optionlist)

    # get the widest option line in the table.
    table_width_max = -1
    table = []
    for key, desc in optionlist:
        if not (key or desc):
            continue
        table_width_max = max(table_width_max,
                              max(m_len(p) for p in key.split("\n")) +
                              max(m_len(p) for p in desc.split("\n")) + colsep)
        raw_key = strip_ansi(key)
        if raw_key != key:
            # already decorations in key definition
            table.append(ANSIString(" |lc%s|lt%s|le: %s" % (raw_key, key, desc)))
        else:
            # add a default white color to key
            table.append(ANSIString(" |lc%s|lt|w%s|n|le: %s" % (raw_key, raw_key, desc)))

    ncols = (_MAX_TEXT_WIDTH // table_width_max) + 1 # number of ncols
    nlastcol = nlist % ncols # number of elements left in last row

    # get the amount of rows needed (start with 4 rows)
    nrows = 4
    while nrows * ncols < nlist:
        nrows += 1
    ncols = nlist // nrows # number of full columns
    nlastcol = nlist % nrows # number of elements in last column

    # get the final column count
    ncols = ncols + 1 if nlastcol > 0 else ncols
    if ncols > 1:
        # only extend if longer than one column
        table.extend([" " for i in range(nrows - nlastcol)])

    # build the actual table grid
    table = [table[icol * nrows : (icol * nrows) + nrows] for icol in range(0, ncols)]

    # adjust the width of each column
    for icol in range(len(table)):
        col_width = max(max(m_len(p) for p in part.split("\n")) for part in table[icol]) + colsep
        table[icol] = [pad(part, width=col_width + colsep, align="l") for part in table[icol]]

    # format the table into columns
    return unicode(EvTable(table=table, border="none"))
Example #7
0
    def node_formatter(self, nodetext, optionstext):
        """
        Formats the entirety of the node.

        Args:
            nodetext (str): The node text as returned by `self.nodetext_formatter`.
            optionstext (str): The options display as returned by `self.options_formatter`.
            caller (Object, Account or None, optional): The caller of the node.

        Returns:
            node (str): The formatted node to display.

        """
        sep = '-'

        if type(nodetext) == dict:
            if 'text' in nodetext:
                text = nodetext['text']
            else:
                text = ''
            if 'format' in nodetext:
                args = nodetext['format']
            else:
                args = ''
            if 'footer' in nodetext:
                footer = nodetext['footer']
            else:
                footer = ''
        else:
            text = str(nodetext)
            args = False
            footer = False

        if self._session:
            screen_width = self._session.protocol_flags.get(
                "SCREENWIDTH", {0: _MAX_TEXT_WIDTH})[0]
        else:
            screen_width = _MAX_TEXT_WIDTH

        nodetext_width_max = max(m_len(line) for line in text.split("\n"))
        options_width_max = max(
            m_len(line) for line in optionstext.split("\n"))
        total_width = min(screen_width,
                          max(options_width_max, nodetext_width_max))
        separator1 = '_' * total_width + "\n\n" if nodetext_width_max else ""
        separator2 = "\n" + '_' * total_width + "\n\n" if total_width else ""
        if args == 'no_bars':
            result = text + "|n\n" + optionstext
        else:
            result = separator1 + "|n" + text + "|n" + separator2 + "|n" + \
                     optionstext
        if footer:
            result = result + '|/' + footer
        return result
    def node_formatter(self, nodetext, optionstext):
        """
        Formats the entirety of the node.

        Args:
            nodetext (str): The node text as returned by `self.nodetext_formatter`.
            optionstext (str): The options display as returned by `self.options_formatter`.
            caller (Object, Account or None, optional): The caller of the node.

        Returns:
            node (str): The formatted node to display.

        """
        nodetext_width_max = max(m_len(line) for line in nodetext.split("\n"))
        options_width_max = max(m_len(line) for line in optionstext.split("\n"))
        total_width = max(options_width_max, nodetext_width_max)
        separator1 = "_" * total_width + "\n\n" if nodetext_width_max else ""
        separator2 = "\n" + "_" * total_width + "\n\n" if total_width else ""
        return separator1 + "|n" + nodetext + "|n" + separator2 + "|n" + optionstext
Example #9
0
    def node_formatter(self, nodetext, optionstext):
        """
        Formats the entirety of the node.

        Args:
            nodetext (str): The node text as returned by `self.nodetext_formatter`.
            optionstext (str): The options display as returned by `self.options_formatter`.
            caller (Object, Account or None, optional): The caller of the node.

        Returns:
            node (str): The formatted node to display.

        """
        nodetext_width_max = max(m_len(line) for line in nodetext.split("\n"))
        options_width_max = max(m_len(line) for line in optionstext.split("\n"))
        total_width = max(options_width_max, nodetext_width_max)
        separator1 = "_" * total_width + "\n\n" if nodetext_width_max else ""
        separator2 = "\n" + "_" * total_width + "\n\n" if total_width else ""
        return separator1 + "|n" + nodetext + "|n" + separator2 + "|n" + optionstext
Example #10
0
    def options_formatter(self, optionlist, options_format):
        """
        Formats the option block.

        Args:
            optionlist (list): List of (key, description) tuples for every
                option related to this node.
            caller (Object, Account or None, optional): The caller of the node.

        Returns:
            options (str): The formatted option display.

        """
        if not optionlist:
            return ""

        # column separation distance
        colsep = 4

        # parse out options for Quit, Back, Proceed, None, and Finish
        option_list_1 = []
        option_list_2 = []
        for item in optionlist:
            if ('move_keys' in options_format
                    and item[0] in options_format['move_keys']):
                option_list_2.append(item)
            elif ('hide_keys' in options_format
                  and item[0] in options_format['hide_keys']):
                pass
            else:
                option_list_1.append(item)
        nlist = len(option_list_1)

        # get the widest option line in the table.
        table_width_max = -1
        table = []
        for key, desc in option_list_1:
            if key or desc:
                desc_string = ": %s" % desc if desc else ""
                table_width_max = max(
                    table_width_max,
                    max(m_len(p) for p in key.split("\n")) +
                    max(m_len(p) for p in desc_string.split("\n")) + colsep,
                )
                raw_key = strip_ansi(key)
                if raw_key != key:
                    # already decorations in key definition
                    table.append(" |lc%s|lt%s|le%s" %
                                 (raw_key, key, desc_string))
                else:
                    # add a default white color to key
                    table.append(" |lc%s|lt|w%s|n|le%s" %
                                 (raw_key, raw_key, desc_string))
        ncols = _MAX_TEXT_WIDTH // table_width_max  # number of ncols

        if ncols < 0:
            # no visible option at all
            return ""

        ncols = ncols + 1 if ncols == 0 else ncols
        # get the amount of rows needed (start with 4 rows)
        if 'rows' in options_format:
            nrows = options_format['rows']
        else:
            nrows = 4
        while nrows * ncols < nlist:
            nrows += 1
        ncols = nlist // nrows  # number of full columns
        nlastcol = nlist % nrows  # number of elements in last column

        # get the final column count
        ncols = ncols + 1 if nlastcol > 0 else ncols
        if ncols > 1:
            # only extend if longer than one column
            table.extend([" " for i in range(nrows - nlastcol)])

        # build the actual table grid
        table = [
            table[icol * nrows:(icol * nrows) + nrows]
            for icol in range(0, ncols)
        ]

        # adjust the width of each column
        for icol in range(len(table)):
            col_width = (max(
                max(m_len(p) for p in part.split("\n"))
                for part in table[icol]) + colsep)
            table[icol] = [
                pad(part, width=col_width + colsep, align="l")
                for part in table[icol]
            ]
        result = EvTable(table=table, border='none')
        if len(option_list_2) > 0:
            result.add_row(' ')
            for key, desc in option_list_2:
                if key or desc:
                    desc_string = ": %s" % desc if desc else ""
                    table_width_max = max(
                        table_width_max,
                        max(m_len(p) for p in key.split("\n")) +
                        max(m_len(p)
                            for p in desc_string.split("\n")) + colsep,
                    )
                    raw_key = strip_ansi(key)
                    if raw_key != key:
                        # already decorations in key definition
                        result.add_row(" |lc%s|lt%s|le%s" %
                                       (raw_key, key, desc_string))
                    else:
                        # add a default white color to key
                        result.add_row(" |lc%s|lt|w%s|n|le%s" %
                                       (raw_key, raw_key, desc_string))

        # format the table into columns
        return str(result)
Example #11
0
 def test_mxp_string(self):
     self.assertEqual(utils.m_len('|lclook|ltat|le'), 2)
Example #12
0
    def _format_node(self, nodetext, optionlist):
        """
        Format the node text + option section

        Args:
            nodetext (str): The node text
            optionlist (list): List of (key, desc) pairs.

        Returns:
            string (str): The options section, including
                all needed spaces.

        Notes:
            This will adjust the columns of the options, first to use
            a maxiumum of 4 rows (expanding in columns), then gradually
            growing to make use of the screen space.

        """
        #
        # handle the node text
        #

        nodetext = dedent(nodetext).strip()

        nodetext_width_max = max(m_len(line) for line in nodetext.split("\n"))

        if not optionlist:
            # return the node text "naked".
            separator1 = "_" * nodetext_width_max + "\n\n" if nodetext_width_max else ""
            separator2 = "\n" if nodetext_width_max else "" + "_" * nodetext_width_max
            return separator1 + nodetext + separator2

        #
        # handle the options
        #

        # column separation distance
        colsep = 4

        nlist = len(optionlist)

        # get the widest option line in the table.
        table_width_max = -1
        table = []
        for key, desc in optionlist:
            table_width_max = max(table_width_max,
                                  max(m_len(p) for p in key.split("\n")) +
                                  max(m_len(p) for p in desc.split("\n")) + colsep)
            raw_key = strip_ansi(key)
            if raw_key != key:
                # already decorations in key definition
                table.append(ANSIString(" {lc%s{lt%s{le: %s" % (raw_key, key, desc)))
            else:
                # add a default white color to key
                table.append(ANSIString(" {lc%s{lt{w%s{n{le: %s" % (raw_key, raw_key, desc)))

        ncols = (_MAX_TEXT_WIDTH // table_width_max) + 1 # number of ncols
        nlastcol = nlist % ncols # number of elements left in last row

        # get the amount of rows needed (start with 4 rows)
        nrows = 4
        while nrows * ncols < nlist:
            nrows += 1
        ncols = nlist // nrows # number of full columns
        nlastcol = nlist % nrows # number of elements in last column

        # get the final column count
        ncols = ncols + 1 if nlastcol > 0 else ncols
        if ncols > 1:
            # only extend if longer than one column
            table.extend([" " for i in xrange(nrows-nlastcol)])

        # build the actual table grid
        table = [table[icol*nrows:(icol*nrows) + nrows] for icol in xrange(0, ncols)]

        # adjust the width of each column
        total_width = 0
        for icol in xrange(len(table)):
            col_width = max(max(m_len(p) for p in part.split("\n")) for part in table[icol]) + colsep
            table[icol] = [pad(part, width=col_width + colsep, align="l") for part in table[icol]]
            total_width += col_width

        # format the table into columns
        table = EvTable(table=table, border="none")

        # build the page
        total_width = max(total_width, nodetext_width_max)
        separator1 = "_" * total_width + "\n\n" if nodetext_width_max else ""
        separator2 = "\n" + "_" * total_width + "\n\n" if total_width else ""
        return separator1 + nodetext + separator2 + unicode(table)
 def test_dict(self):
     self.assertEqual(utils.m_len({"hello": True, "Goodbye": False}), 2)
Example #14
0
 def test_dict(self):
     self.assertEqual(utils.m_len({'hello': True, 'Goodbye': False}), 2)
Example #15
0
 def test_list(self):
     self.assertEqual(utils.m_len([None, None]), 2)
Example #16
0
 def test_non_mxp_ansi_string(self):
     self.assertEqual(utils.m_len(ANSIString('|gHello|n')), 5)
Example #17
0
 def test_mxp_ansi_string(self):
     self.assertEqual(utils.m_len(ANSIString('|lcl|gook|ltat|le|n')), 2)
Example #18
0
 def test_non_mxp_string(self):
     self.assertEqual(utils.m_len('Test_string'), 11)