51 lines
1.5 KiB
Python
51 lines
1.5 KiB
Python
|
# utils/progress_bar.py
|
||
|
"""
|
||
|
Progress bar implementation
|
||
|
"""
|
||
|
import tkinter as tk
|
||
|
from tkinter import ttk
|
||
|
from typing import Optional, Callable
|
||
|
import sys
|
||
|
from queue import Queue
|
||
|
import threading
|
||
|
|
||
|
class ProgressBar:
|
||
|
def __init__(self, total: int, prefix: str = "", suffix: str = "", max_points: int = 30):
|
||
|
self.total = total
|
||
|
self.prefix = prefix
|
||
|
self.suffix = suffix
|
||
|
self.max_points = max_points
|
||
|
self.current = 0
|
||
|
self.last_points = 0
|
||
|
self.output_callback: Optional[Callable] = None
|
||
|
|
||
|
def set_output_callback(self, callback: Callable[[str], None]):
|
||
|
"""Set callback function for output"""
|
||
|
self.output_callback = callback
|
||
|
|
||
|
def update(self, current: int):
|
||
|
self.current = current
|
||
|
points = min(int((current / self.total) * self.max_points), self.max_points)
|
||
|
|
||
|
if points > self.last_points:
|
||
|
new_points = points - self.last_points
|
||
|
self._write_output("." * new_points)
|
||
|
self.last_points = points
|
||
|
|
||
|
def increment(self):
|
||
|
self.update(self.current + 1)
|
||
|
|
||
|
def finish(self):
|
||
|
remaining_points = self.max_points - self.last_points
|
||
|
if remaining_points > 0:
|
||
|
self._write_output("." * remaining_points)
|
||
|
self._write_output(f"] {self.suffix}\n")
|
||
|
|
||
|
def start(self):
|
||
|
self._write_output(f"\r{self.prefix} [")
|
||
|
|
||
|
def _write_output(self, text: str):
|
||
|
if self.output_callback:
|
||
|
self.output_callback(text)
|
||
|
else:
|
||
|
print(text, end="", flush=True)
|