问题
I'm trying to make my command line game to enter its own session (I don't know what the name is, but it looks like this) in bash:
My first attempt looks like this:
To accomplish the above, I use:system('clear')
and escape key \r\e to rewrite lines above the cursor current position:print "\r\e[#{lines}A"
What I want is when the command line application exits, the terminal change back to its previous state as if nothing happened, just like using nano, less, and many other command line tools.
How do you make a command line application do that with ruby?
回答1:
I think you're a looking for rmcup and smcup terminal capabilities.
print `tput smcup`
system('clear')
puts 'We are on alternate buffer'
puts 'press enter to switch back'
gets
print `tput rmcup`
You might also want to check "Why doesn't the screen clear when running vi?"
来源:https://stackoverflow.com/questions/31845890/ruby-gem-command-line-application-creating-a-new-session-in-bash