Programming with c

rob42rob42 Member Posts: 423
Are there any 'c' experts on here? If so, I've a quick question about 'printf'

I understand the basics, but while trying to discover how to have a terminal display update with the output of a program that I'm working on, but without having the cursor displayed, darting about all over the place as the data is updated, I came across this...
printf("\033[?25l");

... which makes the cursor invisible. Nice and simple.

My question is, how does it work and how do I reverse it, so that I can include another 'printf' function to make the cursor again visible, before my program terminates.

I trust that this question is not too "Off-Topic" for this forum.

Thanks.
No longer an active member

Comments

  • paul78paul78 Member Posts: 3,016 ■■■■■■■■■■
    That's actually not a C specific solution. It's simply generating an escape sequence which is interpreted by the terminal. The same technique can be used regardless of language.

    So what's happening is that it is printing out a ESC character which is octal 033. The \ denotes to printf() that it's an octal code - you can use hex or printf() escape instead if that's preferable such as \e. Unix terminals which are based on vt100 will interpret that ESC[ combination and then look for an instruction - in this case - ?25l as the instruction to make the cursor invisible. It's pretty old-school method but it's still supported by most shells and it's now a defacto way to provide in-band signaling to terminals.

    Edit - didn't read your actual question icon_smile.gif

    So - to make the cursor re-appear - you have to send a different vt100 code which is ?25h .


    printf("\033[?25h");

    Hope that makes sense.

    Good wiki explanation here - https://en.wikipedia.org/wiki/ANSI_escape_code

    And for anyone that don't know what a vt100 looks like - https://en.wikipedia.org/wiki/VT100 - and yeah that's what I used to use.

    Edit - clarified sequence elements
  • rob42rob42 Member Posts: 423
    Thank you very much; it makes perfect sense.

    I'll experiment with the escape codes that you've linked to, and see what I can find that may be of use.

    I was a little confused as I thought that it was the number '033', rather than O33 (or, Octal 33), but in retrospect, '033' makes no sense.

    Your time on this is much appreciated.
    No longer an active member
  • paul78paul78 Member Posts: 3,016 ■■■■■■■■■■
    You're welcome.

    If you do plan to do more terminal control other than turning the cursor on/off or colours, you may want to look into the C curses library. The library is a bit better to use in C if you want to do anything more complex. And the library will support various terminal types as well - https://en.wikipedia.org/wiki/Curses_(programming_library)

    Re - your comment about use of octal numbers - yeah - I may not have explained that well. It's actually sort of a nuance in how a C compiler interprets numbers. I'm sure it's in the C99 spec someplace. Basically - for octal - the leading 0 means it's octal. For hex, it would be an x and nothing for decimal. So you could in theory also do printf("\x1B[?25h"); I think.
  • rob42rob42 Member Posts: 423
    No, it was your explanation that made me realise my miss-interpretation, if you see what I mean: you made me realise that it was the letter o, rather than a zero. Thanks for the suggestion re: curses library. That link also links to a very useful pdf on the subject icon_cheers.gif I think I tried #include (but it could have been ncurses.h, or even lcurses.h I forget now, and I didn't make any notes) and if failed to find the library. It was after that, that I found the 'ESC hack' (it was posted the on the cboard.cprogramming.com site), but I'll work through it, again. Your right, on the face of it, I can't see why substituting 'o33' for 'x1b' wouldn't work either, but I'll get it a go, in the interests of experimentation and learning :) Thanks once again. p.s: Just in case any readers are wondering, I'm using 'GCC'.
    Edit:

    Scrub what I wrote before, as I was getting confused.

    The Octal is '033' (that's zero three three), not 'o33' ('o' for octal, three three). icon_redface.gif
    No longer an active member
  • rob42rob42 Member Posts: 423
    Information for anyone that may be interested:
    It seems that the curses library needs to be added with sudo apt-get install libncurses5-dev (for deb based distros) so that #include <curses.h> will work.
    No longer an active member
Sign In or Register to comment.