From 28b16a8106bcd5313a04c85b5df34bb191d6904d Mon Sep 17 00:00:00 2001 From: Saul Pwanson Date: Thu, 16 May 2024 14:13:27 -0700 Subject: [PATCH] [path-] fix undercounted progress for multibyte chars #2323 Co-authored-by: @midichef --- visidata/path.py | 10 +++++----- visidata/threads.py | 10 ++++++++-- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/visidata/path.py b/visidata/path.py index 1d44a8662..44d136048 100644 --- a/visidata/path.py +++ b/visidata/path.py @@ -134,13 +134,13 @@ def read(self, size=-1): r = self.fp_orig_read(size) if self.prog: if r: - self.prog.addProgress(len(r)) + self.prog.addProgress(r) return r def readline(self, size=-1): r = self.fp_orig_readline(size) if self.prog: - self.prog.addProgress(len(r)) + self.prog.addProgress(r) return r def __getattr__(self, k): @@ -152,7 +152,7 @@ def __enter__(self): def __next__(self): r = next(self.fp) - self.prog.addProgress(len(r)) + self.prog.addProgress(r) return r def __iter__(self): @@ -160,7 +160,7 @@ def __iter__(self): yield from self.fp else: for line in self.fp: - self.prog.addProgress(len(line)) + self.prog.addProgress(line) yield line def __exit__(self, type, value, tb): @@ -332,7 +332,7 @@ def __iter__(self): with Progress(total=filesize(self)) as prog: with self.open(encoding=vd.options.encoding) as fd: for i, line in enumerate(fd): - prog.addProgress(len(line)) + prog.addProgress(line) yield line.rstrip('\n') def open_bytes(self, mode='rb'): diff --git a/visidata/threads.py b/visidata/threads.py index 54e9a369f..c7391fc89 100644 --- a/visidata/threads.py +++ b/visidata/threads.py @@ -62,8 +62,10 @@ def __enter__(self): self.sheet.progresses.insert(0, self) return self - def addProgress(self, n): + def addProgress(self, n:'int|str|bytes'): 'Increase the progress count by *n*.' + if isinstance(n, (str, bytes)): + n = len(n) self.made += n return True @@ -83,9 +85,13 @@ def __init__(self, encoding='utf-8', **kwargs): super().__init__(**kwargs) self.est_sample = '' self.est_charbytes = 1 + self.encoding = encoding - def addProgress(self, n:int): + def addProgress(self, n:'int|str|bytes'): if self.made < self.total: + if isinstance(n, (str, bytes)): + self.addSample(n) + n = len(n) return super().addProgress(n * self.est_charbytes) def addSample(self, s:str):