55from typing import Iterable
66
77from textual .app import App
8+ from textual .pilot import Pilot
89from textual ._import_app import import_app
910
1011
@@ -18,7 +19,7 @@ def format_svg(source, language, css_class, options, md, attrs, **kwargs) -> str
1819 path = cmd [0 ]
1920
2021 _press = attrs .get ("press" , None )
21- press = [* _press .split ("," )] if _press else ["_" ]
22+ press = [* _press .split ("," )] if _press else []
2223 title = attrs .get ("title" )
2324
2425 print (f"screenshotting { path !r} " )
@@ -28,7 +29,7 @@ def format_svg(source, language, css_class, options, md, attrs, **kwargs) -> str
2829 rows = int (attrs .get ("lines" , 24 ))
2930 columns = int (attrs .get ("columns" , 80 ))
3031 svg = take_svg_screenshot (
31- None , path , press , title , terminal_size = (rows , columns )
32+ None , path , press , title , terminal_size = (columns , rows )
3233 )
3334 finally :
3435 os .chdir (cwd )
@@ -45,9 +46,9 @@ def format_svg(source, language, css_class, options, md, attrs, **kwargs) -> str
4546def take_svg_screenshot (
4647 app : App | None = None ,
4748 app_path : str | None = None ,
48- press : Iterable [str ] = ("_" , ),
49+ press : Iterable [str ] = (),
4950 title : str | None = None ,
50- terminal_size : tuple [int , int ] = (24 , 80 ),
51+ terminal_size : tuple [int , int ] = (80 , 24 ),
5152) -> str :
5253 """
5354
@@ -63,25 +64,29 @@ def take_svg_screenshot(
6364 the screenshot was taken.
6465
6566 """
66- rows , columns = terminal_size
67-
68- os .environ ["COLUMNS" ] = str (columns )
69- os .environ ["LINES" ] = str (rows )
7067
7168 if app is None :
69+ assert app_path is not None
7270 app = import_app (app_path )
7371
72+ assert app is not None
73+
7474 if title is None :
7575 title = app .title
7676
77- app .run (
78- quit_after = 5 ,
79- press = press or ["ctrl+c" ],
77+ async def auto_pilot (pilot : Pilot ) -> None :
78+ app = pilot .app
79+ await pilot .press (* press )
80+ svg = app .export_screenshot (title = title )
81+ app .exit (svg )
82+
83+ svg = app .run (
8084 headless = True ,
81- screenshot = True ,
82- screenshot_title = title ,
85+ auto_pilot = auto_pilot ,
86+ size = terminal_size ,
8387 )
84- svg = app ._screenshot
88+ assert svg is not None
89+
8590 return svg
8691
8792
0 commit comments