File: simple.rs

package info (click to toggle)
rust-termion 4.0.6-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 280 kB
  • sloc: makefile: 10
file content (44 lines) | stat: -rw-r--r-- 1,094 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
extern crate termion;

use std::io::{stdin, stdout, Read, Write};
use termion::color;
use termion::raw::IntoRawMode;

fn main() {
    // Initialize 'em all.
    let stdout = stdout();
    let mut stdout = stdout.lock().into_raw_mode().unwrap();
    let stdin = stdin();
    let stdin = stdin.lock();

    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();
    stdout.flush().unwrap();

    let mut bytes = stdin.bytes();
    loop {
        let b = bytes.next().unwrap().unwrap();

        match b {
            // 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(255, 0, 0))),
            // Write it to stdout.
            a => write!(stdout, "{}", a),
        }
        .unwrap();

        stdout.flush().unwrap();
    }
}