-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
Copy pathpanela_colors.py
704 lines (572 loc) · 21.7 KB
/
panela_colors.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
# vim: encoding=utf-8
import os
import sys
import colored
import itertools
"""
After panela will be ready for it, it will be split out in a separate project,
that will be used for all chubin's console services.
There are several features that not yet implemented (see ___doc___ in Panela)
TODO:
* png output
"""
from wcwidth import wcswidth
from .colors import find_nearest_color, HEX_TO_ANSI, rgb_from_str
import pyte
# http://stackoverflow.com/questions/19782975/convert-rgb-color-to-the-nearest-color-in-palette-web-safe-color
try:
basestring # Python 2
except NameError:
basestring = str # Python 3
def color_mapping(clr):
if clr == 'default':
return None
return clr
class Point(object):
"""
One point (character) on a terminal
"""
def __init__(self, char=None, foreground=None, background=None):
self.foreground = foreground
self.background = background
self.char = char
class Panela:
"""
To implement:
Blocks manipulation:
[*] copy
[*] crop
[*] cut
[*] extend
[ ] join
[ ] move
[*] paste
[*] strip
Colors manipulation:
[*] paint foreground/background
[*] paint_line
[ ] paint_svg
[ ] fill background
[ ] fill_line
[ ] fill_svg
[ ] trans
Drawing:
[*] put_point
[*] put_line
[*] put_circle
[*] put_rectangle
Printing and reading:
ansi reads vt100 sequence
"""
def __init__(self, x=80, y=25, panela=None, field=None):
if panela:
self.field = [x for x in panela.field]
self.size_x = panela.size_x
self.size_y = panela.size_y
return
if field:
self.field = field
self.size_x = len(field[0])
self.size_y = len(field)
return
self.field = [[Point() for _ in range(x)] for _ in range(y)]
self.size_x = x
self.size_y = y
def in_field(self, col, row):
if col < 0:
return False
if row < 0:
return False
if col >= self.size_x:
return False
if row >= self.size_y:
return False
return True
#
# Blocks manipulation
#
def copy(self, x1, y1, x2, y2):
if x1 < 0:
x1 += self.size_x
if x2 < 0:
x2 += self.size_x
if x1 > x2:
x1, x2 = x2, x1
if y1 < 0:
y1 += self.size_y
if y2 < 0:
y2 += self.size_y
if y1 > y2:
y1, y2 = y2, y1
field = [self.field[i] for i in range(y1, y2+1)]
field = [line[x1:x2+1] for line in field]
return Panela(field=field)
def cut(self, x1, y1, x2, y2):
"""
"""
if x1 < 0:
x1 += self.size_x
if x2 < 0:
x2 += self.size_x
if x1 > x2:
x1, x2 = x2, x1
if y1 < 0:
y1 += self.size_y
if y2 < 0:
y2 += self.size_y
if y1 > y2:
y1, y2 = y2, y1
copied = self.copy(x1, y1, x2, y2)
for y in range(y1, y2+1):
for x in range(x1, x2+1):
self.field[y][x] = Point()
return copied
def extend(self, cols=None, rows=None):
"""
Adds [cols] columns from the right
and [rows] rows from the bottom
"""
if cols and cols > 0:
self.field = [x + [Point() for _ in range(cols)] for x in self.field]
self.size_x += cols
if rows and rows > 0:
self.field = self.field + [[Point() for _ in range(self.size_x)] for _ in range(rows)]
self.size_y += rows
def crop(self, left=None, right=None, top=None, bottom=None):
"""
Crop panela.
Remove <left>, <right> columns from left or right,
and <top> and <bottom> rows from top and bottom.
"""
if left:
if left >= self.size_x:
left = self.size_x
self.field = [x[left:] for x in self.field]
self.size_x -= left
if right:
if right >= self.size_x:
right = self.size_x
self.field = [x[:-right] for x in self.field]
self.size_x -= right
if top:
if top >= self.size_y:
top = self.size_y
self.field = self.field[top:]
self.size_y -= top
if bottom:
if bottom >= self.size_y:
bottom = self.size_y
self.field = self.field[:-bottom]
self.size_y -= bottom
def paste(self, panela, x1, y1, extend=False, transparence=False):
"""
Paste <panela> starting at <x1>, <y1>.
If <extend> is True current panela space will be automatically extended
If <transparence> is True, then <panela> is overlaid and characters behind them are seen
"""
# FIXME:
# negative x1, y1
# x1,y1 > size_x, size_y
if extend:
x_extend = 0
y_extend = 0
if x1 + panela.size_x > self.size_x:
x_extend = x1 + panela.size_x - self.size_x
if y1 + panela.size_y > self.size_y:
y_extend = y1 + panela.size_y - self.size_y
self.extend(cols=x_extend, rows=y_extend)
for i in range(y1, min(self.size_y, y1+panela.size_y)):
for j in range(x1, min(self.size_x, x1+panela.size_x)):
if transparence:
if panela.field[i-y1][j-x1].char and panela.field[i-y1][j-x1].char != " ":
if panela.field[i-y1][j-x1].foreground:
self.field[i][j].foreground = panela.field[i-y1][j-x1].foreground
if panela.field[i-y1][j-x1].background:
self.field[i][j].background = panela.field[i-y1][j-x1].background
self.field[i][j].char = panela.field[i-y1][j-x1].char
else:
self.field[i][j] = panela.field[i-y1][j-x1]
def strip(self):
"""
Strip panela: remove empty spaces around panels rectangle
"""
def left_spaces(line):
answer = 0
for elem in line:
if not elem.char:
answer += 1
else:
break
return answer
def right_spaces(line):
return left_spaces(line[::-1])
def empty_line(line):
return left_spaces(line) == len(line)
left_space = []
right_space = []
for line in self.field:
left_space.append(left_spaces(line))
right_space.append(right_spaces(line))
left = min(left_space)
right = min(right_space)
top = 0
while top < self.size_y and empty_line(self.field[top]):
top += 1
bottom = 0
while bottom < self.size_y and empty_line(self.field[-(bottom+1)]):
bottom += 1
self.crop(left=left, right=right, top=top, bottom=bottom)
#
# Drawing and painting
#
def put_point(self, col, row, char=None, color=None, background=None):
"""
Puts character with color and background color on the field.
Char can be a Point or a character.
"""
if not self.in_field(col, row):
return
if isinstance(char, Point):
self.field[row][col] = char
elif char is None:
if background:
self.field[row][col].background = background
if color:
self.field[row][col].foreground = color
else:
self.field[row][col] = Point(char=char, foreground=color, background=background)
def put_string(self, col, row, s=None, color=None, background=None):
"""
Put string <s> with foreground color <color> and background color <background>
ad <col>, <row>
"""
for i, c in enumerate(s):
self.put_point(col+i, row, c, color=color, background=background)
def put_line(self, x1, y1, x2, y2, char=None, color=None, background=None):
"""
Draw line (x1, y1) - (x2, y2) fith foreground color <color>, background color <background>
and character <char>, if specified.
"""
def get_line(start, end):
"""Bresenham's Line Algorithm
Produces a list of tuples from start and end
Source: http://www.roguebasin.com/index.php?title=Bresenham%27s_Line_Algorithm#Python
>>> points1 = get_line((0, 0), (3, 4))
>>> points2 = get_line((3, 4), (0, 0))
>>> assert(set(points1) == set(points2))
>>> print points1
[(0, 0), (1, 1), (1, 2), (2, 3), (3, 4)]
>>> print points2
[(3, 4), (2, 3), (1, 2), (1, 1), (0, 0)]
"""
# Setup initial conditions
x1, y1 = start
x2, y2 = end
dx = x2 - x1
dy = y2 - y1
# Determine how steep the line is
is_steep = abs(dy) > abs(dx)
# Rotate line
if is_steep:
x1, y1 = y1, x1
x2, y2 = y2, x2
# Swap start and end points if necessary and store swap state
swapped = False
if x1 > x2:
x1, x2 = x2, x1
y1, y2 = y2, y1
swapped = True
# Recalculate differentials
dx = x2 - x1
dy = y2 - y1
# Calculate error
error = int(dx / 2.0)
ystep = 1 if y1 < y2 else -1
# Iterate over bounding box generating points between start and end
y = y1
points = []
for x in range(x1, x2 + 1):
coord = (y, x) if is_steep else (x, y)
points.append(coord)
error -= abs(dy)
if error < 0:
y += ystep
error += dx
# Reverse the list if the coordinates were swapped
if swapped:
points.reverse()
return points
if color and not isinstance(color, basestring):
color_iter = itertools.cycle(color)
else:
color_iter = itertools.repeat(color)
if background and not isinstance(background, basestring):
background_iter = itertools.cycle(background)
else:
background_iter = itertools.repeat(background)
if char:
char_iter = itertools.cycle(char)
else:
char_iter = itertools.repeat(char)
for x, y in get_line((x1,y1), (x2, y2)):
char = next(char_iter)
color = next(color_iter)
background = next(background_iter)
self.put_point(x, y, char=char, color=color, background=background)
def paint(self, x1, y1, x2, y2, c1, c2=None, bg1=None, bg2=None, angle=None, angle_bg=None):
"""
Paint rectangle (x1,y1) (x2,y2) with foreground color c1 and background bg1 if specified.
If spefied colors c2/bg2, rectangle is painted with linear gradient (inclined under angle).
"""
def calculate_color(i, j):
if angle == None:
a = 0
else:
a = angle
r1, g1, b1 = rgb_from_str(c1)
r2, g2, b2 = rgb_from_str(c2)
k = 1.0*(j-x1)/(x2-x1)*(1-a)
l = 1.0*(i-y1)/(y2-y1)*a
r3, g3, b3 = int(r1 + 1.0*(r2-r1)*(k+l)), int(g1 + 1.0*(g2-g1)*(k+l)), int(b1 + 1.0*(b2-b1)*(k+l))
return "#%02x%02x%02x" % (r3, g3, b3)
def calculate_bg(i, j):
if angle_bg == None:
a = 0
else:
a = angle
r1, g1, b1 = rgb_from_str(bg1)
r2, g2, b2 = rgb_from_str(bg2)
k = 1.0*(j-x1)/(x2-x1)*(1-a)
l = 1.0*(i-y1)/(y2-y1)*a
r3, g3, b3 = int(r1 + 1.0*(r2-r1)*(k+l)), int(g1 + 1.0*(g2-g1)*(k+l)), int(b1 + 1.0*(b2-b1)*(k+l))
return "#%02x%02x%02x" % (r3, g3, b3)
if c2 == None:
for i in range(y1,y2):
for j in range(x1, x2):
self.field[i][j].foreground = c1
if bg1:
if bg2:
self.field[i][j].background = calculate_bg(i, j)
else:
self.field[i][j].background = bg1
else:
for i in range(y1,y2):
for j in range(x1, x2):
self.field[i][j].foreground = calculate_color(i, j)
if bg1:
if bg2:
self.field[i][j].background = calculate_bg(i, j)
else:
self.field[i][j].background = bg1
return self
def put_rectangle(self, x1, y1, x2, y2, char=None, frame=None, color=None, background=None):
"""
Draw rectangle (x1,y1), (x2,y2) using <char> character, <color> and <background> color
"""
frame_chars = {
'ascii': u'++++-|',
'single': u'┌┐└┘─│',
'double': u'┌┐└┘─│',
}
if frame in frame_chars:
chars = frame_chars[frame]
else:
chars = char*6
for x in range(x1, x2):
self.put_point(x, y1, char=chars[4], color=color, background=background)
self.put_point(x, y2, char=chars[4], color=color, background=background)
for y in range(y1, y2):
self.put_point(x1, y, char=chars[5], color=color, background=background)
self.put_point(x2, y, char=chars[5], color=color, background=background)
self.put_point(x1, y1, char=chars[0], color=color, background=background)
self.put_point(x2, y1, char=chars[1], color=color, background=background)
self.put_point(x1, y2, char=chars[2], color=color, background=background)
self.put_point(x2, y2, char=chars[3], color=color, background=background)
def put_circle(self, x0, y0, radius, char=None, color=None, background=None):
"""
Draw cricle with center in (x, y) and radius r (x1,y1), (x2,y2)
using <char> character, <color> and <background> color
"""
def k(x):
return int(x*1.9)
f = 1 - radius
ddf_x = 1
ddf_y = -2 * radius
x = 0
y = radius
self.put_point(x0, y0 + radius, char=char, color=color, background=background)
self.put_point(x0, y0 - radius, char=char, color=color, background=background)
self.put_point(x0 + k(radius), y0, char=char, color=color, background=background)
self.put_point(x0 - k(radius), y0, char=char, color=color, background=background)
char = "x"
while x < y:
if f >= 0:
y -= 1
ddf_y += 2
f += ddf_y
x += 1
ddf_x += 2
f += ddf_x
self.put_point(x0 + k(x), y0 + y, char=char, color=color, background=background)
self.put_point(x0 - k(x), y0 + y, char=char, color=color, background=background)
self.put_point(x0 + k(x), y0 - y, char=char, color=color, background=background)
self.put_point(x0 - k(x), y0 - y, char=char, color=color, background=background)
self.put_point(x0 + k(y), y0 + x, char=char, color=color, background=background)
self.put_point(x0 - k(y), y0 + x, char=char, color=color, background=background)
self.put_point(x0 + k(y), y0 - x, char=char, color=color, background=background)
self.put_point(x0 - k(y), y0 - x, char=char, color=color, background=background)
def read_ansi(self, seq, x=0, y=0, transparence=True):
"""
Read ANSI sequence and render it to the panela starting from x and y.
If transparence is True, replace spaces with ""
"""
screen = pyte.screens.Screen(self.size_x, self.size_y+1)
stream = pyte.streams.ByteStream()
stream.attach(screen)
stream.feed(seq.replace('\n', '\r\n'))
for i, line in sorted(screen.buffer.items(), key=lambda x: x[0]):
for j, char in sorted(line.items(), key=lambda x: x[0]):
if j >= self.size_x:
break
self.field[i][j] = Point(char.data, color_mapping(char.fg), color_mapping(char.bg))
def __str__(self):
answer = ""
skip_next = False
for i, line in enumerate(self.field):
for j, c in enumerate(line):
fg_ansi = ""
bg_ansi = ""
stop = ""
if self.field[i][j].foreground:
fg_ansi = '\033[38;2;%s;%s;%sm' % rgb_from_str(self.field[i][j].foreground)
stop = colored.attr("reset")
if self.field[i][j].background:
bg_ansi = '\033[48;2;%s;%s;%sm' % rgb_from_str(self.field[i][j].background)
stop = colored.attr("reset")
char = c.char or " "
if not skip_next:
answer += fg_ansi + bg_ansi + char.encode('utf-8') + stop
skip_next = wcswidth(char) == 2
# answer += "...\n"
answer += "\n"
return answer
########################################################################################################
class Template(object):
def __init__(self):
self._mode = 'page'
self.page = []
self.mask = []
self.code = []
self.panela = None
self.width = 0
self.height = 0
# [x, y, char, color, bg_color], ...
# colors are in #000000 format or None
# char is None for the end of line
self.data = []
self._colors = {
'A': '#00cc00',
'B': '#00cc00',
'C': '#00aacc',
'D': '#888888',
'E': '#cccc00',
'F': '#ff0000',
'H': '#22aa22',
'I': '#cc0000',
'J': '#000000',
}
self._bg_colors = {
'G': '#555555',
'J': '#555555',
}
def _process_line(self, line):
if line == 'mask':
self._mode = 'mask'
if line == '':
self._mode = 'code'
def read(self, filename):
"""
Read template from `filename`
"""
with open(filename) as f:
self._mode = 'page'
for line in f.readlines():
line = line.rstrip('\n')
if line.startswith('==[') and line.endswith(']=='):
self._process_line(line[3:-3].strip())
continue
if self._mode == 'page':
self.page.append(line)
elif self._mode == 'mask':
self.mask.append(line)
elif self._mode == 'code':
self.mask.append(line)
self.width = max([len(line) for line in self.page])
self.height = len(self.page)
def parse(self):
lines = self.page
self.data = []
for y, line in enumerate(lines):
x = 0
for x, char in enumerate(line):
color = None
bg_color = None
if y < len(self.mask):
if x < len(self.mask[y]):
m = self.mask[y][x]
color = self._colors.get(m)
bg_color = self._bg_colors.get(m)
self.data.append([x, y, char, color, bg_color])
# end of line
self.data.append([x, y, None, None, None])
def render_html(self, colorize=True, writer=sys.stdout):
prev_style = (None, None)
for x, y, char, color, bg_color in self.data:
style = (color, bg_color)
if char == None: # end of line
if colorize:
if prev_style != (None, None):
writer.write("</span>")
prev_style = (None, None)
writer.write("\n")
else:
if colorize:
if style != prev_style:
if prev_style != (None, None):
writer.write("</span>")
if style != (None, None):
fore = style[0] if style[0] else 'unset'
back = style[1] if style[1] else 'unset'
writer.write("<span style=\"" +
"color: %s; " % fore +
"background-color: %s\">" % back)
prev_style = style
if char == '<':
writer.write("<")
elif char == '>':
writer.write(">")
elif char == '&':
writer.write("&")
else:
writer.write(char)
def apply_mask(self):
self.panela = Panela(x=self.width, y=self.height)
self.panela.read_ansi("".join("%s\n" % x for x in self.page))
for x, y, _, color, bg_color in self.data:
if color or bg_color:
self.panela.put_point(x, y, color=color, background=bg_color)
def show(self):
if self.panela:
return str(self.panela)
return self.page
def main():
"Only for experiments"
from globals import MYDIR
pagepath = os.path.join(MYDIR, "share/firstpage-v2.pnl")
template = Template()
template.read(pagepath)
template.parse()
template.apply_mask()
sys.stdout.write(template.show())
if __name__ == '__main__':
main()