Skip to content

Commit

Permalink
Update: rich file read styling
Browse files Browse the repository at this point in the history
  • Loading branch information
StefanHeng committed May 30, 2024
1 parent 1a4ee49 commit 2f49ce1
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from setuptools import setup, find_packages

VERSION = '0.42.6'
VERSION = '0.42.7'
DESCRIPTION = 'Machine Learning project startup utilities'
LONG_DESCRIPTION = 'My commonly used utilities for machine learning projects'

Expand All @@ -13,7 +13,7 @@
description=DESCRIPTION,
long_description=LONG_DESCRIPTION,
url='https://github.com/StefanHeng/stef-util',
download_url='https://github.com/StefanHeng/stef-util/archive/refs/tags/v0.42.6.tar.gz',
download_url='https://github.com/StefanHeng/stef-util/archive/refs/tags/v0.42.7.tar.gz',
packages=find_packages(),
include_package_data=True,
install_requires=[
Expand Down
10 changes: 6 additions & 4 deletions stefutil/prettier/prettier_progress.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from tqdm.std import TqdmWarning
from tqdm.utils import FormatReplace, disp_len, disp_trim

from stefutil.os import rel_path
from stefutil.prettier.prettier_debug import s, rc


Expand All @@ -25,6 +26,7 @@ def rich_status(desc: str = None, spinner: str = 'arrow3'):

_I = typing.TypeVar("_I", TextIO, BinaryIO)


# copied over from `rich.progress` for no public access
class _ReadContext(ContextManager[_I], Generic[_I]):
"""A utility class to handle a context for both a reader and a progress."""
Expand All @@ -47,12 +49,12 @@ def __exit__(
self.reader.__exit__(exc_type, exc_val, exc_tb)


# override for our custom progress bar styling
# modified from `rich.progress.open` for our custom progress bar styling
def rich_open(
file: Union[str, "PathLike[str]", bytes],
mode: Union[Literal["rb"], Literal["rt"], Literal["r"]] = "r"
) -> Union[ContextManager[BinaryIO], ContextManager[TextIO]]:
desc = f'Reading file at {s.i(file, backend="rich-markup")}'
desc = f'Reading file {s.i(rel_path(file), backend="rich-markup")}'
progress = rich_progress(return_progress=True, desc=desc)

reader = progress.open(
Expand Down Expand Up @@ -643,7 +645,7 @@ def check_rich_pbar_field():
# sic(ch)
update(dur=t_ms, char=ch)
time.sleep(t_ms / 1000)
check_rich_pbar_field()
# check_rich_pbar_field()

def check_rich_backend_colors():
txt = 'hello'
Expand Down Expand Up @@ -678,7 +680,7 @@ def check_tqdm_color():

def check_rich_open():
import rich.progress
path = '../test-both-handler.log.ansi'
path = '../../stefutil/../stefutil/test-both-handler.log.ansi'
# with rich.progress.open(path, 'r') as f:
with rich_open(path) as f:
txt = f.read()
Expand Down

0 comments on commit 2f49ce1

Please sign in to comment.