-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathscreen.py
175 lines (137 loc) · 5.51 KB
/
screen.py
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import time
import sys
from typing import Tuple, Optional
from .rt.image import Image, Color4f
# class Screen:
# BLOCK = ' '#██'
# def __init__(self, width, height, offset=(0,0)):
# self.width = width
# self.height = height
# self.offset = offset
# self.data = [
# [(0,0,0, False) for _ in range(width)] for _ in range(height)
# ]
# wrt("\033[?47h") # save current screen
# wrt("\033[?25l") # Hide cursor
# wrt("\033[2J") # clear entire screen
# sys.stdout.flush()
# def close(self):
# wrt("\033[?47l") # save current screen
# wrt("\033[?25h") # Show cursor
# sys.stdout.flush()
# def __getitem__(self, coords) -> Color4f:
# x, y = coords
# c = self.data[y][x]
# return Color4f(c[0]/255, c[1]/255, c[2]/255)
# def __setitem__(self, coords, value):
# x, y = coords
# assert isinstance(value, tuple) and 3 <= len(value) <= 4
# r, g, b, *blink = value
# if len(blink) == 0 or blink[0] == False:
# blink= False
# else:
# blink = True
# assert (0<=r<=255) and (0<=g<=255) and (0<=b<=255), f"{value}"
# self.data[y][x] = (r, g, b, bool(blink))
# def hline(self, x0, x1, y, color):
# if x1<0:
# x1+=self.width
# for x in range(x0, x1+1):
# self[x, y] = color
# def vline(self, x, y0, y1, color):
# if y1<0:
# y1+=self.height
# for y in range(y0, y1+1):
# self[x,y] = color
# def rect(self, x0, y0, x1, y1, color):
# """Draw rectangle"""
# if x1 < 0:
# x1 += self.width
# if y1 < 0:
# y1 += self.height
# for x in range(x0, x1+1):
# self[x, y0] = self[x, y1] = color
# for y in range(y0, y1+1):
# self[x0, y] = self[x1, y] = color
# def fullrect(self, x0, y0, x1, y1, color):
# if x1 < 0:
# x1 += self.width
# if y1 < 0:
# y1 += self.height
# for x in range(x0, x1+1):
# for y in range(y0, y1+1):
# self[x, y] = color
# def sync(self):
# for y in range(self.height):
# wrt("\033[%d;%dH" % (1 + y+self.offset[1], 1 + 2 * self.offset[0])) # move cursor to start of yth row
# for x in range(self.width):
# r, g, b, blink = self.data[y][x]
# # if blink:
# # r2, g2, b2, _ = lighten((r, g, b, True),0.6)
# # wrt(f"\033[5m\033[48;2;{r2};{g2};{b2}m") # blinking color
# wrt(f"\033[48;2;{r};{g};{b}m%s\033[0m" % self.BLOCK) # print block in given RGB color
# sys.stdout.flush()
# wrt("\033[H")
# sys.stdout.flush()
# def imshow(self, image: Image, offset: Tuple[int, int] = (0, 0)):
# ox, oy = offset
# for y in range(image.height):
# for x in range(image.width):
# if (0 <= x + ox < self.width) and (0 <= y + oy < self.height):
# color = image[x, y]
# if color.a == 1.0:
# self[x+ox, y+oy] = color.as_3i
# elif color.a == 0.0:
# pass
# else:
# raise NotImplementedError('Alpha Blending')
# def clear(self, color=(0,0,0)):
# for y in range(self.height):
# for x in range(self.width):
# self[x, y] = color
# def save_to_pnm(self, path):
# with open(path, 'wb') as output:
# output.write(b'P6\n')
# output.write(f'{self.width} {self.height}\n'.encode('utf-8'))
# output.write(b'255\n')
# for y in range(self.height):
# for x in range(self.width):
# output.write(bytes(self.data[y][x][:3]))
wrt = sys.stdout.write
class SubPixelScreen:
@property
def height(self):
return self.canvas._height
@property
def width(self):
return self.canvas._width
def __init__(self, width, height, offset: Optional[Tuple[int,int]] = None):
self._offset = offset or (0, 0)
self.canvas = Image(width=width, height=height)
def __enter__(self):
wrt("\033[?47h") # save current screen
wrt("\033[?25l") # Hide cursor
wrt("\033[2J") # clear entire screen
sys.stdout.flush()
return self
def __exit__(self, exc_type, exc_value, traceback):
wrt("\033[?47l") # save current screen
wrt("\033[?25h") # Show cursor
sys.stdout.flush()
def sync(self):
#wrt("\033[2J") # clear entire screen
for y in range(self.canvas._height//2):
wrt("\033[%d;%dH" % (1 + y+self._offset[1], 1 + 2 * self._offset[0])) # move cursor to start of yth row
for x in range(self.canvas._width):
r, g, b = self.canvas[x, 2*y].as_3i
r2, g2, b2 = self.canvas[x, 2*y+1].as_3i
wrt(f"\033[38;2;{r};{g};{b}m\033[48;2;{r2};{g2};{b2}m▀") # print block in given RGB color
wrt('\033[0m')
if self.canvas._height % 2:
wrt("\033[%d;%dH" % (1+self.canvas._height//2+self._offset[1], 1 + 2 * self._offset[0]))
for x in range(self.canvas._width):
r, g, b = self.canvas[x, self.canvas._height-1].as_3i
wrt(f"\033[38;2;{r};{g};{b}m▀")
wrt('\033[0m')
wrt("\033[H")
sys.stdout.flush()