Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions textual_image/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

"""Run the textual_image demo."""

from pathlib import Path
import sys
from argparse import ArgumentParser
from importlib.util import find_spec
Expand All @@ -11,20 +12,33 @@
textual_available = bool(find_spec("textual"))
default_mode = "rich" if find_spec("textual") is None else "textual"

def path_exists(path_str: str) -> Path:
"""Check if the provided path exists and is a file."""
path = Path(path_str)
if not path.is_file():
raise ValueError(f"The provided path '{path_str}' does not exist or is not a file.")
return path

parser = ArgumentParser(description="Demo the capabilities of textual-image")
parser.add_argument("mode", choices=["rich", "textual"], nargs="?", default=default_mode)
parser.add_argument("-m", "--method", choices=RENDERING_METHODS.keys(), default="auto")
parser.add_argument("--image", help="Path to an image file to use for the demo. If not provided, a default image will be used.", default=Path(__file__).parent / "gracehopper.jpg", type=path_exists)
parser.add_argument("--transparent", action="store_true", help="Use a transparent background for the demo (only applicable to the Textual mode).")
arguments = parser.parse_args()

if arguments.mode == "rich":
from textual_image.demo.renderable import run as run_rich_demo
from textual_image.demo import renderable

renderable.TEST_IMAGE = Path(arguments.image)

run_rich_demo(arguments.method)
renderable.run(arguments.method)
elif not textual_available:
sys.stderr.write(
"Optional Textual dependency not available. Install this package as `textual-image[textual]` for Textual support."
)
else:
from textual_image.demo.widget import run as run_textual_demo
from textual_image.demo import widget

widget.TEST_IMAGE = Path(arguments.image)

run_textual_demo(arguments.method)
widget.run(arguments.method, transparent=arguments.transparent)
4 changes: 2 additions & 2 deletions textual_image/demo/widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -328,9 +328,9 @@ def set_rendering_method(self, rendering_method: str) -> None:
self.image_type = rendering_method


def run(rendering_method: str = "auto") -> None:
def run(rendering_method: str = "auto", transparent: bool = False) -> None:
"""Showcase textual-image's Rich renderables."""
app = DemoApp()
app = DemoApp(ansi_color=transparent)
app.image_type = rendering_method
app.run()

Expand Down