@@ -28,6 +28,7 @@ class Demo:
2828 name : str
2929 title : str
3030 snippet : str
31+ log_lines : int = 0
3132
3233
3334DEMOS = [
@@ -54,6 +55,7 @@ class Demo:
5455 bar.update(step + 1, force=True)
5556 time.sleep(0.005)
5657""" ,
58+ log_lines = 2 ,
5759 ),
5860 Demo (
5961 'multibar' ,
@@ -174,7 +176,10 @@ def capture_demo(demo: Demo) -> list[list[str]]:
174176 except subprocess .TimeoutExpired as error :
175177 raise SystemExit (f'timed out capturing demo: { demo .name } ' ) from error
176178
177- return frames_from_output (result .stdout )
179+ frames = parse_frames (result .stdout )
180+ if demo .log_lines :
181+ frames = keep_recent_logs_with_progress (frames , demo .log_lines )
182+ return limit_animation_frames (frames ) or [['No output captured' ]]
178183
179184
180185def normalize_terminal_line (line : str ) -> str :
@@ -185,6 +190,12 @@ def normalize_terminal_line(line: str) -> str:
185190
186191
187192def frames_from_output (output : str ) -> list [list [str ]]:
193+ return limit_animation_frames (parse_frames (output )) or [
194+ ['No output captured' ]
195+ ]
196+
197+
198+ def parse_frames (output : str ) -> list [list [str ]]:
188199 frames : list [list [str ]] = []
189200 output = output .replace ('\x1b [2K' , '' )
190201 if '\f ' in output :
@@ -196,14 +207,35 @@ def frames_from_output(output: str) -> list[list[str]]:
196207 ]
197208 if lines :
198209 frames .append (lines )
199- return limit_animation_frames ( frames ) or [[ 'No output captured' ]]
210+ return frames
200211
201212 for raw_frame in output .splitlines ():
202213 for part in raw_frame .split ('\r ' ):
203214 line = part .strip ()
204215 if line :
205216 frames .append ([normalize_terminal_line (line )])
206- return limit_animation_frames (frames ) or [['No output captured' ]]
217+ return frames
218+
219+
220+ def keep_recent_logs_with_progress (
221+ frames : list [list [str ]],
222+ log_lines : int ,
223+ ) -> list [list [str ]]:
224+ logs : list [str ] = []
225+ output : list [list [str ]] = []
226+
227+ for frame in frames :
228+ log_frame = [line for line in frame if line .startswith ('log:' )]
229+ progress_frame = [
230+ line for line in frame if not line .startswith ('log:' )
231+ ]
232+ if log_frame :
233+ logs .extend (log_frame )
234+ logs = logs [- log_lines :]
235+ if progress_frame :
236+ output .append (logs + progress_frame )
237+
238+ return output
207239
208240
209241def limit_animation_frames (frames : list [list [str ]]) -> list [list [str ]]:
0 commit comments