The Accidental Rubyist

invalid byte sequence in UTF-8

TIL: ncurses cbreak and halfdelay

leave a comment »

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 wgetch or 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:

1. using cbreak in ncurses causes successive Control-C’s to be taken as interrupts.
2. Using half-delay also puts ncurses back into cbreak mode.

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.

Advertisements

Written by totalrecall

December 20, 2011 at 12:03 pm

Posted in ruby

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: