I'm writing an application in terminal with use of curses library. It's working fine but i've got an idea of adding few keystrokes, and also of functionality where user can select entity on screen and will get a info panel (I also need to figure this out). However while adding key listener with .getch() my systeminfo panel was not drawn, I was wondering why, and understood it's because .getch() is blocking. How do i prevent that blocking behaviour?
main.py
def setup_stdscr():
stdscr = curses.initscr()
curses.noecho()
curses.cbreak()
stdscr.keypad(True)
curses.curs_set(0)
stdscr.nodelay(True)
_init_colors()
return stdscr
...
if __name__ == "__main__":
stdscr = setup_stdscr()
stdscr.refresh()
win = curses.newwin(0, 0)
win.refresh()
width = win.getmaxyx()[1]
height = win.getmaxyx()[0]
args = _get_args(sys.argv)
sysinfo = None
if args.sysinfo:
sysinfo = SystemInfo(win)
if sysinfo is not None:
width = width
height = height - 2
ov = Overworld(win, width, height)
sys = System(ov, sysinfo)
curses.wrapper(sys.run())
System
def run(self):
self.overworld.spawn_entities()
while True:
self.overworld.update()
self.overworld.draw()
c = self.overworld.stdscr.getch()
if c:
if c == ord("q"):
break
if c == curses.KEY_MOUSE:
_, mx, my, _, state = curses.getmouse()
print(mx, my, state)
if state & curses.BUTTON1_PRESSED:
entity = self.overworld.get_entity_at(mx, my)
print(entity)
if entity:
self.show_entity_info(self.overworld.stdscr, entity)
if self.system_info:
self.system_info.draw()
self.overworld.stdscr.refresh()
curses.napms(MINUTE_LENGTH * 1000)
bus.emit("minute:passed")