From 0a5838d9643e80b1bd80232c97123f758a973513 Mon Sep 17 00:00:00 2001 From: Will McGugan Date: Wed, 1 Feb 2023 16:26:55 +0100 Subject: [PATCH] checker example --- docs/examples/guide/checker.py | 70 ++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 docs/examples/guide/checker.py diff --git a/docs/examples/guide/checker.py b/docs/examples/guide/checker.py new file mode 100644 index 000000000..27419bf96 --- /dev/null +++ b/docs/examples/guide/checker.py @@ -0,0 +1,70 @@ +from __future__ import annotations + +from textual.app import App, ComposeResult +from textual.geometry import Size +from textual.strip import Strip +from textual.scroll_view import ScrollView + +from rich.segment import Segment + + +class CheckerBoard(ScrollView): + COMPONENT_CLASSES = { + "checkerboard--white-square", + "checkerboard--black-square", + "checkerboard--void", + } + + DEFAULT_CSS = """ + CheckerBoard { + background: $primary; + } + CheckerBoard .checkerboard--void { + background: $background; + } + + CheckerBoard .checkerboard--white-square { + background: $foreground 70%; + } + CheckerBoard .checkerboard--black-square { + background: $primary; + } + """ + + def on_mount(self) -> None: + self.virtual_size = Size(64, 32) + + def render_line(self, y: int) -> Strip: + """Render a line of the widget. y is relative to the top of the widget.""" + + scroll_x, scroll_y = self.scroll_offset + y += scroll_y + row_index = y // 4 # four lines per row + + white = self.get_component_rich_style("checkerboard--white-square") + black = self.get_component_rich_style("checkerboard--black-square") + void = self.get_component_rich_style("checkerboard--void") + + if row_index >= 8: + return Strip.blank(self.size.width, void) + + is_odd = row_index % 2 + + segments = [ + Segment(" " * 8, black if (column + is_odd) % 2 else white) + for column in range(8) + ] + strip = Strip(segments, 8 * 8) + strip = strip.extend_cell_length(self.size.width, void) + strip = strip.crop(scroll_x, scroll_x + self.size.width) + return strip + + +class BoardApp(App): + def compose(self) -> ComposeResult: + yield CheckerBoard() + + +if __name__ == "__main__": + app = BoardApp() + app.run()