def start(self):
"""
start iterates the file and paints the text on the screen.
exits when q is pressed.
"""
try:
fileptr = 0
key = ''
lines = 1
while True:
# get the cursor position for further manipulation
y, x = self.stdscr.getyx()
key = self.stdscr.getch()
if key == curses.KEY_BACKSPACE or key == curses.KEY_DC or \
key == curses.KEY_DL or key == 127 :
# handle backspace
if x == 0 and y == 0:
continue
# take the file pointer back one step
fileptr -= 1
# update the screen
if x == 0:
lines -= 1
self.stdscr.addstr(y-1, len(self.virtualfile[lines-1]), ' ')
self.stdscr.move(y-1, len(self.virtualfile[lines-1]))
else:
self.stdscr.addstr(y, x-1, ' ')
self.stdscr.move(y, x-1)
elif key == curses.KEY_UP or key == curses.KEY_DOWN or \
key == curses.KEY_RESIZE or key == -1:
# ignore
continue
else:
text = self.get_text(fileptr)
# increase the lines if there are "\n" s
lines += sum([1 if c == '\n' else 0 for c in text])
fileptr += self.n
self.stdscr.addstr(text)
self.stdscr.refresh()
# graceful exit
curses.endwin()
except KeyboardInterrupt:
curses.endwin()
exit()
评论列表
文章目录