1

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 the application is finished?

Matias Cicero
  • 25,439
  • 13
  • 82
  • 154
Daniel Jones
  • 115
  • 1
  • 3
  • 10
  • Can you show us what you have tried already? – Ben Oct 19 '15 at 02:18
  • I have no idea where to start, I'm new to shell scripting... – Daniel Jones Oct 19 '15 at 02:31
  • Why is this tagged as `c#`? – Matias Cicero Oct 19 '15 at 02:37
  • I'm making the program in C#, but there is a .sh script executing it. – Daniel Jones Oct 19 '15 at 02:44
  • @DanielJones I see. Are you using Mono? – Matias Cicero Oct 19 '15 at 02:46
  • Tagged with `mono` to avoid future confusions – Matias Cicero Oct 19 '15 at 03:12
  • "But how do I reset the terminal back to normal" : What do you mean by this? Clearing the current terminal/shell text? Restoring the text that was there before your apps execution? Resetting the terminal mode/escape chars/prompt/etc.. ? – SushiHangover Oct 19 '15 at 03:17
  • @RobertN Yes, so when I execute my program with a command and exit it, I won't lose everything I had on my screen. – Daniel Jones Oct 19 '15 at 03:32
  • You would need to create an app that uses a different std out stream, such as termcap/ncurses/... libraries, this is why an app like `vi` does not effect the shell's current text buffer. By using .Net/Mono's Console.Write/WriteLine you have altered the shell's buffer and without handling each shell type specifically you do not have access the text that was already in the shell's application buffer before your app started – SushiHangover Oct 19 '15 at 03:57

1 Answers1

2

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?.

Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105