Hide cursor on remote terminal
Asked Answered
E

4

13

I have an open socket to a remote terminal. Using the answer to "Force telnet client into character mode" I was able to put that terminal into character mode.

My question is, how do I hide the cursor in the remote terminal using this method?

Exurbanite answered 15/4, 2010 at 23:46 Comment(0)
V
13

This is something that the ncurses library can do for you.

The curs_set() function can make the cursor invisible.

Vitiated answered 20/4, 2010 at 16:44 Comment(0)
F
18

To expand upon mjh2007's answer, the following c/c++ code will implement sending the escape codes to the terminal, and is slightly more readable than raw hex numbers.

void showCursor(bool show) const {
#define CSI "\e["
  if (show) {
    fputs(CSI "?25h", stdout);
  }
  else {
    fputs(CSI "?25l", stdout);
  }
#undef CSI
}
Fides answered 7/3, 2012 at 15:33 Comment(5)
Do not hard-code the terminal control codes unless you have no other choice. Use ncurses to look up the appropriate codes for the current $TERM value unless you cannot use ncurses for some reason.Inlier
I agree that ncursrs would be best, but if for some reason you don't have access to that, this might be a "good enough" solution for some cases.Fides
Powershell seems to respect this sequence if it's preceded by a SGR escape sequence or a cursor move sequence, but not if it's preceded by some other sequences including another instance of ?25! \e[0m\e[?25h seems to work fine.Pertain
As for the escape codes: \e[?25h and \e[?25l. To memorize it, remember that L stands for Lehidden and H stands for HonestlyTheCursorWillBeVisible.Disrupt
Also, no offense or anything, but using ncurses for this is like downloading a web-browser to read a text file :) @ChrisPageDisrupt
V
13

This is something that the ncurses library can do for you.

The curs_set() function can make the cursor invisible.

Vitiated answered 20/4, 2010 at 16:44 Comment(0)
O
6

If the terminal you are using supports ANSI format you should be able to send the following escape codes:

Hide the cursor: 0x9B 0x3F 0x32 0x35 0x6C
Show the cursor: 0x9B 0x3F 0x32 0x35 0x68
Oehsen answered 20/4, 2010 at 20:20 Comment(3)
I know this is a few years old but where did you get that command? Is there a link with other escape codes in?Mazdaism
Do not hard-code the terminal control codes unless you have no other choice. Use ncurses to look up the appropriate codes for the current $TERM value unless you cannot use ncurses for some reason.Inlier
Like my code is running on a PIC microprocessor w/ 8KB of ROM?Oehsen
S
3

If this is using the 'telnet' application then your app should send 'IAC WILL ECHO' to disable echoing on their remote side. This is useful for entering passwords or if your app is doing the echoing.

#define TEL_IAC "\377"
#define TEL_WILL "\373"
#define TEL_ECHO "\001"

char buf[4];
snprintf(buf, sizeof(buf), "%c%c%c" TEL_IAC, TEL_WILL, TEL_ECHO);
write(sock, buf, sizeof(buf));

Or

write(sock, TEL_IAC TEL_WILL TEL_ECHO, 3);

Hope this helps.

Samaritan answered 20/4, 2010 at 16:39 Comment(1)
The question seems to be about hiding the cursor, not hiding user input.Inlier

© 2022 - 2024 — McMap. All rights reserved.