Skip to content

Commit

Permalink
Add all App keyword arguments to run_gadget_as_app.
Browse files Browse the repository at this point in the history
`run_gadget_as_app` no longer sets terminal title to gadget name.
  • Loading branch information
salt-die committed Aug 15, 2024
1 parent d2ca6d7 commit 0358642
Show file tree
Hide file tree
Showing 10 changed files with 70 additions and 13 deletions.
2 changes: 1 addition & 1 deletion examples/advanced/connect4/connect4/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
4 changes: 3 additions & 1 deletion examples/advanced/game_of_life.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
4 changes: 3 additions & 1 deletion examples/advanced/labyrinth.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
2 changes: 1 addition & 1 deletion examples/advanced/minesweeper/minesweeper/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
from .minesweeper import MineSweeper

if __name__ == "__main__":
run_gadget_as_app(MineSweeper())
run_gadget_as_app(MineSweeper(), title="MineSweeper")
5 changes: 4 additions & 1 deletion examples/advanced/navier_stokes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
)
3 changes: 2 additions & 1 deletion examples/advanced/pipes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
)
2 changes: 1 addition & 1 deletion examples/advanced/sandbox/sandbox/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
2 changes: 1 addition & 1 deletion examples/advanced/sliding_puzzle.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
5 changes: 4 additions & 1 deletion examples/basic/color_picker.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
)
54 changes: 50 additions & 4 deletions src/batgrl/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

0 comments on commit 0358642

Please sign in to comment.