From c8d0b70d97fbe164f1d1c6bbf744edf7dc25845d Mon Sep 17 00:00:00 2001 From: Jacob Lifshay Date: Mon, 29 Aug 2022 20:53:55 -0700 Subject: [PATCH] write progress to terminal, even if output is redirected --- src/budget_sync/util.py | 33 ++++++++++++++++++++++++++------- 1 file changed, 26 insertions(+), 7 deletions(-) diff --git a/src/budget_sync/util.py b/src/budget_sync/util.py index f26c1a0..7a4170c 100644 --- a/src/budget_sync/util.py +++ b/src/budget_sync/util.py @@ -5,6 +5,7 @@ from bugzilla.bug import Bug from typing import Any, Callable, Dict, Iterator, List, Type, Union from enum import Enum from io import StringIO +import os class BugStatus(Enum): @@ -37,13 +38,31 @@ class BugStatus(Enum): def all_bugs(bz: Bugzilla) -> Iterator[Bug]: chunk_start = 1 chunk_size = 100 - while True: - bugs = bz.getbugs(list(range(chunk_start, chunk_start + chunk_size))) - chunk_start += chunk_size - print("bugs loaded", len(bugs), chunk_start, flush=True) - if len(bugs) == 0: - return - yield from bugs + try: + if hasattr(os, "ctermid"): + term = open(os.ctermid(), "wt", encoding="utf-8") + elif os.name == "nt": + term = open("CONOUT$", "wt", encoding="utf-8") + else: + term = None + except OSError: + # no terminal available + term = None + try: # can't use `with` since it doesn't work with None + while True: + bugs = list(range(chunk_start, chunk_start + chunk_size)) + bugs = bz.getbugs(bugs) + chunk_start += chunk_size + # progress indicator, should go to terminal + if term is not None: + print("bugs loaded", len(bugs), chunk_start, flush=True, + file=term) + if len(bugs) == 0: + return + yield from bugs + finally: + if term is not None: + term.close() class SequencePrettyPrinter: -- 2.30.2