2017-02-27 12:42:55 +00:00
extern crate termion ;
use termion ::event ::Key ;
use termion ::input ::TermRead ;
use termion ::raw ::IntoRawMode ;
2021-06-08 17:59:01 +01:00
use termion ::screen ::{ IntoAlternateScreen , ToAlternateScreen , ToMainScreen } ;
2017-02-27 12:42:55 +00:00
use std ::io ::{ Write , stdout , stdin } ;
fn write_alt_screen_msg < W : Write > ( screen : & mut W ) {
write! ( screen , " {}{}Welcome to the alternate screen.{}Press '1' to switch to the main screen or '2' to switch to the alternate screen.{}Press 'q' to exit (and switch back to the main screen). " ,
termion ::clear ::All ,
termion ::cursor ::Goto ( 1 , 1 ) ,
termion ::cursor ::Goto ( 1 , 3 ) ,
termion ::cursor ::Goto ( 1 , 4 ) ) . unwrap ( ) ;
}
fn main ( ) {
let stdin = stdin ( ) ;
2021-06-08 17:59:01 +01:00
let mut screen = stdout ( ) . into_raw_mode ( ) . unwrap ( ) . into_alternate_screen ( ) . unwrap ( ) ;
2017-02-27 12:42:55 +00:00
write! ( screen , " {} " , termion ::cursor ::Hide ) . unwrap ( ) ;
write_alt_screen_msg ( & mut screen ) ;
screen . flush ( ) . unwrap ( ) ;
for c in stdin . keys ( ) {
match c . unwrap ( ) {
Key ::Char ( 'q' ) = > break ,
Key ::Char ( '1' ) = > {
write! ( screen , " {} " , ToMainScreen ) . unwrap ( ) ;
2017-03-24 20:53:05 +00:00
}
2017-02-27 12:42:55 +00:00
Key ::Char ( '2' ) = > {
write! ( screen , " {} " , ToAlternateScreen ) . unwrap ( ) ;
write_alt_screen_msg ( & mut screen ) ;
2017-03-24 20:53:05 +00:00
}
_ = > { }
2017-02-27 12:42:55 +00:00
}
screen . flush ( ) . unwrap ( ) ;
}
write! ( screen , " {} " , termion ::cursor ::Show ) . unwrap ( ) ;
}