"""
Defines the tprint function, that prints tables fomt sqlite.
"""
import numpy as np

def print_as_table(data, limit: int =10):
    """Pretty printing for a sqlite output.# {{{
    Args:
        limit(int): The max number of lines to print.
                    Can be set to float("inf") to print all the lines.
                    Defaults to 10.
    """
    if len(data) == 0:
        print("┏━━━━━━━━━━━┓\n┃empty table┃\n┗━━━━━━━━━━━┛")
        return
    column_width = np.max(np.vectorize(lambda x: len(repr(x)))(data), axis=0)
    # column_width = np.max(np.apply_along_axis(lambda x: len(repr(x)), 0, data))
    header = "┏━" + "━┳━".join(width*"━" for width in column_width) + "━┓"
    print(header)
    for num, line in enumerate(data):
        if num > limit:
            ellipsis = "┃ " + " ┃ ".join("⋮".center(width) for width in column_width) + " ┃"
            print(ellipsis)
            return
        res_line = "┃ "
        res_line += " ┃ ".join(repr(elt).ljust(column_width[col_num])
                            for col_num, elt in enumerate(line))
        res_line += " ┃"
        print(res_line)
    footer = "┗━" + "━┻━".join(width*"━" for width in column_width) + "━┛"
    print(footer)# }}}

def tprint(*args, sep: str ="\n\n", limit: int =10):
    """Pretty print ouputs from sqlite.# {{{
    Args:
        *args: The tables to print.
        sep (str): The separation betweet two tables. Defaults to "\\n\\n"
        limit(int): The max number of lines to print for each table.
                    Can be set to float("inf") to print all the lines.
                    Defaults to 10.
        """
    for table in args:
        print_as_table(table, limit)
        print(sep, end="")# }}}

