Download src/animations/progress_bar.py from amkyawdev/burme-coder-max: direct link, hf CLI and curl.
- Browser
- Download file 4.46 kB
-
https://huggingface.co/datasets/amkyawdev/burme-coder-max/resolve/main/src/animations/progress_bar.py
- Command line
-
hf download hf://datasets/amkyawdev/burme-coder-max/src/animations/progress_bar.py
-
curl -L -o progress_bar.py https://huggingface.co/datasets/amkyawdev/burme-coder-max/resolve/main/src/animations/progress_bar.py
4.46 kB
| """Progress Bar - Show progress for long operations""" | |
| import sys | |
| import time | |
| from typing import Iterable, Optional, Union | |
| from dataclasses import dataclass | |
| from .config import DEFAULT_CONFIG, AnimationConfig | |
| class ProgressBarConfig: | |
| """Configuration for progress bar appearance""" | |
| width: int = 40 | |
| fill_char: str = "█" | |
| empty_char: str = "░" | |
| show_percentage: bool = True | |
| show_count: bool = True | |
| class ProgressBar: | |
| """Customizable progress bar for terminal""" | |
| def __init__( | |
| self, | |
| iterable: Optional[Iterable] = None, | |
| total: Optional[int] = None, | |
| description: str = "Progress", | |
| config: Optional[ProgressBarConfig] = None, | |
| animation_config: Optional[AnimationConfig] = None, | |
| ): | |
| self.iterable = iterable | |
| self.total = total if total is not None else len(iterable) if iterable else 100 | |
| self.description = description | |
| self.progress_config = config or ProgressBarConfig() | |
| self.animation_config = animation_config or DEFAULT_CONFIG | |
| self.current = 0 | |
| self.start_time = time.time() | |
| def __iter__(self): | |
| """Make ProgressBar iterable""" | |
| for item in self.iterable or range(self.total): | |
| yield item | |
| self.update(self.current + 1) | |
| def update(self, current: int): | |
| """Update progress bar to new value""" | |
| self.current = min(current, self.total) | |
| self._render() | |
| def _render(self): | |
| """Render the progress bar""" | |
| percentage = self.current / self.total if self.total > 0 else 0 | |
| filled_len = int(self.progress_config.width * percentage) | |
| empty_len = self.progress_config.width - filled_len | |
| bar = ( | |
| self.progress_config.fill_char * filled_len | |
| + self.progress_config.empty_char * empty_len | |
| ) | |
| if self.progress_config.show_percentage: | |
| pct_str = f" {percentage * 100:.1f}%" | |
| else: | |
| pct_str = "" | |
| if self.progress_config.show_count: | |
| count_str = f" [{self.current}/{self.total}]" | |
| else: | |
| count_str = "" | |
| elapsed = time.time() - self.start_time | |
| if self.current > 0: | |
| rate = self.current / elapsed | |
| eta = (self.total - self.current) / rate if rate > 0 else 0 | |
| rate_str = f" ETA: {eta:.1f}s" | |
| else: | |
| rate_str = "" | |
| line = f"\r{self.description}: |{bar}|{pct_str}{count_str}{rate_str}" | |
| sys.stdout.write(line) | |
| sys.stdout.flush() | |
| if self.current >= self.total: | |
| sys.stdout.write("\n") | |
| def set_description(self, description: str): | |
| """Update the description""" | |
| self.description = description | |
| def percent(self) -> float: | |
| """Get current percentage""" | |
| return self.current / self.total if self.total > 0 else 0 | |
| class DownloadProgressBar(ProgressBar): | |
| """Progress bar designed for file downloads""" | |
| def __init__(self, total_bytes: int, filename: str = "file"): | |
| super().__init__( | |
| total=total_bytes, | |
| description=f"Downloading {filename}", | |
| ) | |
| self.total_bytes = total_bytes | |
| def update_bytes(self, current_bytes: int): | |
| """Update with byte count""" | |
| self.update(current_bytes) | |
| def _render(self): | |
| """Custom render for byte-based progress""" | |
| percentage = self.current / self.total if self.total > 0 else 0 | |
| filled_len = int(self.progress_config.width * percentage) | |
| bar = self.progress_config.fill_char * filled_len | |
| current_mb = self.current / (1024 * 1024) | |
| total_mb = self.total / (1024 * 1024) | |
| line = f"\r{self.description}: {current_mb:.1f}/{total_mb:.1f} MB |{bar}| {percentage * 100:.1f}%" | |
| sys.stdout.write(line) | |
| sys.stdout.flush() | |
| class InfiniteProgressBar: | |
| """Infinite looping progress bar""" | |
| CHARS = ["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"] | |
| PROGRESS = " █▓▒░" | |
| def __init__(self, description: str = "Working"): | |
| self.description = description | |
| self.frame = 0 | |
| def spin(self): | |
| """Generate next frame""" | |
| char = self.CHARS[self.frame % len(self.CHARS)] | |
| self.frame += 1 | |
| return f"\r{char} {self.description}..." | |
| def reset(self): | |
| """Reset frame counter""" | |
| self.frame = 0 | |