diff --git a/examples/advanced/connect4/connect4/__main__.py b/examples/advanced/connect4/connect4/__main__.py index 1cc9a833..7075ef9d 100644 --- a/examples/advanced/connect4/connect4/__main__.py +++ b/examples/advanced/connect4/connect4/__main__.py @@ -166,4 +166,4 @@ def on_mouse(self, mouse_event): if __name__ == "__main__": - run_gadget_as_app(Connect4()) + run_gadget_as_app(Connect4(), title="Connect4") diff --git a/examples/advanced/game_of_life.py b/examples/advanced/game_of_life.py index 7bf0f7f1..0c9b80a5 100644 --- a/examples/advanced/game_of_life.py +++ b/examples/advanced/game_of_life.py @@ -76,4 +76,6 @@ def on_mouse(self, mouse_event): if __name__ == "__main__": - run_gadget_as_app(Life(size_hint={"height_hint": 1.0, "width_hint": 1.0})) + run_gadget_as_app( + Life(size_hint={"height_hint": 1.0, "width_hint": 1.0}), title="Game of Life" + ) diff --git a/examples/advanced/labyrinth.py b/examples/advanced/labyrinth.py index b26dd9ea..a5a7c1d6 100644 --- a/examples/advanced/labyrinth.py +++ b/examples/advanced/labyrinth.py @@ -143,4 +143,6 @@ def on_key(self, key_event): if __name__ == "__main__": - run_gadget_as_app(Labyrinth(size_hint={"height_hint": 1.0, "width_hint": 1.0})) + run_gadget_as_app( + Labyrinth(size_hint={"height_hint": 1.0, "width_hint": 1.0}), title="Labyrinth" + ) diff --git a/examples/advanced/minesweeper/minesweeper/__main__.py b/examples/advanced/minesweeper/minesweeper/__main__.py index ac4ff3a8..9c6f5087 100644 --- a/examples/advanced/minesweeper/minesweeper/__main__.py +++ b/examples/advanced/minesweeper/minesweeper/__main__.py @@ -3,4 +3,4 @@ from .minesweeper import MineSweeper if __name__ == "__main__": - run_gadget_as_app(MineSweeper()) + run_gadget_as_app(MineSweeper(), title="MineSweeper") diff --git a/examples/advanced/navier_stokes.py b/examples/advanced/navier_stokes.py index 336c821d..219d73c3 100644 --- a/examples/advanced/navier_stokes.py +++ b/examples/advanced/navier_stokes.py @@ -113,4 +113,7 @@ async def _update(self): if __name__ == "__main__": - run_gadget_as_app(Fluid(size_hint={"height_hint": 1.0, "width_hint": 1.0})) + run_gadget_as_app( + Fluid(size_hint={"height_hint": 1.0, "width_hint": 1.0}), + title="Fluid Simulation", + ) diff --git a/examples/advanced/pipes.py b/examples/advanced/pipes.py index 44b43f91..db8d7ab3 100644 --- a/examples/advanced/pipes.py +++ b/examples/advanced/pipes.py @@ -113,5 +113,6 @@ async def pipe(self): if __name__ == "__main__": run_gadget_as_app( - Pipes(npipes=5, size_hint={"height_hint": 1.0, "width_hint": 1.0}) + Pipes(npipes=5, size_hint={"height_hint": 1.0, "width_hint": 1.0}), + title="Pipe Dreams", ) diff --git a/examples/advanced/sandbox/sandbox/__main__.py b/examples/advanced/sandbox/sandbox/__main__.py index 40493a81..3027401a 100644 --- a/examples/advanced/sandbox/sandbox/__main__.py +++ b/examples/advanced/sandbox/sandbox/__main__.py @@ -3,4 +3,4 @@ from .sandbox import Sandbox if __name__ == "__main__": - run_gadget_as_app(Sandbox(size=(31, 100))) + run_gadget_as_app(Sandbox(size=(31, 100)), title="Sandbox") diff --git a/examples/advanced/sliding_puzzle.py b/examples/advanced/sliding_puzzle.py index f595cd18..ba1121b0 100644 --- a/examples/advanced/sliding_puzzle.py +++ b/examples/advanced/sliding_puzzle.py @@ -70,4 +70,4 @@ def __init__(self, path: Path, **kwargs): if __name__ == "__main__": - run_gadget_as_app(SlidingPuzzle(PATH_TO_LOGO, size=SIZE)) + run_gadget_as_app(SlidingPuzzle(PATH_TO_LOGO, size=SIZE), title="Sliding Puzzle") diff --git a/examples/basic/color_picker.py b/examples/basic/color_picker.py index 9329e7d5..d3100362 100644 --- a/examples/basic/color_picker.py +++ b/examples/basic/color_picker.py @@ -2,4 +2,7 @@ from batgrl.gadgets.color_picker import ColorPicker if __name__ == "__main__": - run_gadget_as_app(ColorPicker(size_hint={"height_hint": 1.0, "width_hint": 1.0})) + run_gadget_as_app( + ColorPicker(size_hint={"height_hint": 1.0, "width_hint": 1.0}), + title="Color Picker", + ) diff --git a/src/batgrl/app.py b/src/batgrl/app.py index 5edefeb2..8f7c7ea4 100644 --- a/src/batgrl/app.py +++ b/src/batgrl/app.py @@ -414,18 +414,64 @@ def children(self) -> list[Gadget] | None: return self.root.children -def run_gadget_as_app(gadget: Gadget) -> None: +def run_gadget_as_app( + gadget: Gadget, + *, + bg_color: Color = BLACK, + title: str | None = None, + inline: bool = False, + inline_height: int = 10, + color_theme: ColorTheme = DEFAULT_COLOR_THEME, + double_click_timeout: float = 0.5, + render_interval: float = 0.0, + redirect_stderr: Path | None = None, + render_mode: Literal["regions", "painter"] = "regions", +) -> None: """ - Run a gadget as a full-screen app. + Run a gadget as an app. + + This convenience function provided for cases when the app would only have a single + gadget. Parameters ---------- gadget : Gadget - A gadget to run as a full screen app. + A gadget to run as an app. + bg_color : Color, default: BLACK + Background color of app. + title : str | None, default: None + The terminal's title. + inline : bool, default: False + Whether to render app inline or in the alternate screen. + inline_height :int, default: 10 + Height of app if rendered inline. + color_theme : ColorTheme, default: DEFAULT_COLOR_THEME + Color theme for :class:`batgrl.gadgets.behaviors.themable.Themable` gadgets. + double_click_timeout : float, default: 0.5 + Max duration of a double-click. + render_interval : float, default: 0.0 + Duration in seconds between consecutive frame renders. + redirect_stderr : Path | None, default: None + If provided, stderr is written to this path. + render_mode : Literal["regions", "painter"], default: "regions" + Determines how the gadget tree is rendered. ``"painter"`` fully paints every + gadget back-to-front. ``"regions"`` only paints the visible portion of each + gadget. ``"painter"`` may be more efficient for a large number of + non-overlapping gadgets. """ class _DefaultApp(App): async def on_start(self): self.add_gadget(gadget) - _DefaultApp(title=type(gadget).__name__).run() + _DefaultApp( + bg_color=bg_color, + title=title, + inline=inline, + inline_height=inline_height, + color_theme=color_theme, + double_click_timeout=double_click_timeout, + render_interval=render_interval, + redirect_stderr=redirect_stderr, + render_mode=render_mode, + ).run()