]> git.scottworley.com Git - paperdoorknob/commitdiff
--quiet flag to suppress progress messages
authorScott Worley <scottworley@scottworley.com>
Sat, 30 Dec 2023 00:19:00 +0000 (16:19 -0800)
committerScott Worley <scottworley@scottworley.com>
Sat, 30 Dec 2023 00:26:14 +0000 (16:26 -0800)
args.py
paperdoorknob.py
paperdoorknob_test.py
spec.py

diff --git a/args.py b/args.py
index 384e4487b76bfb918f7316dee8604276f92e1cc5..c5506729797f32ac2e8aaa9b050aae065da81082 100644 (file)
--- 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)
index 26a60fbc0524f5daf46fbc8f92e131c011e0fef1..4e7cdf6840b8bbd7fb6a227db538bc9db9cde915 100644 (file)
@@ -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')
 
 
index bafce174a1f0af81b46796297cd813d5adf14c57..8527dd3144f91d91a42b88995c65e5b78837e1e2 100644 (file)
@@ -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 77a6a127ceff5da3fa273268ddad11bdcb4583ad..2eccbb5562fd359c2148f0039d8e637f9d10474d 100644 (file)
--- a/spec.py
+++ b/spec.py
@@ -28,3 +28,4 @@ class Spec:
     layout: Layout
     geometry: str | None
     texout: IO[bytes]
+    log: Callable[[str], None]