Skip to content

Commit bd91a11

Browse files
committed
Show README logs and use relative SVG links
1 parent 18c5a05 commit bd91a11

4 files changed

Lines changed: 67 additions & 10 deletions

File tree

README.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ unknown-length progress, and pipe-friendly CLI usage.
1414
:alt: coverage status
1515
:target: https://coveralls.io/r/WoLpH/python-progressbar?branch=master
1616

17-
.. image:: https://raw.githubusercontent.com/WoLpH/python-progressbar/develop/docs/_static/progressbar-hero.svg
17+
.. image:: docs/_static/progressbar-hero.svg
1818
:alt: progressbar2 showing clean progress output with logs
1919

2020
Install
@@ -73,19 +73,19 @@ Prints and logs while the bar is active
7373
Multiple bars
7474
==============================================================================
7575

76-
.. image:: https://raw.githubusercontent.com/WoLpH/python-progressbar/develop/docs/_static/progressbar-multibar.svg
76+
.. image:: docs/_static/progressbar-multibar.svg
7777
:alt: multiple progress bars updating together
7878

7979
Unknown length and animated bars
8080
==============================================================================
8181

82-
.. image:: https://raw.githubusercontent.com/WoLpH/python-progressbar/develop/docs/_static/progressbar-unknown-length.svg
82+
.. image:: docs/_static/progressbar-unknown-length.svg
8383
:alt: unknown length progress with an animated marker
8484

8585
Tqdm-style ergonomic options
8686
==============================================================================
8787

88-
.. image:: https://raw.githubusercontent.com/WoLpH/python-progressbar/develop/docs/_static/progressbar-ergonomics.svg
88+
.. image:: docs/_static/progressbar-ergonomics.svg
8989
:alt: progressbar2 tqdm-style desc total unit scale and postfix output
9090

9191
CLI usage

docs/_static/progressbar-hero.svg

Lines changed: 3 additions & 3 deletions
Loading

scripts/render_readme_demos.py

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class Demo:
2828
name: str
2929
title: str
3030
snippet: str
31+
log_lines: int = 0
3132

3233

3334
DEMOS = [
@@ -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

180185
def normalize_terminal_line(line: str) -> str:
@@ -185,6 +190,12 @@ def normalize_terminal_line(line: str) -> str:
185190

186191

187192
def 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

209241
def limit_animation_frames(frames: list[list[str]]) -> list[list[str]]:

tests/test_readme_demos.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,19 @@ def test_demo_definitions_are_ordered_and_exercise_key_features() -> None:
8787
assert 'postfix=' in snippets['ergonomics']
8888

8989

90+
def test_readme_uses_branch_relative_demo_assets() -> None:
91+
readme = (demos.ROOT / 'README.rst').read_text(encoding='utf-8')
92+
93+
assert (
94+
'raw.githubusercontent.com/WoLpH/python-progressbar/develop'
95+
not in readme
96+
)
97+
assert '.. image:: docs/_static/progressbar-hero.svg' in readme
98+
assert '.. image:: docs/_static/progressbar-multibar.svg' in readme
99+
assert '.. image:: docs/_static/progressbar-unknown-length.svg' in readme
100+
assert '.. image:: docs/_static/progressbar-ergonomics.svg' in readme
101+
102+
90103
def test_render_svg_first_frame_is_visible_without_animation(
91104
tmp_path: Path,
92105
) -> None:
@@ -205,6 +218,18 @@ def test_readme_demos_show_wide_determinate_progress_bars() -> None:
205218
assert max(_bar_widths(frames_by_name[name])) >= 32
206219

207220

221+
def test_hero_demo_keeps_recent_logs_visible_above_progress() -> None:
222+
demo = next(demo for demo in demos.DEMOS if demo.name == 'hero')
223+
frames = demos.capture_demo(demo)
224+
225+
assert max(len(frame) for frame in frames) >= 3
226+
assert any(
227+
any(line.startswith('log: completed step 8') for line in frame)
228+
and any('Build:' in line for line in frame)
229+
for frame in frames
230+
)
231+
232+
208233
def test_capture_frames_normalizes_variable_timing_text() -> None:
209234
frames = demos.frames_from_output(
210235
'Build: 50% Elapsed Time: 0:01:23 ETA: 9:08:07\n'

0 commit comments

Comments
 (0)