Skip to content

Commit 4afec1e

Browse files
committed
Parse .pnl file into data structure [x, y, char, color, bg_color], ...
This allows to use Template object for different kind of output
1 parent 8c7142c commit 4afec1e

File tree

2 files changed

+25
-6
lines changed

2 files changed

+25
-6
lines changed

lib/panela/panela_colors.py

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -568,6 +568,10 @@ def __init__(self):
568568
self.width = 0
569569
self.height = 0
570570

571+
# [x, y, char, color, bg_color], ...
572+
# colors are in #000000 format or None
573+
self.data = []
574+
571575
self._colors = {
572576
'A': '#00cc00',
573577
'B': '#00cc00',
@@ -613,17 +617,30 @@ def read(self, filename):
613617
self.width = max([len(line) for line in self.page])
614618
self.height = len(self.page)
615619

620+
def parse(self):
621+
lines = self.page
622+
self.data = []
623+
624+
for y, line in enumerate(lines):
625+
for x, char in enumerate(line):
626+
color = None
627+
bg_color = None
628+
if y < len(self.mask):
629+
if x < len(self.mask[y]):
630+
m = self.mask[y][x]
631+
color = self._colors.get(m)
632+
bg_color = self._bg_colors.get(m)
633+
634+
self.data.append([x, y, char, color, bg_color])
635+
616636
def apply_mask(self):
617637

618638
self.panela = Panela(x=self.width, y=self.height)
619639
self.panela.read_ansi("".join("%s\n" % x for x in self.page))
620640

621-
for i, line in enumerate(self.mask):
622-
for j, char in enumerate(line):
623-
if char in self._colors or char in self._bg_colors:
624-
color = self._colors.get(char)
625-
bg_color = self._bg_colors.get(char)
626-
self.panela.put_point(j, i, color=color, background=bg_color)
641+
for x, y, _, color, bg_color in self.data:
642+
if color or bg_color:
643+
self.panela.put_point(x, y, color=color, background=bg_color)
627644

628645
def show(self):
629646

@@ -640,6 +657,7 @@ def main():
640657
pagepath = os.path.join(MYDIR, "share/firstpage-v2.pnl")
641658
template = Template()
642659
template.read(pagepath)
660+
template.parse()
643661
template.apply_mask()
644662
sys.stdout.write(template.show())
645663

lib/pnl2html.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
pnl = Template()
2828
pnl.read(pnlfile)
29+
pnl.parse()
2930
pnl.apply_mask()
3031
print(pnl.show())
3132

0 commit comments

Comments
 (0)