Examples

Table with title

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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_data = tb.new_column("DATA")

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

    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)

Wrapping lines in table

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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"
        ln[cl_text] = gen_text("T", 50)
        return ln

    ln = add_line("A")
    add_line("aa", ln)
    add_line("ab", ln)

    ln = add_line("B")
    xln = add_line("ba", ln)
    add_line("baa", xln)
    add_line("bab", xln)
    add_line("bb", ln)

    print(tb)

Continuous printing of table

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
from __future__ import division, print_function, unicode_literals
import locale
import time

import smartcols

TIME_PERIOD = 3.0

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

    tb = smartcols.Table()
    tb.maxout = True
    cl_num = tb.new_column("#NUM")
    cl_num.whint = 0.1
    cl_num.right = True
    cl_data = tb.new_column("DATA")
    cl_data.whint = 0.7
    cl_time = tb.new_column("TIME")
    cl_time.whint = 0.2

    last = time.time()
    for i in range(10):
        ln = tb.new_line()
        ln[cl_num] = str(i)
        ln[cl_data] = "data-{:0>2d}-{:0>2d}-{:0>2d}-end".format(i + 1, i + 2, i + 3)

        done = False
        while not done:
            now = time.time()
            diff = now - last
            if (now - last) >= TIME_PERIOD:
                done = True
            else:
                time.sleep(0.1)

            ln[cl_time] = "{:f} [{: >3d}%]".format(diff, int(diff / (TIME_PERIOD / 100)))

            s = tb.str_line(ln)
            if not done:
                print("{}\r".format(s), end='')
            else:
                print(s)

        last = now