Skip to content

Commit 5602a1a

Browse files
committed
Fix issues with printing with progress bar
1 parent be1855a commit 5602a1a

File tree

1 file changed

+53
-15
lines changed

1 file changed

+53
-15
lines changed

philh_myftp_biz/pc.py

Lines changed: 53 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
from typing import Literal, Self, Generator, TYPE_CHECKING
1+
from typing import Literal, Self, Generator, TYPE_CHECKING, Callable, Any
22

33
if TYPE_CHECKING:
4-
from .db import colors
54
from .time import from_stamp
65
from psutil import Process
6+
from .db import colors
77

88
def NAME() -> str:
99
"""
@@ -476,6 +476,16 @@ def open(self, mode='r'):
476476
"""
477477
return open(self.path, mode)
478478

479+
class BasePipe:
480+
481+
def __init__(self):
482+
pass
483+
484+
write: Callable[[Self, Any], None]
485+
486+
def flush(self):
487+
pass
488+
479489
def cwd() -> Path:
480490
"""
481491
Get the Current Working Directory
@@ -539,6 +549,8 @@ class terminal:
539549
"""
540550
Misc. Functions for the Terminal/Console
541551
"""
552+
553+
from sys import stdout, stderr
542554

543555
def width() -> int:
544556
"""
@@ -1004,45 +1016,71 @@ def PIDs(self) -> Generator[int]:
10041016

10051017
class ProgressBar:
10061018

1019+
class Pipe(BasePipe):
1020+
1021+
def __init__(self,
1022+
pbar: 'ProgressBar'
1023+
):
1024+
from sys import stdout
1025+
1026+
self.pbar = pbar
1027+
self.tqdm = pbar._tqdm
1028+
self.sys = stdout
1029+
1030+
def write(self, s:str):
1031+
1032+
if self.pbar.finished():
1033+
1034+
self.sys.write(s)
1035+
1036+
else:
1037+
1038+
self.tqdm.clear()
1039+
1040+
self.sys.write(s)
1041+
10071042
__bar_format = "{n_fmt}/{total_fmt} | {bar} | {elapsed}"
10081043

10091044
def __init__(self,
10101045
total: int = 0
10111046
):
10121047
from .__init__ import thread
1013-
from tqdm import tqdm
1048+
from tqdm.auto import tqdm
1049+
import sys
10141050

1015-
self.__tqdm = tqdm(
1051+
self._tqdm = tqdm(
10161052
iterable = range(total),
10171053
bar_format = self.__bar_format,
1018-
dynamic_ncols = True,
1054+
dynamic_ncols = True
10191055
)
10201056

1021-
self.reset = self.__tqdm.reset
1022-
self.stop = self.__tqdm.close
1023-
self.step = self.__tqdm.update
1057+
self.reset = self._tqdm.reset
1058+
self.stop = self._tqdm.close
1059+
self.step = self._tqdm.update
1060+
1061+
self.total = self._tqdm.total
10241062

1025-
self.total = self.__tqdm.total
1063+
sys.stdout = self.Pipe(pbar=self)
10261064

10271065
thread(self.__refresh)
10281066

10291067
def finished(self) -> bool:
10301068

1031-
if self.__tqdm.total == 0:
1069+
if self._tqdm.total == 0:
10321070
return False
10331071
else:
1034-
return (self.__tqdm.n == self.__tqdm.total)
1072+
return (self._tqdm.n == self._tqdm.total)
10351073

10361074
def running(self):
1037-
return not (self.finished() or self.__tqdm.disable)
1075+
return not (self.finished() or self._tqdm.disable)
10381076

10391077
def set_total(self, total:int):
1040-
self.__tqdm.total = total
1078+
self._tqdm.total = total
10411079

10421080
def step_total(self,
10431081
n: float = 1
10441082
):
1045-
self.__tqdm.total += n
1083+
self._tqdm.total += n
10461084

10471085
def __refresh(self):
10481086
from .time import sleep
@@ -1053,4 +1091,4 @@ def __refresh(self):
10531091
sleep(.3)
10541092

10551093
# Update the timer
1056-
self.__tqdm.refresh()
1094+
self._tqdm.refresh()

0 commit comments

Comments
 (0)