diff --git a/docs/exporter.md b/docs/exporter.md index 7e66bda..97c5356 100644 --- a/docs/exporter.md +++ b/docs/exporter.md @@ -71,7 +71,9 @@ quality, or 300 DPI if no compression preset was selected, then tries lower resolutions: 150, 120, 96, 72, 50, and 36 DPI. Each pass uses the original PDF to avoid accumulating compression damage. It stops at the first result strictly below the target, with at most seven attempts. Progress shows each attempt and -its resulting byte count. A PDF already below the target is copied unchanged. +its resulting byte count. After the first completed pass, it estimates the +remaining time from the average pass duration so far. A PDF already below the +target is copied unchanged. Very small targets can noticeably degrade images, and some documents cannot reach the requested size. In that case the command fails, reports the smallest @@ -82,8 +84,9 @@ PDF compression writes a separate export and never changes the input. Ghostscript rewrites the PDF, so some interactive content or annotations may change. See its [PDF output documentation](https://ghostscript.readthedocs.io/en/latest/VectorDevices.html). -Interactive terminals show an updating progress line, with page counts for PDF -rendering and a spinner during DeckCraft's bulk export. Only output paths, the +Interactive terminals show an updating progress line, with page counts and an +approximate remaining time after the first completed page or compression pass. +DeckCraft operations without measurable units use a spinner. Only output paths, the final summary, and warnings/errors remain afterward. Redirected output uses plain line logs. Set `NO_COLOR` to disable colors or `TERM=dumb` to disable animation as well. diff --git a/tests/test_exporter.py b/tests/test_exporter.py index f178b88..8ec4ddb 100644 --- a/tests/test_exporter.py +++ b/tests/test_exporter.py @@ -132,6 +132,18 @@ class ExporterTests(unittest.TestCase): self.assertEqual((self.root / 'input_export/input.pdf').stat().st_size, 900) self.assertEqual(source.stat().st_size, 2000) + def test_target_compression_updates_progress_for_each_attempt(self): + source = self.root / 'input.pdf' + source.write_bytes(b'x' * 2000) + bar = unittest.mock.Mock() + with contextlib.redirect_stdout(io.StringIO()), \ + patch.object(exporter.shutil, 'which', return_value='/fake/gs'), \ + patch.object(exporter, 'run', side_effect=self.compressor([1500, 900], [])): + result = exporter.compress_pdf(source, self.root, 'email', 1000, bar) + self.assertEqual(result.stat().st_size, 900) + self.assertEqual(bar.set.call_args_list, + [unittest.mock.call(0, 6), unittest.mock.call(1), unittest.mock.call(2)]) + def test_unreachable_target_is_bounded_and_preserves_existing_output(self): source = self.root / 'input.pdf' source.write_bytes(b'x' * 2000) diff --git a/tests/test_progress.py b/tests/test_progress.py index 3455ba6..a9d3d62 100644 --- a/tests/test_progress.py +++ b/tests/test_progress.py @@ -69,6 +69,25 @@ class ProgressTests(unittest.TestCase): self.assertEqual(output.getvalue(), '[INFO] Step\n') self.assertEqual(terminal.getvalue(), '') + def test_eta_uses_average_time_of_completed_work(self): + output, terminal = Terminal(), Terminal() + with patch.dict(os.environ, {'TERM': 'xterm', 'NO_COLOR': '1'}), \ + contextlib.redirect_stdout(output), contextlib.redirect_stderr(terminal), \ + patch.object(progress.time, 'monotonic', return_value=40): + bar = progress.Progress() + bar.text = 'Rendering' + bar.started = 0 + bar.phase_started = 10 + bar.done = 2 + bar.total = 5 + bar._draw() + self.assertIn('2/5 ~45s left', terminal.getvalue()) + + def test_duration_formats_long_estimates_compactly(self): + self.assertEqual(progress._duration(5.4), '5s') + self.assertEqual(progress._duration(89.6), '1m 30s') + self.assertEqual(progress._duration(7380), '2h 03m') + if __name__ == '__main__': unittest.main() diff --git a/tools/exporter.py b/tools/exporter.py index 8a0ee42..3058149 100644 --- a/tools/exporter.py +++ b/tools/exporter.py @@ -53,7 +53,7 @@ def byte_size(value): return size -def compress_pdf(source, work, quality, target_size): +def compress_pdf(source, work, quality, target_size, progress=None): """Try bounded downsampling passes, always using the original PDF.""" original_size = source.stat().st_size if target_size and original_size < target_size: @@ -66,6 +66,8 @@ def compress_pdf(source, work, quality, target_size): resolutions = [first_dpi] if target_size: resolutions += [dpi for dpi in (150, 120, 96, 72, 50, 36) if dpi < first_dpi] + if progress: + progress.set(0, len(resolutions)) best, best_size = source, original_size for attempt, dpi in enumerate(resolutions, 1): status(f'Compressing PDF [{attempt}/{len(resolutions)}]: {dpi} DPI images' @@ -84,6 +86,8 @@ def compress_pdf(source, work, quality, target_size): if not candidate.is_file() or not candidate.stat().st_size: raise RuntimeError('Ghostscript did not create a PDF.') size = candidate.stat().st_size + if progress: + progress.set(attempt) status(f'Compression pass: {size:,} bytes (original {original_size:,}).') if size < best_size: best, best_size = candidate, size @@ -179,7 +183,7 @@ def export(source, formats, destination, dpi, overwrite, cli_override, progress= if not pdf.is_file() or not pdf.stat().st_size: raise RuntimeError('DeckCraft did not create a PDF.') if compression: - pdf = compress_pdf(pdf, work, pdf_quality, target_size) + pdf = compress_pdf(pdf, work, pdf_quality, target_size, progress) if 'images' in formats: rendered.mkdir() if is_deck: diff --git a/tools/progress.py b/tools/progress.py index 3e69834..9364e49 100644 --- a/tools/progress.py +++ b/tools/progress.py @@ -8,6 +8,17 @@ import time _active = None +def _duration(seconds): + seconds = max(0, round(seconds)) + minutes, seconds = divmod(seconds, 60) + hours, minutes = divmod(minutes, 60) + if hours: + return f'{hours}h {minutes:02d}m' + if minutes: + return f'{minutes}m {seconds:02d}s' + return f'{seconds}s' + + def status(message, level='info', *, persist=False): if _active is not None: _active.message(message, level, persist) @@ -38,6 +49,7 @@ class Progress: self.done = 0 self.total = None self.started = time.monotonic() + self.phase_started = self.started self.previous = None def __enter__(self): @@ -59,6 +71,8 @@ class Progress: def set(self, done, total=None): with self.lock: + if done == 0 or (total is not None and total != self.total) or done < self.done: + self.phase_started = time.monotonic() self.done = done if total is not None: self.total = total @@ -89,6 +103,10 @@ class Progress: if self.total: filled = min(16, max(0, int(16 * self.done / self.total))) indicator = '[' + '=' * filled + '-' * (16 - filled) + f'] {self.done}/{self.total}' + measured = time.monotonic() - self.phase_started + if 0 < self.done < self.total and measured >= 0.1: + remaining = measured / self.done * (self.total - self.done) + indicator += f' ~{_duration(remaining)} left' else: indicator = spinner width = max(1, shutil.get_terminal_size((80, 24)).columns - 1)