Skip to content

Commit

Permalink
[path-] fix undercounted progress for multibyte chars
Browse files Browse the repository at this point in the history
  • Loading branch information
midichef committed Feb 19, 2024
1 parent be295d8 commit 2a45fd5
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions visidata/path.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def peek(self, size=-1):


class FileProgress:
'Open file in binary mode and track read() progress.'
'Open file as text or binary, then track read() progress.'
def __init__(self, path, fp, mode='r', **kwargs):
self.path = path
self.fp = fp
Expand Down Expand Up @@ -130,13 +130,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(len(r.encode(self.encoding)) if self.encoding else len(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(len(r.encode(self.encoding)) if self.encoding else len(r))
return r

def __getattr__(self, k):
Expand All @@ -148,15 +148,15 @@ def __enter__(self):

def __next__(self):
r = next(self.fp)
self.prog.addProgress(len(r))
self.prog.addProgress(len(r.encode(self.encoding)) if self.encoding else len(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(len(line.encode(self.encoding)) if self.encoding else len(line))
yield line

def __exit__(self, type, value, tb):
Expand Down Expand Up @@ -326,9 +326,10 @@ def _open(self, *args, **kwargs):

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))
enc = vd.options.encoding
with self.open(encoding=enc) as fd:
for line in fd:
prog.addProgress(len(line.encode(enc)))
yield line.rstrip('\n')

def open_bytes(self, mode='rb'):
Expand Down

0 comments on commit 2a45fd5

Please sign in to comment.