Пример #1
0
def tabular_table(word_list=None,
                  field_width=26,
                  line_length=78,
                  output_separator=" ",
                  truncate_elements=True):
    """
    This function returns a tabulated string composed of a basic list of words.
    """
    if not word_list:
        word_list = list()
    elements = [ANSIString(entry) for entry in word_list]
    if truncate_elements:
        elements = [entry[:field_width] for entry in elements]
    elements = [entry.ljust(field_width) for entry in elements]
    separator_length = len(output_separator)
    per_line = line_length / (field_width + separator_length)
    result_string = ANSIString("")
    count = 0
    total = len(elements)
    for num, element in enumerate(elements):
        count += 1
        if count == 1:
            result_string += element
        elif count == per_line:
            result_string += output_separator
            result_string += element
            if not num + 1 == total:
                result_string += '\n'
            count = 0
        elif count > 1:
            result_string += output_separator
            result_string += element
    return result_string
Пример #2
0
def clean_and_ansi(input_text, thing_name="Name"):
    if not input_text:
        raise ValueError(f"{thing_name} must not be empty!")
    input_text = input_text.strip()
    if '|' in input_text and not input_text.endswith('|n'):
        input_text += "|n"
    colored_text = ANSIString(input_text)
    clean_text = str(colored_text.clean())
    if '|' in clean_text:
        raise ValueError(f"Malformed ANSI in {thing_name}.")
    return clean_text, colored_text
Пример #3
0
def sanitize_string(text=None,
                    length=None,
                    strip_ansi=False,
                    strip_mxp=True,
                    strip_newlines=True,
                    strip_indents=True):
    if not text:
        return ''
    text = text.strip()
    if strip_mxp:
        text = ANSI_PARSER.strip_mxp(text)
    if strip_ansi:
        text = ANSIString(text).clean()
    if strip_newlines:
        for bad_char in ['\n', '%r', '%R', '|/']:
            text = text.replace(bad_char, '')
    if strip_indents:
        for bad_char in ['\t', '%t', '%T', '|-']:
            text = text.replace(bad_char, '')
    if length:
        text = text[:length]
    return text
Пример #4
0
 def demarkup(self):
     return_string = None
     if self.special_format == 0:
         return_string = f'{self.display_name} {self.action_string}, "{self.speech_string}|n"'
     elif self.special_format == 1:
         return_string = f'{self.display_name} {self.speech_string}'
     elif self.special_format == 2:
         return_string = f'{self.display_name}{self.speech_string}'
     elif self.special_format == 3:
         return_string = self.speech_string
     if self.title:
         return_string = f'{self.title} {return_string}'
     return ANSIString(return_string)
Пример #5
0
    def render(self, viewer=None):
        if not viewer:
            return ANSIString(self.demarkup())
        return_string = None
        if self.special_format == 0:
            return_string = f'{self.markup_name} {self.action_string}, "{self.markup_string}|n"'
        elif self.special_format == 1:
            return_string = f'{self.markup_name} {self.markup_string}'
        elif self.special_format == 2:
            return_string = f'{self.markup_name}{self.markup_string}'
        elif self.special_format == 3:
            return_string = self.markup_string
        if self.title:
            return_string = f'{self.title} {return_string}'
        if self.mode == 'page' and len(self.targets) > 1:
            pref = f'(To {", ".join(self.targets)})'
            return_string = f'{pref} {return_string}'

        return self.colorize(return_string, viewer)
Пример #6
0
    def styled_table(self, *args, **kwargs):
        """
        Create an EvTable styled by on user preferences.

        Args:
            *args (str): Column headers. If not colored explicitly, these will get colors
                from user options.
        Kwargs:
            any (str, int or dict): EvTable options, including, optionally a `table` dict
                detailing the contents of the table.
        Returns:
            table (EvTable): An initialized evtable entity, either complete (if using `table` kwarg)
                or incomplete and ready for use with `.add_row` or `.add_collumn`.

        """
        border_color = self.connection.options.get("border_color")
        column_color = self.connection.options.get("column_names_color")

        colornames = ["|%s%s|n" % (column_color, col) for col in args]

        h_line_char = kwargs.pop("header_line_char", "~")
        header_line_char = ANSIString(f"|{border_color}{h_line_char}|n")
        c_char = kwargs.pop("corner_char", "+")
        corner_char = ANSIString(f"|{border_color}{c_char}|n")

        b_left_char = kwargs.pop("border_left_char", "||")
        border_left_char = ANSIString(f"|{border_color}{b_left_char}|n")

        b_right_char = kwargs.pop("border_right_char", "||")
        border_right_char = ANSIString(f"|{border_color}{b_right_char}|n")

        b_bottom_char = kwargs.pop("border_bottom_char", "-")
        border_bottom_char = ANSIString(f"|{border_color}{b_bottom_char}|n")

        b_top_char = kwargs.pop("border_top_char", "-")
        border_top_char = ANSIString(f"|{border_color}{b_top_char}|n")

        table = EvTable(
            *colornames,
            header_line_char=header_line_char,
            corner_char=corner_char,
            border_left_char=border_left_char,
            border_right_char=border_right_char,
            border_top_char=border_top_char,
            **kwargs,
        )
        return table
Пример #7
0
def mxp(text="", command="", hints=""):
    if text:
        return ANSIString("|lc%s|lt%s|le" % (command, text))
    else:
        return ANSIString("|lc%s|lt%s|le" % (command, command))
Пример #8
0
            else:
                if (found := self.fallback_cache.get(cache_id, None)):
                    return found

        colors = dict()
        colors["border"] = self.connection.options.get("border_color")
        colors["headertext"] = self.connection.options.get(f"{mode}_text_color")
        colors["headerstar"] = self.connection.options.get(f"{mode}_star_color")

        width = self.width
        if edge_character:
            width -= 2

        if header_text:
            if color_header:
                header_text = ANSIString(header_text).clean()
                header_text = ANSIString("|n|%s%s|n" % (colors["headertext"], header_text))
            if mode == "header":
                begin_center = ANSIString(
                    "|n|%s<|%s* |n" % (colors["border"], colors["headerstar"])
                )
                end_center = ANSIString("|n |%s*|%s>|n" % (colors["headerstar"], colors["border"]))
                center_string = ANSIString(begin_center + header_text + end_center)
            else:
                center_string = ANSIString("|n |%s%s |n" % (colors["headertext"], header_text))
        else:
            center_string = ""

        fill_character = self.connection.options.get("%s_fill" % mode)

        remain_fill = width - len(center_string)
Пример #9
0
 def sanitize_name(self, name):
     name = ANSIString(name).clean()
     name = name.strip()
     return name