clear full-screen in script and get the previous prompts after exiting script

Today I was using glance tool in my office to monitor CPU utilization.When I run glance from terminal ,the command clears the screen and after all the work when I quit back to the terminal,the previous prompts are still there on my screen.I mean that I can see all the previous lines.My question is how is this effect of clearing screen achieved.If they had used command like clear,It should have cleared the whole screen,how did they manage to get back all the previous prompts ? I want to implement the same feature for some of my scripts.

Similarly when using vi command ,the screen is fully occupied and after exiting,we are able to view previous prompts as well.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Many terminal emulators maintain two copies of the terminal output: the normal screen buffer, and the alternate screen buffer. The normal buffer is for command line applications which normally don’t move the cursor outside the current line and provides scrollback. The alternate buffer is for full-screen applications and does not provide scrollback.

Glance, vi and others switch to the alternate screen when they start and switch back when they exit.

In xterm, you can see the non-current screen by toggling “Show Alternate Screen” in the middle mouse button menu.

From a script, you can obtain the control sequence to switch between screens with tput smcup and tput rmcup (smcup and rmcup are the terminfo capabilities to switch into and out of full screen mode).


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x