keith-packard / snek

Snek programming language for tiny systems
GNU General Public License v3.0
292 stars 30 forks source link

When using curses.initscr() printing to screen doesn't work #87

Open mobluse opened 3 months ago

mobluse commented 3 months ago

I wrote a small test program in snek version 1.9 in most updated Debian GNU/Linux 12 (bookworm) aarch64 installed using apt on Raspberry Pi 4 B and I use LXTerminal 0.4.0, but it doesn't output when I use curses.initscr(), but works when I don't use it. It moves the cursor, but erase() and addstr() doesn't affect the screen.

curses.initscr() # I also tried stdscr=curses.initscr()
stdscr.erase()
stdscr.addstr(5, 16, "Hello")
stdscr.move(9, 0)
stdscr.refresh()
curses.endwin()

This works:

#curses.initscr()
stdscr.erase()
stdscr.addstr(5, 16, "Hello")
stdscr.move(9, 0)
stdscr.refresh()
#curses.endwin()

I intended to write a small arcade game and then I probably need initscr() in order to read key presses without blocking, but now I'm stuck at not being able to output. Raspberry Pi with this OS and terminal emulator is a very common system.

mobluse commented 3 months ago

I discovered my error: you have to use getch() or some other delay mechanism, because curses prints to another screen and then switches back.

This works with both python and snek:

import curses
stdscr=curses.initscr()
stdscr.erase()
stdscr.addstr(5, 16, "Hello")
stdscr.move(9, 0)
stdscr.refresh()
stdscr.getch()
curses.endwin()

A minimal version for snek:

curses.initscr()
stdscr.erase()
stdscr.addstr(5, 16, "Hello")
stdscr.move(9, 0)
stdscr.refresh()
stdscr.getch()
curses.endwin()

It might be useful to note this in the documentation. It's also useful that snek can use some stdscr methods without initscr().