TIL: ncurses cbreak and halfdelay
I accidentally typed C-c quickly in a sample of rbcurse and found that the program crashed giving an error somewhere deep in
logger. I repeated this in various programs, the line giving the error would be random. I tried various versions of ruby, wondered whether this had started happening after upgrading to Lion, or perhaps a bug in ffi-ncurses, or something with iTerm2, or maybe xterm-256color.
(I might add, that when i do a
getch in my app, there is "rescue Interrupt" clause to take care of this situation.)
After many hours, I finally found that if I press C-c slowly one after the other, my app takes it, but in quick succession, the 3rd or 4th causes the program to abort. After further research, i came down to the following:
cbreak in ncurses causes successive Control-C’s to be taken as interrupts.
half-delay also puts ncurses back into
I was using half-delay so that some complex key codes can be handled, and this includes using ESC in place of ALT/META. So pressing ESC followed by ‘w’ in quick succession is taken as ALT-w and not 2 key strokes.
Currently my working solution has been to comment off
cbreak, and to use
nodelay in place of
halfdelay. It seems to be working as of now. No idea whether there is some key under some terminal that may fail. I have tested this out under many different TERMs and stuff. I hope this doesn’t make the application hog CPU.
Update: I cannot set nodelay in startup as this means that wherever in the application I might do a
getch, it will be non-blocking and I will not get the key I am expecting. I found this while typing “gg” in a textview. So I have used a
wtimeout and set it to 1000 for the meantime. Switching off
nodelay by itself does not work for me since in my app, when the window gets an ESC it waits for the next character or ERR to see if its an Esc-alphabet.