File size: 4,458 Bytes
a7d7463
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
"""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


@dataclass
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

    @property
    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