Skip to content

Commit 0819c2f

Browse files
committed
use new RGBColor and RGBGradient things
1 parent 84f3bc5 commit 0819c2f

1 file changed

Lines changed: 20 additions & 24 deletions

File tree

laser_prynter/pbar.py

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@
66
import sys
77
import time
88
from random import randint
9-
from typing import Iterator, NamedTuple
9+
from typing import Iterator
1010

11-
from laser_prynter.colour.gradient import interp_xyz
11+
from laser_prynter.colour.c import RGBColour
12+
from laser_prynter.colour.gradient import RGBGradient
1213

1314

1415
def indexes(t: int, w: int) -> Iterator[int]:
@@ -26,17 +27,11 @@ def _print_to_terminal(s: str) -> None:
2627
sys.stdout.flush()
2728

2829

29-
class RGB(NamedTuple):
30-
r: int
31-
g: int
32-
b: int
33-
34-
35-
DEFAULT_C1, DEFAULT_C2 = RGB(240, 50, 0), RGB(10, 220, 0)
30+
DEFAULT_C1, DEFAULT_C2 = RGBColour(240, 50, 0), RGBColour(10, 220, 0)
3631

3732

3833
class PBar:
39-
def __init__(self, total: int, c1: RGB = DEFAULT_C1, c2: RGB = DEFAULT_C2):
34+
def __init__(self, total: int, c1: RGBColour = DEFAULT_C1, c2: RGBColour = DEFAULT_C2):
4035
self.t = total
4136
self.w = shutil.get_terminal_size().columns
4237
self.h = shutil.get_terminal_size().lines
@@ -45,36 +40,36 @@ def __init__(self, total: int, c1: RGB = DEFAULT_C1, c2: RGB = DEFAULT_C2):
4540
self.progress = 0 # Track logical progress out of total
4641
self.start_time = time.time()
4742
if self.t > self.w:
48-
self.g = list(interp_xyz(c1, c2, self.t + 1))
43+
self.g = RGBGradient(start=c1, end=c2, steps=self.t + 1)
4944
else:
50-
self.g = list(interp_xyz(c1, c2, self.w + 1))
45+
self.g = RGBGradient(start=c1, end=c2, steps=self.w + 1)
5146

5247
self._iter_pbar = iter(self._pbar())
5348

5449
@staticmethod
55-
def randgrad() -> tuple[RGB, RGB]:
56-
rgb1 = RGB(randint(0, 255), randint(0, 255), randint(0, 255))
57-
rgb2 = RGB(
58-
(rgb1.r + (255 // 2)) % 255, (rgb1.g + (255 // 2)) % 255, (rgb1.b + (255 // 2)) % 255
50+
def randgrad() -> tuple[RGBColour, RGBColour]:
51+
'Generate a random gradient colour pair.'
52+
return (
53+
RGBColour(randint(0, 255), randint(0, 255), randint(0, 255)),
54+
RGBColour(randint(0, 255), randint(0, 255), randint(0, 255)),
5955
)
60-
return (rgb1, rgb2)
6156

62-
def _pbar(self) -> Iterator[tuple[tuple[int, RGB]]]:
57+
def _pbar(self) -> Iterator[tuple[tuple[int, RGBColour]]]:
6358
for x in range(self.w + 1): # TODO: i'm so dumb, why do I need a +1 here?
6459
if self.t > self.w:
6560
tpos = int((x / self.w) * self.t)
66-
color = self.g[tpos]
61+
color = self.g.sequence[tpos]
6762
else:
68-
color = self.g[x]
63+
color = self.g.sequence[x]
6964

70-
yield ((x, RGB(*map(int, color))),)
65+
yield ((x, RGBColour(*map(int, color))),)
7166

7267
def __iter__(self) -> PBar:
7368
return self
7469

7570
@staticmethod
76-
def _true_colour(rgb: RGB) -> str:
77-
return f'\x1b[48;2;{rgb.r};{rgb.g};{rgb.b}m'
71+
def _true_colour(rgbColour: RGBColour) -> str:
72+
return f'\x1b[48;2;{rgbColour.r};{rgbColour.g};{rgbColour.b}m'
7873

7974
@staticmethod
8075
def _format_time(seconds: float) -> str:
@@ -131,7 +126,7 @@ def _initial_bar(self) -> None:
131126
)
132127

133128
def __next__(self) -> None:
134-
s = [f'{self._true_colour(rgb)} ' for _, rgb in next(self._iter_pbar)]
129+
s = [f'{self._true_colour(rgbColour)} ' for _, rgbColour in next(self._iter_pbar)]
135130
self._print_bar_chars(''.join(s))
136131

137132
self.curr += len(s)
@@ -189,3 +184,4 @@ def __exit__(self, _exc_type: type, _exc_val: BaseException, _exc_tb: type) -> N
189184
time.sleep(randint(int(0.01 * 100), int(0.1 * 100)) / 100)
190185
print(f'-> {i}')
191186
pbar.update(1)
187+
pbar.update(1)

0 commit comments

Comments
 (0)