ProgBar2077 0.0.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- progbar2077-0.0.0/LICENSE +7 -0
- progbar2077-0.0.0/MANIFEST.in +1 -0
- progbar2077-0.0.0/PKG-INFO +46 -0
- progbar2077-0.0.0/ProgBar2077.egg-info/PKG-INFO +46 -0
- progbar2077-0.0.0/ProgBar2077.egg-info/SOURCES.txt +12 -0
- progbar2077-0.0.0/ProgBar2077.egg-info/dependency_links.txt +1 -0
- progbar2077-0.0.0/ProgBar2077.egg-info/requires.txt +1 -0
- progbar2077-0.0.0/ProgBar2077.egg-info/top_level.txt +1 -0
- progbar2077-0.0.0/README.md +32 -0
- progbar2077-0.0.0/progbar2077_src/__init__.py +1 -0
- progbar2077-0.0.0/progbar2077_src/main.py +591 -0
- progbar2077-0.0.0/pyproject.toml +20 -0
- progbar2077-0.0.0/setup.cfg +4 -0
- progbar2077-0.0.0/setup.py +10 -0
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
Copyright 2026 Rui Luo
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
4
|
+
|
|
5
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
6
|
+
|
|
7
|
+
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
graft pipcpptemp_src # distribute all files in this folder
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: ProgBar2077
|
|
3
|
+
Version: 0.0.0
|
|
4
|
+
Summary: 2077-style progress bar.
|
|
5
|
+
Author-email: Rui Luo <Rui.1002@proton.me>
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Repository, https://github.com/rui-luo1002/ProgBar2077
|
|
8
|
+
Project-URL: Examples, https://github.com/rui-luo1002/ProgBar2077/tree/main/example
|
|
9
|
+
Keywords: C-API,template
|
|
10
|
+
Description-Content-Type: text/markdown
|
|
11
|
+
License-File: LICENSE
|
|
12
|
+
Requires-Dist: numpy
|
|
13
|
+
Dynamic: license-file
|
|
14
|
+
|
|
15
|
+
# 2077-style Progress Bar (ProgBar2077)
|
|
16
|
+
*Note: Codex is used during the development of this package.*
|
|
17
|
+
|
|
18
|
+
This is a Python library helps to show a 2077-style progress bar, which looks like:
|
|
19
|
+
```bash
|
|
20
|
+
|
|
21
|
+
- ┌━━━━━━━━━━━━━━━━━━━━━━─────────────────────────────────────────────┐
|
|
22
|
+
/!\ │ Processing │≡≡≡ │
|
|
23
|
+
‾‾‾ └───────────────────────────────────────────────────────────────────┘
|
|
24
|
+
CURRENT PROGRESS 32% RUNTIME: 0.853 s
|
|
25
|
+
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
## How to Use:
|
|
29
|
+
Import packages:
|
|
30
|
+
```python
|
|
31
|
+
from progbar2077 import ProgBar
|
|
32
|
+
from time import time, sleep
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
Create a progress bar context manager (so that the progress bar know the status of the code within, such as exceptions):
|
|
36
|
+
```python
|
|
37
|
+
with ProgBar("Reconstructing Images") as bar:
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
Continuously update progress bar:
|
|
41
|
+
```python
|
|
42
|
+
t = time()
|
|
43
|
+
for i in range(1, 256):
|
|
44
|
+
sleep(0.01) # execution
|
|
45
|
+
bar.update(i/255, f"RUNTIME: {time()-t:.3f} s")
|
|
46
|
+
```
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: ProgBar2077
|
|
3
|
+
Version: 0.0.0
|
|
4
|
+
Summary: 2077-style progress bar.
|
|
5
|
+
Author-email: Rui Luo <Rui.1002@proton.me>
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Repository, https://github.com/rui-luo1002/ProgBar2077
|
|
8
|
+
Project-URL: Examples, https://github.com/rui-luo1002/ProgBar2077/tree/main/example
|
|
9
|
+
Keywords: C-API,template
|
|
10
|
+
Description-Content-Type: text/markdown
|
|
11
|
+
License-File: LICENSE
|
|
12
|
+
Requires-Dist: numpy
|
|
13
|
+
Dynamic: license-file
|
|
14
|
+
|
|
15
|
+
# 2077-style Progress Bar (ProgBar2077)
|
|
16
|
+
*Note: Codex is used during the development of this package.*
|
|
17
|
+
|
|
18
|
+
This is a Python library helps to show a 2077-style progress bar, which looks like:
|
|
19
|
+
```bash
|
|
20
|
+
|
|
21
|
+
- ┌━━━━━━━━━━━━━━━━━━━━━━─────────────────────────────────────────────┐
|
|
22
|
+
/!\ │ Processing │≡≡≡ │
|
|
23
|
+
‾‾‾ └───────────────────────────────────────────────────────────────────┘
|
|
24
|
+
CURRENT PROGRESS 32% RUNTIME: 0.853 s
|
|
25
|
+
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
## How to Use:
|
|
29
|
+
Import packages:
|
|
30
|
+
```python
|
|
31
|
+
from progbar2077 import ProgBar
|
|
32
|
+
from time import time, sleep
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
Create a progress bar context manager (so that the progress bar know the status of the code within, such as exceptions):
|
|
36
|
+
```python
|
|
37
|
+
with ProgBar("Reconstructing Images") as bar:
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
Continuously update progress bar:
|
|
41
|
+
```python
|
|
42
|
+
t = time()
|
|
43
|
+
for i in range(1, 256):
|
|
44
|
+
sleep(0.01) # execution
|
|
45
|
+
bar.update(i/255, f"RUNTIME: {time()-t:.3f} s")
|
|
46
|
+
```
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
LICENSE
|
|
2
|
+
MANIFEST.in
|
|
3
|
+
README.md
|
|
4
|
+
pyproject.toml
|
|
5
|
+
setup.py
|
|
6
|
+
ProgBar2077.egg-info/PKG-INFO
|
|
7
|
+
ProgBar2077.egg-info/SOURCES.txt
|
|
8
|
+
ProgBar2077.egg-info/dependency_links.txt
|
|
9
|
+
ProgBar2077.egg-info/requires.txt
|
|
10
|
+
ProgBar2077.egg-info/top_level.txt
|
|
11
|
+
progbar2077_src/__init__.py
|
|
12
|
+
progbar2077_src/main.py
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
numpy
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
progbar2077
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
# 2077-style Progress Bar (ProgBar2077)
|
|
2
|
+
*Note: Codex is used during the development of this package.*
|
|
3
|
+
|
|
4
|
+
This is a Python library helps to show a 2077-style progress bar, which looks like:
|
|
5
|
+
```bash
|
|
6
|
+
|
|
7
|
+
- ┌━━━━━━━━━━━━━━━━━━━━━━─────────────────────────────────────────────┐
|
|
8
|
+
/!\ │ Processing │≡≡≡ │
|
|
9
|
+
‾‾‾ └───────────────────────────────────────────────────────────────────┘
|
|
10
|
+
CURRENT PROGRESS 32% RUNTIME: 0.853 s
|
|
11
|
+
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
## How to Use:
|
|
15
|
+
Import packages:
|
|
16
|
+
```python
|
|
17
|
+
from progbar2077 import ProgBar
|
|
18
|
+
from time import time, sleep
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
Create a progress bar context manager (so that the progress bar know the status of the code within, such as exceptions):
|
|
22
|
+
```python
|
|
23
|
+
with ProgBar("Reconstructing Images") as bar:
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
Continuously update progress bar:
|
|
27
|
+
```python
|
|
28
|
+
t = time()
|
|
29
|
+
for i in range(1, 256):
|
|
30
|
+
sleep(0.01) # execution
|
|
31
|
+
bar.update(i/255, f"RUNTIME: {time()-t:.3f} s")
|
|
32
|
+
```
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
from .main import ProgBar
|
|
@@ -0,0 +1,591 @@
|
|
|
1
|
+
"""A dependency-free terminal progress bar for simulated uploads.
|
|
2
|
+
|
|
3
|
+
Use ProgBar as a context manager for an initial frame and cleanup on early
|
|
4
|
+
exit. Call it with the absolute number of units uploaded and an optional
|
|
5
|
+
footer string. By default, output goes to stderr. Redirected output receives
|
|
6
|
+
one status line with the configured padding.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
from __future__ import annotations
|
|
10
|
+
|
|
11
|
+
import os
|
|
12
|
+
import re
|
|
13
|
+
import shutil
|
|
14
|
+
import sys
|
|
15
|
+
import unicodedata
|
|
16
|
+
from fractions import Fraction
|
|
17
|
+
from operator import index
|
|
18
|
+
from time import monotonic, sleep
|
|
19
|
+
from types import TracebackType
|
|
20
|
+
from typing import (
|
|
21
|
+
Literal,
|
|
22
|
+
SupportsIndex,
|
|
23
|
+
TextIO,
|
|
24
|
+
)
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
# CSS Color Level 3 extended color keywords.
|
|
28
|
+
# https://www.w3.org/TR/css-color-3/#svg-color
|
|
29
|
+
_CSS_COLORS: dict[str, str] = {
|
|
30
|
+
"aliceblue": "#f0f8ff",
|
|
31
|
+
"antiquewhite": "#faebd7",
|
|
32
|
+
"aqua": "#00ffff",
|
|
33
|
+
"aquamarine": "#7fffd4",
|
|
34
|
+
"azure": "#f0ffff",
|
|
35
|
+
"beige": "#f5f5dc",
|
|
36
|
+
"bisque": "#ffe4c4",
|
|
37
|
+
"black": "#000000",
|
|
38
|
+
"blanchedalmond": "#ffebcd",
|
|
39
|
+
"blue": "#0000ff",
|
|
40
|
+
"blueviolet": "#8a2be2",
|
|
41
|
+
"brown": "#a52a2a",
|
|
42
|
+
"burlywood": "#deb887",
|
|
43
|
+
"cadetblue": "#5f9ea0",
|
|
44
|
+
"chartreuse": "#7fff00",
|
|
45
|
+
"chocolate": "#d2691e",
|
|
46
|
+
"coral": "#ff7f50",
|
|
47
|
+
"cornflowerblue": "#6495ed",
|
|
48
|
+
"cornsilk": "#fff8dc",
|
|
49
|
+
"crimson": "#dc143c",
|
|
50
|
+
"cyan": "#00ffff",
|
|
51
|
+
"darkblue": "#00008b",
|
|
52
|
+
"darkcyan": "#008b8b",
|
|
53
|
+
"darkgoldenrod": "#b8860b",
|
|
54
|
+
"darkgray": "#a9a9a9",
|
|
55
|
+
"darkgreen": "#006400",
|
|
56
|
+
"darkgrey": "#a9a9a9",
|
|
57
|
+
"darkkhaki": "#bdb76b",
|
|
58
|
+
"darkmagenta": "#8b008b",
|
|
59
|
+
"darkolivegreen": "#556b2f",
|
|
60
|
+
"darkorange": "#ff8c00",
|
|
61
|
+
"darkorchid": "#9932cc",
|
|
62
|
+
"darkred": "#8b0000",
|
|
63
|
+
"darksalmon": "#e9967a",
|
|
64
|
+
"darkseagreen": "#8fbc8f",
|
|
65
|
+
"darkslateblue": "#483d8b",
|
|
66
|
+
"darkslategray": "#2f4f4f",
|
|
67
|
+
"darkslategrey": "#2f4f4f",
|
|
68
|
+
"darkturquoise": "#00ced1",
|
|
69
|
+
"darkviolet": "#9400d3",
|
|
70
|
+
"deeppink": "#ff1493",
|
|
71
|
+
"deepskyblue": "#00bfff",
|
|
72
|
+
"dimgray": "#696969",
|
|
73
|
+
"dimgrey": "#696969",
|
|
74
|
+
"dodgerblue": "#1e90ff",
|
|
75
|
+
"firebrick": "#b22222",
|
|
76
|
+
"floralwhite": "#fffaf0",
|
|
77
|
+
"forestgreen": "#228b22",
|
|
78
|
+
"fuchsia": "#ff00ff",
|
|
79
|
+
"gainsboro": "#dcdcdc",
|
|
80
|
+
"ghostwhite": "#f8f8ff",
|
|
81
|
+
"gold": "#ffd700",
|
|
82
|
+
"goldenrod": "#daa520",
|
|
83
|
+
"gray": "#808080",
|
|
84
|
+
"green": "#008000",
|
|
85
|
+
"greenyellow": "#adff2f",
|
|
86
|
+
"grey": "#808080",
|
|
87
|
+
"honeydew": "#f0fff0",
|
|
88
|
+
"hotpink": "#ff69b4",
|
|
89
|
+
"indianred": "#cd5c5c",
|
|
90
|
+
"indigo": "#4b0082",
|
|
91
|
+
"ivory": "#fffff0",
|
|
92
|
+
"khaki": "#f0e68c",
|
|
93
|
+
"lavender": "#e6e6fa",
|
|
94
|
+
"lavenderblush": "#fff0f5",
|
|
95
|
+
"lawngreen": "#7cfc00",
|
|
96
|
+
"lemonchiffon": "#fffacd",
|
|
97
|
+
"lightblue": "#add8e6",
|
|
98
|
+
"lightcoral": "#f08080",
|
|
99
|
+
"lightcyan": "#e0ffff",
|
|
100
|
+
"lightgoldenrodyellow": "#fafad2",
|
|
101
|
+
"lightgray": "#d3d3d3",
|
|
102
|
+
"lightgreen": "#90ee90",
|
|
103
|
+
"lightgrey": "#d3d3d3",
|
|
104
|
+
"lightpink": "#ffb6c1",
|
|
105
|
+
"lightsalmon": "#ffa07a",
|
|
106
|
+
"lightseagreen": "#20b2aa",
|
|
107
|
+
"lightskyblue": "#87cefa",
|
|
108
|
+
"lightslategray": "#778899",
|
|
109
|
+
"lightslategrey": "#778899",
|
|
110
|
+
"lightsteelblue": "#b0c4de",
|
|
111
|
+
"lightyellow": "#ffffe0",
|
|
112
|
+
"lime": "#00ff00",
|
|
113
|
+
"limegreen": "#32cd32",
|
|
114
|
+
"linen": "#faf0e6",
|
|
115
|
+
"magenta": "#ff00ff",
|
|
116
|
+
"maroon": "#800000",
|
|
117
|
+
"mediumaquamarine": "#66cdaa",
|
|
118
|
+
"mediumblue": "#0000cd",
|
|
119
|
+
"mediumorchid": "#ba55d3",
|
|
120
|
+
"mediumpurple": "#9370db",
|
|
121
|
+
"mediumseagreen": "#3cb371",
|
|
122
|
+
"mediumslateblue": "#7b68ee",
|
|
123
|
+
"mediumspringgreen": "#00fa9a",
|
|
124
|
+
"mediumturquoise": "#48d1cc",
|
|
125
|
+
"mediumvioletred": "#c71585",
|
|
126
|
+
"midnightblue": "#191970",
|
|
127
|
+
"mintcream": "#f5fffa",
|
|
128
|
+
"mistyrose": "#ffe4e1",
|
|
129
|
+
"moccasin": "#ffe4b5",
|
|
130
|
+
"navajowhite": "#ffdead",
|
|
131
|
+
"navy": "#000080",
|
|
132
|
+
"oldlace": "#fdf5e6",
|
|
133
|
+
"olive": "#808000",
|
|
134
|
+
"olivedrab": "#6b8e23",
|
|
135
|
+
"orange": "#ffa500",
|
|
136
|
+
"orangered": "#ff4500",
|
|
137
|
+
"orchid": "#da70d6",
|
|
138
|
+
"palegoldenrod": "#eee8aa",
|
|
139
|
+
"palegreen": "#98fb98",
|
|
140
|
+
"paleturquoise": "#afeeee",
|
|
141
|
+
"palevioletred": "#db7093",
|
|
142
|
+
"papayawhip": "#ffefd5",
|
|
143
|
+
"peachpuff": "#ffdab9",
|
|
144
|
+
"peru": "#cd853f",
|
|
145
|
+
"pink": "#ffc0cb",
|
|
146
|
+
"plum": "#dda0dd",
|
|
147
|
+
"powderblue": "#b0e0e6",
|
|
148
|
+
"purple": "#800080",
|
|
149
|
+
"red": "#ff0000",
|
|
150
|
+
"rosybrown": "#bc8f8f",
|
|
151
|
+
"royalblue": "#4169e1",
|
|
152
|
+
"saddlebrown": "#8b4513",
|
|
153
|
+
"salmon": "#fa8072",
|
|
154
|
+
"sandybrown": "#f4a460",
|
|
155
|
+
"seagreen": "#2e8b57",
|
|
156
|
+
"seashell": "#fff5ee",
|
|
157
|
+
"sienna": "#a0522d",
|
|
158
|
+
"silver": "#c0c0c0",
|
|
159
|
+
"skyblue": "#87ceeb",
|
|
160
|
+
"slateblue": "#6a5acd",
|
|
161
|
+
"slategray": "#708090",
|
|
162
|
+
"slategrey": "#708090",
|
|
163
|
+
"snow": "#fffafa",
|
|
164
|
+
"springgreen": "#00ff7f",
|
|
165
|
+
"steelblue": "#4682b4",
|
|
166
|
+
"tan": "#d2b48c",
|
|
167
|
+
"teal": "#008080",
|
|
168
|
+
"thistle": "#d8bfd8",
|
|
169
|
+
"tomato": "#ff6347",
|
|
170
|
+
"turquoise": "#40e0d0",
|
|
171
|
+
"violet": "#ee82ee",
|
|
172
|
+
"wheat": "#f5deb3",
|
|
173
|
+
"white": "#ffffff",
|
|
174
|
+
"whitesmoke": "#f5f5f5",
|
|
175
|
+
"yellow": "#ffff00",
|
|
176
|
+
"yellowgreen": "#9acd32",
|
|
177
|
+
}
|
|
178
|
+
_MIN_PANEL_COLUMNS = 24
|
|
179
|
+
_MAX_PANEL_COLUMNS = 69
|
|
180
|
+
|
|
181
|
+
|
|
182
|
+
def _accent_code(color: str) -> str:
|
|
183
|
+
"""Convert a CSS color name or hex value to an ANSI foreground code.
|
|
184
|
+
|
|
185
|
+
Args:
|
|
186
|
+
color: A CSS3 color name or a three- or six-digit hex color.
|
|
187
|
+
|
|
188
|
+
Returns:
|
|
189
|
+
ANSI foreground parameters without the escape sequence.
|
|
190
|
+
|
|
191
|
+
Raises:
|
|
192
|
+
ValueError: The color name or hex code is unsupported.
|
|
193
|
+
"""
|
|
194
|
+
value = color.strip().lower()
|
|
195
|
+
if not value.startswith("#"):
|
|
196
|
+
name = re.sub(r"[\s_-]+", "", value)
|
|
197
|
+
value = _CSS_COLORS.get(name, "")
|
|
198
|
+
if re.fullmatch(r"#[0-9a-f]{3}", value):
|
|
199
|
+
value = "#" + "".join(digit * 2 for digit in value[1:])
|
|
200
|
+
if not re.fullmatch(r"#[0-9a-f]{6}", value):
|
|
201
|
+
raise ValueError("color must be a CSS3 name, #RGB, or #RRGGBB")
|
|
202
|
+
red, green, blue = (int(value[start:start + 2], 16) for start in (1, 3, 5))
|
|
203
|
+
return f"38;2;{red};{green};{blue}"
|
|
204
|
+
|
|
205
|
+
|
|
206
|
+
def _single_line(text: str, encoding: str) -> str:
|
|
207
|
+
"""Replace control characters and fit text to the stream encoding."""
|
|
208
|
+
printable = "".join(char if char.isprintable() else " " for char in text)
|
|
209
|
+
return printable.encode(encoding, "replace").decode(encoding)
|
|
210
|
+
|
|
211
|
+
|
|
212
|
+
def _clip(text: str, columns: int | None = None) -> tuple[str, int]:
|
|
213
|
+
"""Measure text in terminal columns and optionally clip it.
|
|
214
|
+
|
|
215
|
+
Args:
|
|
216
|
+
text: The text to fit.
|
|
217
|
+
columns: The maximum display width, if any.
|
|
218
|
+
|
|
219
|
+
Returns:
|
|
220
|
+
The fitting prefix and its width in terminal columns.
|
|
221
|
+
"""
|
|
222
|
+
width = 0
|
|
223
|
+
for offset, char in enumerate(text):
|
|
224
|
+
char_width = 1
|
|
225
|
+
if unicodedata.category(char) in ("Mn", "Me"):
|
|
226
|
+
char_width = 0
|
|
227
|
+
elif unicodedata.east_asian_width(char) in ("W", "F"):
|
|
228
|
+
char_width = 2
|
|
229
|
+
if columns is not None and width + char_width > columns:
|
|
230
|
+
return text[:offset], width
|
|
231
|
+
width += char_width
|
|
232
|
+
return text, width
|
|
233
|
+
|
|
234
|
+
|
|
235
|
+
def _icon_rows(icon: str | list[str] | list[list[str]] | None, encoding: str) -> tuple[tuple[str, ...], int]:
|
|
236
|
+
"""Fit text artwork into the three rows beside the panel.
|
|
237
|
+
|
|
238
|
+
Args:
|
|
239
|
+
icon: A line of text, up to three rows of text, or None.
|
|
240
|
+
encoding: The encoding of the output stream.
|
|
241
|
+
|
|
242
|
+
Returns:
|
|
243
|
+
The padded icon rows and their display width. An empty icon has
|
|
244
|
+
no rows and a width of zero.
|
|
245
|
+
|
|
246
|
+
Raises:
|
|
247
|
+
ValueError: The icon has too many rows or contains nonprintable text.
|
|
248
|
+
"""
|
|
249
|
+
if icon is None:
|
|
250
|
+
return (), 0
|
|
251
|
+
if isinstance(icon, str):
|
|
252
|
+
rows = (icon,)
|
|
253
|
+
else:
|
|
254
|
+
matrix = icon
|
|
255
|
+
try:
|
|
256
|
+
rows = tuple("".join(row) for row in matrix)
|
|
257
|
+
except TypeError:
|
|
258
|
+
raise ValueError("icon rows must contain text") from None
|
|
259
|
+
if len(rows) > 3 or any(row and not row.isprintable() for row in rows):
|
|
260
|
+
raise ValueError("icon must contain at most three rows of printable text")
|
|
261
|
+
|
|
262
|
+
above = (3 - len(rows)) // 2
|
|
263
|
+
rows = ("",) * above + rows + ("",) * (3 - len(rows) - above)
|
|
264
|
+
encoded = [row.encode(encoding, "replace").decode(encoding) for row in rows]
|
|
265
|
+
widths = [_clip(row)[1] for row in encoded]
|
|
266
|
+
width = max(widths)
|
|
267
|
+
if not width:
|
|
268
|
+
return (), 0
|
|
269
|
+
return tuple(row + " " * (width - used) for row, used in zip(encoded, widths)), width
|
|
270
|
+
|
|
271
|
+
|
|
272
|
+
class ProgBar:
|
|
273
|
+
"""A terminal upload panel with a progress meter and elapsed time.
|
|
274
|
+
|
|
275
|
+
Timing starts on the first update and stops when the upload ends.
|
|
276
|
+
When the upload ends, the title briefly goes blank, then shows its result.
|
|
277
|
+
|
|
278
|
+
Attributes:
|
|
279
|
+
total: The number of units required to finish the upload.
|
|
280
|
+
completed: The number of units uploaded so far.
|
|
281
|
+
event_name: The event title displayed while the upload is active.
|
|
282
|
+
"""
|
|
283
|
+
|
|
284
|
+
def __init__(
|
|
285
|
+
self,
|
|
286
|
+
name: str = "Processing",
|
|
287
|
+
total: SupportsIndex | float = 1.0,
|
|
288
|
+
*,
|
|
289
|
+
color: str = "indianred",
|
|
290
|
+
icon: str | list[str] | list[list[str]] | None = [" - ", "/!\\", "‾‾‾"],
|
|
291
|
+
blank_lines_before: SupportsIndex = 4,
|
|
292
|
+
blank_lines_after: SupportsIndex = 4,
|
|
293
|
+
stream: TextIO | None = None,
|
|
294
|
+
) -> None:
|
|
295
|
+
"""Initialize an upload panel.
|
|
296
|
+
|
|
297
|
+
Args:
|
|
298
|
+
total: A positive, finite number of units.
|
|
299
|
+
event_name: The title displayed during the upload.
|
|
300
|
+
color: A CSS3 color name, #RGB, or #RRGGBB hex value.
|
|
301
|
+
icon: A string aligned with the event name, or a list of up to
|
|
302
|
+
three strings for multiline text.
|
|
303
|
+
stream: The text stream to write to. Defaults to stderr.
|
|
304
|
+
blank_lines_before: Blank rows to place above the display.
|
|
305
|
+
blank_lines_after: Blank rows to place below the display.
|
|
306
|
+
|
|
307
|
+
Raises:
|
|
308
|
+
TypeError: The total is not a float or integer-like object, or a
|
|
309
|
+
blank-line count is not integer-like.
|
|
310
|
+
ValueError: The total, event name, accent, icon, or padding is
|
|
311
|
+
invalid.
|
|
312
|
+
"""
|
|
313
|
+
self.total = total if isinstance(total, float) else index(total)
|
|
314
|
+
if not 0 < self.total < float("inf"):
|
|
315
|
+
raise ValueError("total must be positive and finite")
|
|
316
|
+
self._blank_lines_before = index(blank_lines_before)
|
|
317
|
+
self._blank_lines_after = index(blank_lines_after)
|
|
318
|
+
if min(self._blank_lines_before, self._blank_lines_after) < 0:
|
|
319
|
+
raise ValueError("blank line counts must be nonnegative")
|
|
320
|
+
self._accent = _accent_code(color)
|
|
321
|
+
self.completed: int | float = 0
|
|
322
|
+
self._stream = sys.stderr if stream is None else stream
|
|
323
|
+
encoding = getattr(self._stream, "encoding", None) or "utf-8"
|
|
324
|
+
self._encoding = encoding
|
|
325
|
+
self.event_name = _single_line(name, encoding).strip()
|
|
326
|
+
if not self.event_name:
|
|
327
|
+
raise ValueError("event_name must contain visible text")
|
|
328
|
+
self._interactive = self._stream.isatty() and os.environ.get("TERM") != "dumb"
|
|
329
|
+
self._color = self._interactive and "NO_COLOR" not in os.environ
|
|
330
|
+
try:
|
|
331
|
+
"┌─┐│└┘━≡".encode(encoding)
|
|
332
|
+
self._unicode = True
|
|
333
|
+
except UnicodeEncodeError:
|
|
334
|
+
self._unicode = False
|
|
335
|
+
self._icon, self._icon_width = _icon_rows(icon, encoding)
|
|
336
|
+
self._closed = False
|
|
337
|
+
self._rows = 0
|
|
338
|
+
self._started_at: float | None = None
|
|
339
|
+
self._finished_at: float | None = None
|
|
340
|
+
self._footer_text: str | None = None
|
|
341
|
+
|
|
342
|
+
def __enter__(self) -> ProgBar:
|
|
343
|
+
"""Draw the initial frame and return the progress bar."""
|
|
344
|
+
self.update(self.completed)
|
|
345
|
+
return self
|
|
346
|
+
|
|
347
|
+
def __exit__(
|
|
348
|
+
self,
|
|
349
|
+
exc_type: type[BaseException] | None,
|
|
350
|
+
exc_value: BaseException | None,
|
|
351
|
+
traceback: TracebackType | None,
|
|
352
|
+
) -> None:
|
|
353
|
+
"""Mark an unfinished upload as failed when the context exits.
|
|
354
|
+
|
|
355
|
+
Args:
|
|
356
|
+
exc_type: The type of the exception, if any.
|
|
357
|
+
exc_value: The exception instance, if any.
|
|
358
|
+
traceback: The traceback of the exception, if any.
|
|
359
|
+
"""
|
|
360
|
+
if not self._closed:
|
|
361
|
+
self._finish("FAILED")
|
|
362
|
+
|
|
363
|
+
def __call__(
|
|
364
|
+
self,
|
|
365
|
+
completed: SupportsIndex | float,
|
|
366
|
+
footer: str | None = None,
|
|
367
|
+
) -> None:
|
|
368
|
+
"""Set progress and optionally replace the runtime footer.
|
|
369
|
+
|
|
370
|
+
Args:
|
|
371
|
+
completed: The cumulative number of units uploaded.
|
|
372
|
+
footer: Footer text. Omit it to keep the current footer.
|
|
373
|
+
"""
|
|
374
|
+
self.update(completed, footer)
|
|
375
|
+
|
|
376
|
+
def update(
|
|
377
|
+
self,
|
|
378
|
+
completed: SupportsIndex | float,
|
|
379
|
+
footer: str | None = None,
|
|
380
|
+
) -> None:
|
|
381
|
+
"""Set the uploaded count, finishing automatically at the total.
|
|
382
|
+
|
|
383
|
+
Args:
|
|
384
|
+
completed: The cumulative number of units uploaded.
|
|
385
|
+
footer: Footer text.
|
|
386
|
+
Omit it to keep the current footer, which defaults to runtime.
|
|
387
|
+
|
|
388
|
+
Raises:
|
|
389
|
+
RuntimeError: The upload has already ended.
|
|
390
|
+
TypeError: The count is not a float or integer-like object, or
|
|
391
|
+
the footer is not a string.
|
|
392
|
+
ValueError: The count falls outside the current progress and
|
|
393
|
+
total.
|
|
394
|
+
"""
|
|
395
|
+
if self._closed:
|
|
396
|
+
raise RuntimeError("this upload has already ended")
|
|
397
|
+
count = completed if isinstance(completed, float) else index(completed)
|
|
398
|
+
if not self.completed <= count <= self.total:
|
|
399
|
+
raise ValueError("completed must stay between the current count and total")
|
|
400
|
+
if footer is not None:
|
|
401
|
+
if not isinstance(footer, str):
|
|
402
|
+
raise TypeError("footer must be a string")
|
|
403
|
+
self._footer_text = _single_line(footer, self._encoding)
|
|
404
|
+
if self._started_at is None:
|
|
405
|
+
self._started_at = monotonic()
|
|
406
|
+
self.completed = count
|
|
407
|
+
if count == self.total:
|
|
408
|
+
self._finish("COMPLETED")
|
|
409
|
+
elif self._interactive:
|
|
410
|
+
self._render()
|
|
411
|
+
|
|
412
|
+
def _finish(self, result: Literal["COMPLETED", "FAILED"]) -> None:
|
|
413
|
+
"""Blink the result title, hold it briefly, and close the display."""
|
|
414
|
+
self._finished_at = monotonic()
|
|
415
|
+
try:
|
|
416
|
+
if self._interactive:
|
|
417
|
+
self._render(result, hide_title=True)
|
|
418
|
+
sleep(0.25)
|
|
419
|
+
self._render(result)
|
|
420
|
+
sleep(0.5)
|
|
421
|
+
finally:
|
|
422
|
+
self._render(result, final=True)
|
|
423
|
+
self._closed = True
|
|
424
|
+
|
|
425
|
+
def _render(
|
|
426
|
+
self,
|
|
427
|
+
result: Literal["COMPLETED", "FAILED"] | None = None,
|
|
428
|
+
*,
|
|
429
|
+
final: bool = False,
|
|
430
|
+
hide_title: bool = False,
|
|
431
|
+
) -> None:
|
|
432
|
+
"""Draw the frame or write a plain-text result for a redirected stream.
|
|
433
|
+
|
|
434
|
+
Args:
|
|
435
|
+
result: The title to display when the upload ends.
|
|
436
|
+
final: Whether to end the frame and leave the cursor below it.
|
|
437
|
+
hide_title: Whether to blank the title for the final blink.
|
|
438
|
+
"""
|
|
439
|
+
if self.completed == self.total:
|
|
440
|
+
progress = Fraction(1)
|
|
441
|
+
percent = "100%"
|
|
442
|
+
else:
|
|
443
|
+
# Use the decimal spelling of floats so values like 0.29 display as 29%.
|
|
444
|
+
completed = Fraction(
|
|
445
|
+
str(self.completed) if isinstance(self.completed, float) else self.completed
|
|
446
|
+
)
|
|
447
|
+
total = Fraction(str(self.total) if isinstance(self.total, float) else self.total)
|
|
448
|
+
progress = completed / total
|
|
449
|
+
percent = f"{min(99, int(progress * 100))}%"
|
|
450
|
+
footer_text = self._footer_text
|
|
451
|
+
if footer_text is None:
|
|
452
|
+
elapsed = 0.0
|
|
453
|
+
if self._started_at is not None:
|
|
454
|
+
end = monotonic() if self._finished_at is None else self._finished_at
|
|
455
|
+
elapsed = end - self._started_at
|
|
456
|
+
footer_text = f"RUNTIME: {elapsed:.2f}s"
|
|
457
|
+
title = self.event_name if result is None else result
|
|
458
|
+
if not self._interactive:
|
|
459
|
+
self._stream.write(
|
|
460
|
+
"\n" * self._blank_lines_before
|
|
461
|
+
+ f"{title} {percent} {footer_text}"
|
|
462
|
+
+ "\n" * (1 + self._blank_lines_after)
|
|
463
|
+
)
|
|
464
|
+
self._stream.flush()
|
|
465
|
+
return
|
|
466
|
+
|
|
467
|
+
# Leave one column free so writing the frame never triggers a wrap.
|
|
468
|
+
size = self._terminal_size()
|
|
469
|
+
columns = max(1, size.columns - 1)
|
|
470
|
+
if columns < _MIN_PANEL_COLUMNS or size.lines < 6:
|
|
471
|
+
label_columns = columns - len(percent) - 1
|
|
472
|
+
if result is not None and label_columns < len(title) <= columns:
|
|
473
|
+
label, suffix = title, ""
|
|
474
|
+
else:
|
|
475
|
+
label, _ = _clip(title, max(0, label_columns))
|
|
476
|
+
suffix = f" {percent}" if label else percent[-columns:]
|
|
477
|
+
summary = label + suffix
|
|
478
|
+
_, summary_width = _clip(summary, columns)
|
|
479
|
+
margin = " " * ((size.columns - summary_width) // 2)
|
|
480
|
+
if hide_title:
|
|
481
|
+
summary = " " * len(label) + suffix
|
|
482
|
+
rows = [margin + self._paint(summary)]
|
|
483
|
+
if self._footer_text is not None and size.lines > 1:
|
|
484
|
+
footer_line, footer_width = _clip(footer_text, columns)
|
|
485
|
+
footer_margin = " " * ((size.columns - footer_width) // 2)
|
|
486
|
+
rows.append(footer_margin + self._paint(footer_line))
|
|
487
|
+
else:
|
|
488
|
+
rows = self._panel(
|
|
489
|
+
"" if hide_title else title, progress, percent, footer_text, columns
|
|
490
|
+
)
|
|
491
|
+
# Keep the whole panel visible even when requested padding exceeds the viewport.
|
|
492
|
+
trailing_rows = min(self._blank_lines_after, max(0, size.lines - len(rows)))
|
|
493
|
+
self._write_frame(rows, trailing_rows, final)
|
|
494
|
+
|
|
495
|
+
def _panel(
|
|
496
|
+
self,
|
|
497
|
+
title: str,
|
|
498
|
+
progress: Fraction,
|
|
499
|
+
percent: str,
|
|
500
|
+
footer_text: str,
|
|
501
|
+
usable_columns: int,
|
|
502
|
+
) -> list[str]:
|
|
503
|
+
"""Build rows for a centered panel and its footer.
|
|
504
|
+
|
|
505
|
+
Args:
|
|
506
|
+
title: The title to place inside the panel.
|
|
507
|
+
progress: The fraction of the total uploaded.
|
|
508
|
+
percent: The formatted progress percentage.
|
|
509
|
+
footer_text: The right-hand footer text.
|
|
510
|
+
usable_columns: The number of terminal columns available to draw.
|
|
511
|
+
|
|
512
|
+
Returns:
|
|
513
|
+
The styled panel and footer rows, including their left margin.
|
|
514
|
+
"""
|
|
515
|
+
top_left, top_right, bottom_left, bottom_right, side, rail, fill, meter = (
|
|
516
|
+
"┌┐└┘│─━≡" if self._unicode else "++++|-=="
|
|
517
|
+
)
|
|
518
|
+
icon = self._icon
|
|
519
|
+
gutter = self._icon_width + 1 if icon else 0
|
|
520
|
+
if usable_columns - gutter < _MIN_PANEL_COLUMNS:
|
|
521
|
+
icon = ()
|
|
522
|
+
gutter = 0
|
|
523
|
+
columns = min(usable_columns, _MAX_PANEL_COLUMNS + gutter)
|
|
524
|
+
margin = " " * ((usable_columns + 1 - columns) // 2)
|
|
525
|
+
width = columns - gutter - 2
|
|
526
|
+
filled = int(width * progress + Fraction(1, 2))
|
|
527
|
+
if self.completed < self.total:
|
|
528
|
+
filled = min(filled, width - 1)
|
|
529
|
+
top = top_left + fill * filled + rail * (width - filled) + top_right
|
|
530
|
+
bottom = bottom_left + rail * width + bottom_right
|
|
531
|
+
label_columns = width - 6
|
|
532
|
+
title, title_width = _clip(title, label_columns)
|
|
533
|
+
title += " " * (label_columns - title_width)
|
|
534
|
+
panel = [top, f"{side} {title}{side}{meter * 3} {side}", bottom]
|
|
535
|
+
if icon:
|
|
536
|
+
panel = [f"{icon_row} {panel_row}" for icon_row, panel_row in zip(icon, panel)]
|
|
537
|
+
|
|
538
|
+
indent = " " * (gutter + 2)
|
|
539
|
+
available = columns - len(indent)
|
|
540
|
+
progress_text = f"CURRENT PROGRESS {percent:>4}"
|
|
541
|
+
_, footer_width = _clip(footer_text)
|
|
542
|
+
gap = available - len(progress_text) - footer_width
|
|
543
|
+
if gap >= 2:
|
|
544
|
+
footer = [indent + progress_text + " " * gap + footer_text]
|
|
545
|
+
else:
|
|
546
|
+
if len(progress_text) > available:
|
|
547
|
+
progress_text = f"PROGRESS {percent}"
|
|
548
|
+
footer_text, footer_width = _clip(footer_text, columns)
|
|
549
|
+
footer_indent = " " * min(len(indent), columns - footer_width)
|
|
550
|
+
footer = [indent + progress_text, footer_indent + footer_text]
|
|
551
|
+
|
|
552
|
+
return [margin + self._paint(line) for line in panel + footer]
|
|
553
|
+
|
|
554
|
+
def _write_frame(self, rows: list[str], trailing_rows: int, final: bool) -> None:
|
|
555
|
+
"""Replace the live terminal frame and maintain its blank margins.
|
|
556
|
+
|
|
557
|
+
Args:
|
|
558
|
+
rows: The formatted content lines.
|
|
559
|
+
trailing_rows: The number of blank rows to draw within the frame.
|
|
560
|
+
final: Whether to leave the cursor below the completed display.
|
|
561
|
+
"""
|
|
562
|
+
rows = rows + [""] * trailing_rows
|
|
563
|
+
previous_rows = self._rows
|
|
564
|
+
prefix = ""
|
|
565
|
+
if previous_rows > 1:
|
|
566
|
+
prefix = f"\033[{previous_rows - 1}A"
|
|
567
|
+
elif not previous_rows:
|
|
568
|
+
prefix = "\n" * self._blank_lines_before
|
|
569
|
+
clear = ["\r" + row + "\033[K" for row in rows]
|
|
570
|
+
extra = max(0, previous_rows - len(rows))
|
|
571
|
+
clear.extend(["\r\033[K"] * extra)
|
|
572
|
+
frame = prefix + "\n".join(clear)
|
|
573
|
+
if extra:
|
|
574
|
+
frame += f"\033[{extra}A"
|
|
575
|
+
if final:
|
|
576
|
+
frame += "\n" * (1 + self._blank_lines_after - trailing_rows)
|
|
577
|
+
self._stream.write(frame)
|
|
578
|
+
self._stream.flush()
|
|
579
|
+
self._rows = len(rows)
|
|
580
|
+
|
|
581
|
+
def _terminal_size(self) -> os.terminal_size:
|
|
582
|
+
"""Return the output terminal size or the platform fallback."""
|
|
583
|
+
try:
|
|
584
|
+
size = os.get_terminal_size(self._stream.fileno())
|
|
585
|
+
except (AttributeError, OSError):
|
|
586
|
+
return shutil.get_terminal_size()
|
|
587
|
+
return size if size.columns and size.lines else shutil.get_terminal_size()
|
|
588
|
+
|
|
589
|
+
def _paint(self, text: str) -> str:
|
|
590
|
+
"""Return text in the selected ANSI accent when color is enabled."""
|
|
591
|
+
return f"\033[{self._accent}m{text}\033[0m" if self._color and text else text
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools", "numpy"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "ProgBar2077"
|
|
7
|
+
version = "0.0.0"
|
|
8
|
+
dependencies = ["numpy"]
|
|
9
|
+
|
|
10
|
+
description = "2077-style progress bar."
|
|
11
|
+
keywords = ["C-API", "template"]
|
|
12
|
+
authors = [{name = "Rui Luo", email = "Rui.1002@proton.me"}]
|
|
13
|
+
|
|
14
|
+
license = "MIT"
|
|
15
|
+
license-files = ["LICENSE"]
|
|
16
|
+
readme = "README.md"
|
|
17
|
+
|
|
18
|
+
[project.urls]
|
|
19
|
+
Repository = "https://github.com/rui-luo1002/ProgBar2077"
|
|
20
|
+
Examples = "https://github.com/rui-luo1002/ProgBar2077/tree/main/example"
|