From: Scott Worley Date: Sat, 30 Dec 2023 00:19:00 +0000 (-0800) Subject: --quiet flag to suppress progress messages X-Git-Url: http://git.scottworley.com/paperdoorknob/commitdiff_plain/c594c8bf593ab97e863827b7a22f2148bcb174b6?ds=sidebyside --quiet flag to suppress progress messages --- diff --git a/args.py b/args.py index 384e448..c550672 100644 --- a/args.py +++ b/args.py @@ -8,6 +8,7 @@ from argparse import ArgumentParser from contextlib import contextmanager import os.path +from sys import stderr from typing import Iterator @@ -23,6 +24,10 @@ from spec import Spec from texify import PandocTexifier +def _print_status(msg: str) -> None: + print(msg, file=stderr, end='' if msg.endswith('\r') else '\n') + + def _command_line_parser() -> ArgumentParser: parser = ArgumentParser(prog='paperdoorknob', description='Print glowfic') parser.add_argument( @@ -60,6 +65,10 @@ See https://faculty.bard.edu/bloch/geometry.pdf for details '(eg: "%(default)s" produces %(default)s.tex, %(default)s.pdf, etc.)', default='book') parser.add_argument('--pandoc', help='Location of the pandoc executable') + parser.add_argument( + '--quiet', + action='store_true', + help='Suppress progress messages') parser.add_argument( '--texfilters', help='Which TeX filters to use (default: %(default)s)', @@ -96,4 +105,5 @@ def spec_from_commandline_args() -> Iterator[Spec]: lambda x: ApplyTexFilters(args.texfilters, x), layout, args.geometry, - texout) + texout, + (lambda _: None) if args.quiet else _print_status) diff --git a/paperdoorknob.py b/paperdoorknob.py index 26a60fb..4e7cdf6 100644 --- a/paperdoorknob.py +++ b/paperdoorknob.py @@ -4,8 +4,6 @@ # under the terms of the GNU General Public License as published by the # Free Software Foundation, version 3. -from sys import stderr - from typing import Any, Iterable from bs4 import BeautifulSoup @@ -42,13 +40,11 @@ def process(spec: Spec) -> None: num_chunks = ilen(chunkDOMs(html)) for i, r in enumerate(chunkDOMs(html)): percent = 100.0 * i / num_chunks - print( - f'Processing chunk {i} of {num_chunks} ({percent:.1f}%)', - end='\r', - file=stderr) + spec.log(f'Processing chunk {i} of {num_chunks} ({percent:.1f}%)\r') spec.domfilter(r) chunk = makeChunk(r, spec.images) spec.texout.write(spec.texfilter(spec.layout.renderChunk(chunk))) + spec.log('') spec.texout.write(b'\\end{document}\n') diff --git a/paperdoorknob_test.py b/paperdoorknob_test.py index bafce17..8527dd3 100644 --- a/paperdoorknob_test.py +++ b/paperdoorknob_test.py @@ -44,7 +44,8 @@ class BaseTestProcess(ABC): lambda x: x, ContentOnlyLayout(PandocTexifier('pandoc')), 'margin=20mm', - buf) + buf, + lambda _: None) paperdoorknob.process(spec) assert re.match(br'''\\documentclass{article} (\\usepackage{[a-z]+}\n)+\\usepackage\[margin=20mm\]{geometry} @@ -71,7 +72,8 @@ Pretty sure. lambda x: x, ContentOnlyLayout(texifier), None, - buf) + buf, + lambda _: None) paperdoorknob.process(spec) def testPDF(self) -> None: @@ -85,7 +87,8 @@ Pretty sure. lambda x: x, BelowIconLayout(PandocTexifier('pandoc'), 20), None, - out) + out, + lambda _: None) paperdoorknob.process(spec) subprocess.run(['pdflatex', 'test.tex'], stdin=subprocess.DEVNULL, check=True) diff --git a/spec.py b/spec.py index 77a6a12..2eccbb5 100644 --- a/spec.py +++ b/spec.py @@ -28,3 +28,4 @@ class Spec: layout: Layout geometry: str | None texout: IO[bytes] + log: Callable[[str], None]