This repository has been archived by the owner on May 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tshow.py
74 lines (57 loc) · 1.71 KB
/
tshow.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
import curses
import sys
from time import sleep, time
DISPLAY_MODE = {
'WHITE': 1,
'BLACK': 2
}
def getDisplayMode(mode):
if mode not in DISPLAY_MODE:
return 0
else:
return DISPLAY_MODE[mode]
def main(stdscr):
if(len(sys.argv) < 2):
sys.exit(1)
filepath = sys.argv[1]
with open(filepath) as f:
f.readline()
name, fps, width, height, _, _ = f.readline().split(',')
fps = float(fps)
width, height = map(int, [width, height])
f.readline()
rows, cols = stdscr.getmaxyx()
if(rows < height or cols < width):
exit(1)
texts = []
cnt = 0
tmpstr = ''
frame_height = height + 1
for text in f:
tmpstr += text
cnt += 1
if cnt == frame_height:
cnt = 0
texts.append(tmpstr)
tmpstr = ''
curses.init_pair(1, curses.COLOR_BLACK, curses.COLOR_WHITE)
curses.init_pair(2, curses.COLOR_WHITE, curses.COLOR_BLACK)
stdscr.bkgd(' ', curses.color_pair(1))
begin_time = time()
end_time = time()
delta = 0.00625
# takes the very first time of playing the text
begin_time = time()
frame = 0
for text in texts:
stdscr.erase()
mode, rendertext = text.split('\n', 1)
stdscr.bkgd(' ', curses.color_pair(getDisplayMode(mode)))
stdscr.addstr(rendertext)
sleep_time = (frame + 1) / fps - (time() - begin_time)
sleep(max(0, sleep_time))
stdscr.refresh()
end_time = time()
frame += 1
if __name__ == "__main__":
curses.wrapper(main)