termion/examples/simple.rs

43 lines
1.1 KiB
Rust
Raw Normal View History

2016-03-15 20:36:33 +00:00
extern crate termion;
2016-03-06 13:55:01 +00:00
use termion::color;
use termion::raw::IntoRawMode;
2016-03-06 13:55:01 +00:00
use std::io::{Read, Write, stdout, stdin};
fn main() {
2016-04-02 17:05:31 +01:00
// Initialize 'em all.
let stdout = stdout();
2016-03-07 21:19:35 +00:00
let mut stdout = stdout.lock().into_raw_mode().unwrap();
let stdin = stdin();
2016-03-15 19:32:25 +00:00
let stdin = stdin.lock();
2016-03-06 13:55:01 +00:00
2017-03-24 20:53:05 +00:00
write!(stdout,
"{}{}{}yo, 'q' will exit.{}{}",
termion::clear::All,
termion::cursor::Goto(5, 5),
termion::style::Bold,
termion::style::Reset,
termion::cursor::Goto(20, 10))
.unwrap();
2016-03-07 15:23:05 +00:00
stdout.flush().unwrap();
2016-03-06 13:55:01 +00:00
let mut bytes = stdin.bytes();
loop {
let b = bytes.next().unwrap().unwrap();
match b {
2017-03-24 20:53:05 +00:00
// Quit
b'q' => return,
// Clear the screen
b'c' => write!(stdout, "{}", termion::clear::All),
// Set red color
b'r' => write!(stdout, "{}", color::Fg(color::Rgb(5, 0, 0))),
// Write it to stdout.
a => write!(stdout, "{}", a),
}
.unwrap();
2016-03-06 13:55:01 +00:00
2016-03-07 15:23:05 +00:00
stdout.flush().unwrap();
2016-03-06 13:55:01 +00:00
}
}