Search code examples
telnetansi-escape

In telnet, insert text instead of overwriting


So, I have a chat server in python (using twisted) setup, to which users connect using telnet. The problem is, when someone sends a message while someone else is typing one, the sent message will overwrite the one that is being typed. My current solution is to delete the line the cursor is on (with the \033[2K ANSI escape sequence, followed by \r), and replace it with the message, but obviously, people won't like it if their draft message gets deleted every time someone else sends one. I have tried to use escape sequences to

  1. save the cursor's position,
  2. go to the beginning of the line,
  3. insert the message followed by a new line, and
  4. return to the message by going to the saved position and going one line down.

However, the draft message gets overwritten by the sent one, instead of getting pushed aside. So my question is, is there a way to insert the sent message before the draft one without overwriting it, and then return the cursor to the message for further typing?


Solution

  • I've managed to avoid creating my own client, and instead, I'm using stty -icanon && nc {CHAT IP} {CHAT PORT}. On the server side, I've created a buffer to hold the message that is being typed, and to edit it accordingly and reprint it when it receives the character code for the backspace key. When it receives the character code for the arrow keys, it keeps track of the cursor and sends the escape codes to move the cursor accordingly.