Using python curses I am trying to write some text in to window.
But when I reach end of window i get addstr() returned ERR
how to scroll the output page by page or line by line.? how can I bind SPACE key or down arrow ?
here is my code:
try:
screen = curses.initscr()
screen.immedok(True)
curses.start_color()
curses.init_pair(1, curses.COLOR_GREEN, curses.COLOR_BLACK)
curses.init_pair(2, curses.COLOR_WHITE, curses.COLOR_BLACK)
screen.bkgd(curses.color_pair(2))
screen.scrollok(1)
screen.border(0)
p = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
(output, err) = p.communicate()
linenumber = 1
for line in getline(output):
if linenumber <= 2:
# First two lines are header, mark bold
screen.addstr(linenumber, 2, line, curses.color_pair(1)|curses.A_BOLD)
else:
screen.addstr(linenumber, 2, line)
linenumber += 1
screen.getch()
curses.endwin()
except Exception, e:
print "\n\033[1;91m[FAILED]\033[0m Interrupt ", e
logging.error(e, exc_info=True)
curses.endwin()
The problem is that your calls to
screen.addstr
have asked for a line number past the end of the screen. Taking into account the box which you have drawn on the screen, a better approach would be to create a new window just inside the box (usingcurses.newwin
for example) and write theaddstr
calls within that window.Your example does not compile as given. Here is a working example: