termion/examples/simple.rs

45 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
2016-03-15 20:36:33 +00:00
use termion::{TermWrite, IntoRawMode, Color, Style};
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
2016-04-02 17:05:31 +01:00
// Move the cursor to (5, 5)
2016-03-07 15:23:05 +00:00
stdout.goto(5, 5).unwrap();
2016-04-02 17:05:31 +01:00
// Clear the screen.
2016-03-07 15:23:05 +00:00
stdout.clear().unwrap();
2016-04-02 17:05:31 +01:00
// Set style to bold.
stdout.style(Style::Bold).unwrap();
2016-04-02 17:05:31 +01:00
// Write some guiding stuff
2016-03-07 15:23:05 +00:00
stdout.write(b"yo, 'q' will exit.").unwrap();
2016-04-02 17:05:31 +01:00
// Reset the style.
2016-03-07 16:57:17 +00:00
stdout.reset().unwrap();
2016-04-02 17:05:31 +01:00
// Flush and goto (20, 10)
2016-03-07 15:23:05 +00:00
stdout.flush().unwrap();
stdout.goto(20, 10).unwrap();
2016-03-06 13:55:01 +00:00
let mut bytes = stdin.bytes();
loop {
let b = bytes.next().unwrap().unwrap();
match b {
2016-04-02 17:05:31 +01:00
// Quit
2016-03-06 13:55:01 +00:00
b'q' => return,
2016-04-02 17:05:31 +01:00
// Clear the screen
2016-03-06 13:55:01 +00:00
b'c' => stdout.clear(),
2016-04-02 17:05:31 +01:00
// Set red color
2016-03-07 16:39:25 +00:00
b'r' => stdout.color(Color::Rgb(5, 0, 0)),
2016-04-02 17:05:31 +01:00
// Write it to stdout.
2016-03-06 13:55:01 +00:00
a => stdout.write(&[a]),
2016-03-07 15:23:05 +00:00
}.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
}
}