If you disable the echo (see stty(1) -echo ) for the terminal, then you have full control over when the input is printed on the screen.
My assumption is that the insertion causes all lines to be written to the terminal at once, and your program will never get the opportunity to send output to the terminal when necessary. Thus, if you turn off the echo and print out user input as you type it, you can perform special processing when you see newline characters.
You can see that the irb program does something very similar by running strace on it:
ioctl(0, SNDCTL_TMR_TIMEBASE or TCGETS, {B38400 opost isig icanon echo ...}) = 0 ... ioctl(0, SNDCTL_TMR_STOP or TCSETSW, {B38400 opost isig -icanon -echo ...}) = 0
source share