예제 #1
0
def test_table_title():
    tb = smartcols.Table()
    title = tb.title
    assert isinstance(title, smartcols.Title)
    del tb
    with pytest.raises(ReferenceError):
        assert not title
예제 #2
0
    def create_and_fill_table(self, versions_by_repo):
        table = smartcols.Table()
        table.termforce = 'always'
        table.maxout = True
        column_name = table.new_column("Name")
        column_stream = table.new_column("Stream")
        column_version = table.new_column("Version")
        column_profiles = table.new_column("Profiles")
        column_profiles.wrap = True
        column_info = table.new_column("Info")
        column_info.wrap = True

        if not self.base.conf.verbose:
            column_info.hidden = True

        for repo_id, versions in sorted(versions_by_repo.items(),
                                        key=lambda key: key[0]):
            for i in sorted(versions,
                            key=lambda data:
                            (data.name, data.stream, data.version)):
                line = table.new_line()
                conf = i.repo_module.conf
                defaults_conf = i.repo_module.defaults
                default_str = ""
                enabled_str = ""
                locked_str = ""
                profiles_str = ""
                available_profiles = i.profiles
                installed_profiles = []

                if i.stream == defaults_conf.peek_default_stream():
                    default_str = " [d]"

                if i.stream == conf.stream._get() and conf.enabled._get():
                    if not default_str:
                        enabled_str = " "
                    enabled_str += "[e]"

                if i.stream == conf.stream._get(
                ) and i.version == conf.version._get():
                    if conf.locked._get():
                        locked_str = " [l]"
                    installed_profiles = list(conf.profiles._get())

                for profile in available_profiles[:2]:
                    profiles_str += "{}{}, ".format(
                        profile,
                        " [i]" if profile in installed_profiles else "")

                profiles_str = profiles_str[:-2]
                profiles_str += ", ..." if len(available_profiles) > 2 else ""

                line[column_name] = i.name
                line[column_stream] = i.stream + default_str + enabled_str
                line[column_version] = str(i.version) + locked_str
                line[column_profiles] = profiles_str
                line[column_info] = i.summary()

        return table
예제 #3
0
    def create_and_fill_table(self, versions_by_repo):
        table = smartcols.Table()
        table.termforce = 'always'
        table.maxout = True
        column_name = table.new_column("Name")
        column_stream = table.new_column("Stream")
        column_profiles = table.new_column("Profiles")
        column_profiles.wrap = True
        column_info = table.new_column("Summary")
        column_info.wrap = True

        if not self.base.conf.verbose:
            column_info.hidden = True

        for repo_id, versions in versions_by_repo.items():
            for i in sorted(versions):
                line = table.new_line()
                conf = i.repo_module.conf
                defaults_conf = i.repo_module.defaults
                default_str = ""
                enabled_str = ""
                profiles_str = ""
                available_profiles = i.profiles
                installed_profiles = []

                if i.stream == defaults_conf.peek_default_stream():
                    default_str = " [d]"

                if i.stream == conf.stream._get() and conf.enabled._get():
                    if not default_str:
                        enabled_str = " "
                    enabled_str += "[e]"

                if i.stream == conf.stream._get():
                    installed_profiles = list(conf.profiles._get())

                default_profiles = []
                default_stream = defaults_conf.peek_default_stream()
                profile_defaults = defaults_conf.peek_profile_defaults()
                if default_stream in profile_defaults:
                    default_profiles.extend(
                        profile_defaults[default_stream].dup())
                for profile in available_profiles:
                    profiles_str += "{}{}".format(
                        profile, " [d]" if profile in default_profiles else "")
                    profiles_str += "[i], " if profile in installed_profiles else ", "

                profiles_str = profiles_str[:-2]
                profiles_str += ", ..." if len(available_profiles) > 2 else ""

                line[column_name] = i.name
                line[column_stream] = i.stream + default_str + enabled_str
                line[column_profiles] = profiles_str
                line[column_info] = i.summary()

        return table
예제 #4
0
def main(args=None):
    parser = argparse.ArgumentParser()
    parser.add_argument("-m", "--maxout", action="store_true")
    parser.add_argument("-w", "--width", type=int)
    args = parser.parse_args(args)

    tb = smartcols.Table()
    tb.colors = os.isatty(sys.stdout.fileno())
    if args.maxout:
        tb.maxout = args.maxout
    if args.width:
        tb.termforce = "always"
        tb.termwidth = args.width

    cl_name = tb.new_column("NAME")
    cl_data = tb.new_column("DATA")

    def add_line(name, data):
        ln = tb.new_line()
        ln[cl_name] = name
        ln[cl_data] = data
        return ln

    add_line("foo", "bla bla bla")
    add_line("bar", "alb alb alb")
    title = tb.title

    # right
    title.data = "This is right title"
    title.color = "red"
    title.position = "right"
    print(tb)

    # center
    sm = smartcols.Symbols()
    sm.title_padding = "="
    tb.symbols = sm
    title.data = "This is center title (with padding)"
    title.color = "green"
    title.position = "center"
    print(tb)

    # left
    sm.title_padding = "-"
    title.data = "This is left title (with padding)"
    title.color = "blue"
    title.position = "left"
    print(tb)
예제 #5
0
    def create_simple_table(lines):
        table = smartcols.Table()
        table.noheadings = True
        table.column_separator = " : "

        column_name = table.new_column("Name")
        column_value = table.new_column("Value")
        column_value.wrap = True
        column_value.safechars = "\n"
        column_value.set_wrapfunc(smartcols.wrapnl_chunksize,
                                  smartcols.wrapnl_nextchunk)

        for line_name, value in lines.items():
            if value:
                line = table.new_line()
                line[column_name] = line_name
                line[column_value] = str(value)

        return table
예제 #6
0
from __future__ import print_function, unicode_literals
import locale

import smartcols

if __name__ == "__main__":
    locale.setlocale(locale.LC_ALL, "")

    tb = smartcols.Table()
    tb.colors = True

    cl_name = tb.new_column("NAME")
    cl_name.tree = True
    cl_desc = tb.new_column("DESC")
    cl_foo = tb.new_column("FOO")
    cl_foo.wrap = True
    cl_like = tb.new_column("LIKE")
    cl_like.right = True
    cl_text = tb.new_column("TEXT")
    cl_text.wrap = True

    def add_line(prefix, parent=None):
        def gen_text(sub_prefix, size):
            tmp = "{!s}-{!s}-".format(prefix, sub_prefix)
            return "{}{}x".format(tmp, prefix[0] * (size - 1 - len(tmp)))

        ln = tb.new_line(parent)
        ln[cl_name] = gen_text("N", 15)
        ln[cl_desc] = gen_text("D", 10)
        ln[cl_foo] = gen_text("U", 55)
        ln[cl_like] = "1"
예제 #7
0
def main(args=None):
    parser = argparse.ArgumentParser()
    parser.add_argument("-m",
                        "--maxout",
                        action="store_true",
                        help="fill all terminal width")
    parser.add_argument("-c",
                        "--column",
                        metavar="FILE",
                        action="append",
                        default=[],
                        help="column definition")
    parser.add_argument("-n",
                        "--nlines",
                        metavar="NUM",
                        required=True,
                        type=int,
                        action=PositiveArg,
                        help="number of lines")
    outfmt = parser.add_mutually_exclusive_group()
    outfmt.add_argument("-J",
                        "--json",
                        action="store_true",
                        help="JSON output format")
    outfmt.add_argument("-r",
                        "--raw",
                        action="store_true",
                        help="RAW output format")
    outfmt.add_argument("-E",
                        "--export",
                        action="store_true",
                        help="use key=\"value\" output format")
    parser.add_argument("-C",
                        "--colsep",
                        metavar="STR",
                        help="set columns separator")
    parser.add_argument("-w",
                        "--width",
                        type=int,
                        help="hardcode terminal width")
    parser.add_argument("-p",
                        "--tree-parent-column",
                        metavar="N",
                        type=int,
                        help="parent column")
    parser.add_argument("-i",
                        "--tree-id-column",
                        metavar="N",
                        type=int,
                        help="id column")
    parser.add_argument("column_data_files",
                        metavar="column-data-file",
                        nargs="+")
    args = parser.parse_args(args)

    tb = smartcols.Table()
    tb.colors = os.isatty(sys.stdout.fileno())
    if args.maxout:
        tb.maxout = args.maxout
    if args.width:
        tb.termforce = "always"
        tb.termwidth = args.width
    if args.json:
        raise NotImplementedError()
    if args.raw:
        raise NotImplementedError()
    if args.export:
        raise NotImplementedError()
    if args.colsep:
        tb.column_separator = args.colsep

    for col in args.column:
        cl = None
        with open(col, "r") as fobj:
            for nlines, line in enumerate(fobj):
                if line.endswith("\n"):
                    line = line[:-1]
                if nlines == 0:
                    # NAME
                    cl = smartcols.Column(line)
                elif nlines == 1:
                    # WIDTH-HINT
                    cl.whint = float(line)
                elif nlines == 2:
                    # FLAGS
                    if line == "none":
                        continue
                    flags = line.split(",")
                    for flag in flags:
                        if flag == "wrapnl":
                            flag = "wrap"
                            cl.safechars = "\n"
                            cl.set_wrapfunc(smartcols.wrapnl_chunksize,
                                            smartcols.wrapnl_nextchunk)
                        setattr(cl, flag, True)
                elif nlines == 3:
                    # COLOR
                    cl.color = line
                else:
                    break
        tb.add_column(cl)

    for n in range(args.nlines):
        tb.new_line()

    lines = tb.lines()
    columns = tb.columns()
    for i, cl_data_file in enumerate(args.column_data_files):
        with open(cl_data_file, "r") as fobj:
            for n, data in enumerate(fobj):
                if data.endswith("\n"):
                    data = data[:-1]
                data = data.replace("\\n", "\n")
                data = data.rstrip()
                ln = lines[n]
                cl = columns[i]
                ln[cl] = data

    if tb.tree and args.tree_parent_column >= 0 and args.tree_id_column >= 0:
        columns = tb.columns()
        parent_col = columns[args.tree_parent_column]
        col = columns[args.tree_id_column]
        for ln in tb.lines():
            data = ln[parent_col].data
            if data is not None:
                try:
                    parent = next(l for l in tb.lines() if l[col].data == data)
                except StopIteration:
                    pass
                else:
                    ln.parent = parent

    print(tb)