File size: 2,516 Bytes
2c3c408
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
from textual.app import App, ComposeResult
from textual.widgets import DataTable

from rich.syntax import Syntax
from rich.table import Table

CODE = '''\
def loop_first_last(values: Iterable[T]) -> Iterable[tuple[bool, bool, T]]:
    """Iterate and generate a tuple with a flag for first and last value."""
    iter_values = iter(values)
    try:
        previous_value = next(iter_values)
    except StopIteration:
        return
    first = True
    for value in iter_values:
        yield first, False, previous_value
        first = False
        previous_value = value
    yield first, True, previous_value'''

test_table = Table(title="Star Wars Movies")

test_table.add_column("Released", style="cyan", no_wrap=True)
test_table.add_column("Title", style="magenta")
test_table.add_column("Box Office", justify="right", style="green")

test_table.add_row("Dec 20, 2019", "Star Wars: The Rise of Skywalker", "$952,110,690")
test_table.add_row("May 25, 2018", "Solo: A Star Wars Story", "$393,151,347")
test_table.add_row(
    "Dec 15, 2017", "Star Wars Ep. V111: The Last Jedi", "$1,332,539,889"
)
test_table.add_row("Dec 16, 2016", "Rogue One: A Star Wars Story", "$1,332,439,889")


class TableApp(App):
    def compose(self) -> ComposeResult:
        table = self.table = DataTable(id="data")
        yield table

        table.add_column("Foo")
        table.add_column("Bar")
        table.add_column("Baz")
        table.add_column("Foo")
        table.add_column("Bar")
        table.add_column("Baz")

        for n in range(200):
            height = 1
            row = [f"row [b]{n}[/b] col [i]{c}[/i]" for c in range(6)]
            if n == 10:
                row[1] = Syntax(
                    CODE,
                    "python",
                    theme="ansi_dark",
                    line_numbers=True,
                    indent_guides=True,
                )
                height = 13

            if n == 30:
                row[1] = test_table
                height = 13
            table.add_row(*row, height=height)

        table.focus()

    def on_mount(self):
        self.bind("d", "toggle_dark")
        self.bind("z", "toggle_zebra")
        self.bind("x", "exit")

    def action_toggle_dark(self) -> None:
        self.app.dark = not self.app.dark

    def action_toggle_zebra(self) -> None:
        self.table.zebra_stripes = not self.table.zebra_stripes

    def action_exit(self) -> None:
        pass


app = TableApp()
if __name__ == "__main__":
    print(app.run())