1

If you look at programs like Nano, Vim, or sl, it clears your screen while the program is running and opens a "new screen." Once the program is finished running, the original terminal with your command history reappears.

Is there a way to get this effect in C++ on Linux/MacOS without the ncurses library? I'd like my program to clear the screen during runtime and return to the state it was at before running. How could this be done on Windows?

  • The ncurses library probably uses OS specific functions, and it may consider the terminal type as well. I'm not aware of how to do this with pure C++ (with standard library) only. A terminal is beyond what is considered in C++ with standard libraries. I.e. stick with ncurses of have a look into its source code to find inspiration but I tend to recommend the former. – Scheff's Cat Jun 24 '22 at 05:44
  • Library recommendations are off-topic. That said... FYI: [Is ncurses available for windows?](https://stackoverflow.com/q/138153/7478597) (a bit aged) and [How To add Ncurses to my Project in Visual Studio 2017?](https://stackoverflow.com/q/54309104/7478597) which both mention [PDCurses](https://github.com/wmcbrine/PDCurses). – Scheff's Cat Jun 24 '22 at 05:49
  • 1
    I think the thing you want is called an *"Aternate Screen"*. It is mentioned here https://rich.readthedocs.io/en/latest/console.html?highlight=Alternate#alternate-screen and here https://stackoverflow.com/a/11024208/2836621 – Mark Setchell Jun 24 '22 at 07:17

1 Answers1

0

You may use tput smcup to "open" a new screen, and use "\033[H\033[J" to clean your screen. To exit, use tput rmcup.

For the reference: What this character sequence "\033[H\033[J" does in C?.