Skip to content

Commit

Permalink
[path-] fix undercounted progress for multibyte chars #2323
Browse files Browse the repository at this point in the history
Co-authored-by: @midichef
  • Loading branch information
saulpw authored and anjakefala committed May 18, 2024
1 parent 5cefaa9 commit 28b16a8
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
10 changes: 5 additions & 5 deletions visidata/path.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -152,15 +152,15 @@ def __enter__(self):

def __next__(self):
r = next(self.fp)
self.prog.addProgress(len(r))
self.prog.addProgress(r)
return r

def __iter__(self):
if not self.prog:
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):
Expand Down Expand Up @@ -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'):
Expand Down
10 changes: 8 additions & 2 deletions visidata/threads.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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):
Expand Down

0 comments on commit 28b16a8

Please sign in to comment.