I am trying to write a console chat client using only readline and ANSI escape codes.
My goal is to just let the terminal handle the scrollback and scrolling of the chat history while always providing a readline prompt after the messages for new input.
I have tried the following with my two threads. My console input thread does:
printf("\x1B[s"); // Save cursor position
message = readline("Prompt > ");
And my message receiving thread does:
message = receive_message(); // Blocks for next message
printf("\x1B[u"); // Restore cursor to before the prompt
printf("\x1B[J"); // Erase readline prompt and input (down to bottom of screen)
printf("%s\n", message); // Print the message (where readline was)
printf("\x1B[s"); // Save new cursor position
rl_forced_update_display(); // Restore readline
The above works as long as the readline input doesn't wrap. When it wraps the restoring of the saved cursor position doesn't work as expected, it only seems to restore the horizontal position, not the vertical position.
How can I adapt the above code to work even if the input line wraps?