I want to write a GUI/Terminal based application where I would need to clear the terminal and then write out the gui.
But how do I reset the terminal back to normal once
Terminals (such as xterm and "any" which emulate it) support a feature called the alternate screen. Often, terminal descriptions include switching to/from the alternate screen in the smcup
and rmcup
capabilities, and it is used by ncurses applications. (The feature is not always used, because some users do not like the feature). Even if it is not part of the terminal description (seen with tgetstr("ti") or tigetstr("smcup")) your application could write the literal escape sequence.
On switching to the alternate screen, the convention (used in the terminal escape sequences) is to clear the alternate screen, putting the cursor at the upper left corner. Switching back restores the original (normal) screen and cursor location.
There is some discussion of alternate screen in the xterm FAQ Why doesn't the screen clear when running vi?.