diff options
author | wustho <benawiadha@gmail.com> | 2020-09-24 05:44:56 +0700 |
---|---|---|
committer | wustho <benawiadha@gmail.com> | 2020-09-24 05:44:56 +0700 |
commit | f291ebeeee65ef196a12219ae1a0a0ec64b5e4d1 (patch) | |
tree | 652c2e3e1b9211d197a2d2c7f8a49f10d234979b /epy.py | |
parent | 2c8891a2b508d54bc58ad3fe27bd180dec3819e7 (diff) | |
download | epy-f291ebeeee65ef196a12219ae1a0a0ec64b5e4d1.tar.gz |
Class Board as curses.newpad() replacement. TODO: PageDown behaviour
isn't working properly between Board.chunks.
Diffstat (limited to 'epy.py')
-rwxr-xr-x | epy.py | 79 |
1 files changed, 67 insertions, 12 deletions
@@ -1281,6 +1281,67 @@ def count_max_reading_pg(ebook): ALLPREVLETTERS, SUMALLLETTERS = count_pct(ebook) +class Board: + MAXCHUNKS = 32000 # lines + + def __init__(self, totlines, width): + self.chunks = [self.MAXCHUNKS*(i+1)-1 for i in range(totlines // self.MAXCHUNKS)] + self.chunks += [] if totlines % self.MAXCHUNKS == 0 else [totlines % self.MAXCHUNKS + (0 if self.chunks == [] else self.chunks[-1])] # -1 + self.pad = curses.newpad(min([self.MAXCHUNKS, totlines]), width) + self.pad.keypad(True) + # self.current_chunk = 0 + self.y = 0 + self.width = width + + def feed(self, textlist): + self.text = textlist + + def getch(self): + return self.pad.getch() + + def bkgd(self, bg): + self.pad.bkgd(SCREEN.getbkgd()) + + def find_chunkidx(self, y): + for n, i in enumerate(self.chunks): + if y <= i: + return n + + def paint_text(self, chunkidx=0): + self.pad.clear() + start_chunk = 0 if chunkidx == 0 else self.chunks[chunkidx-1]+1 + end_chunk = self.chunks[chunkidx] + for n, i in enumerate(self.text[start_chunk:end_chunk+1]): + if re.search("\\[IMG:[0-9]+\\]", i): + self.pad.addstr(n, self.width//2 - len(i)//2 + 1, i, curses.A_REVERSE) + else: + self.pad.addstr(n, 0, i) + # chapter suffix + ch_suffix = "***" # "\u3064\u3065\u304f" つづく + try: + self.pad.addstr(n+1, (self.width - len(ch_suffix))//2 + 1, ch_suffix) + except curses.error: + pass + + def chgat(self, y, x, n, attr): + chunkidx = self.find_chunkidx(y) + start_chunk = 0 if chunkidx == 0 else self.chunks[chunkidx-1]+1 + end_chunk = self.chunks[chunkidx] + if y in range(start_chunk, end_chunk+1): + self.pad.chgat(y % self.MAXCHUNKS, x, n, attr) + + def getbkgd(self): + return self.pad.getbkgd() + + def refresh(self, y, b, c, d, e, f): + chunkidx = self.find_chunkidx(y) + if chunkidx != self.find_chunkidx(self.y): + self.paint_text(chunkidx) + # TODO: not modulo by self.MAXCHUNKS but self.pad.height + self.pad.refresh(y % self.MAXCHUNKS, b, c, d, e, f) + self.y = y + + def reader(ebook, index, width, y, pctg, sect): global SHOWPROGRESS @@ -1314,25 +1375,19 @@ def reader(ebook, index, width, y, pctg, sect): else: y = y % totlines - pad = curses.newpad(totlines, width + 2) # + 2 unnecessary + pad = Board(totlines, width) + pad.feed(src_lines) + + # this make curses.A_REVERSE not working + # put before paint_text if COLORSUPPORT: pad.bkgd(SCREEN.getbkgd()) - pad.keypad(True) + pad.paint_text(0) LOCALPCTG = [] for n, i in enumerate(src_lines): - if re.search("\\[IMG:[0-9]+\\]", i): - pad.addstr(n, width//2 - len(i)//2 + 1, i, curses.A_REVERSE) - else: - pad.addstr(n, 0, i) LOCALPCTG.append(len(re.sub("\s", "", i))) - # chapter suffix - ch_suffix = "***" # "\u3064\u3065\u304f" つづく - try: - pad.addstr(n+1, (width - len(ch_suffix))//2 + 1, ch_suffix) - except curses.error: - pass SCREEN.clear() SCREEN.refresh() |