]> git.proxmox.com Git - cargo.git/blob - vendor/termion-1.5.1/examples/rainbow.rs
New upstream version 0.23.0
[cargo.git] / vendor / termion-1.5.1 / examples / rainbow.rs
1 extern crate termion;
2
3 use termion::event::Key;
4 use termion::input::TermRead;
5 use termion::raw::IntoRawMode;
6 use std::io::{Write, stdout, stdin};
7
8 fn rainbow<W: Write>(stdout: &mut W, blue: u8) {
9 write!(stdout,
10 "{}{}",
11 termion::cursor::Goto(1, 1),
12 termion::clear::All)
13 .unwrap();
14
15 for red in 0..32 {
16 let red = red * 8;
17 for green in 0..64 {
18 let green = green * 4;
19 write!(stdout,
20 "{} ",
21 termion::color::Bg(termion::color::Rgb(red, green, blue)))
22 .unwrap();
23 }
24 write!(stdout, "\n\r").unwrap();
25 }
26
27 writeln!(stdout, "{}b = {}", termion::style::Reset, blue).unwrap();
28 }
29
30 fn main() {
31 let stdin = stdin();
32 let mut stdout = stdout().into_raw_mode().unwrap();
33
34 writeln!(stdout,
35 "{}{}{}Use the up/down arrow keys to change the blue in the rainbow.",
36 termion::clear::All,
37 termion::cursor::Goto(1, 1),
38 termion::cursor::Hide)
39 .unwrap();
40
41 let mut blue = 172u8;
42
43 for c in stdin.keys() {
44 match c.unwrap() {
45 Key::Up => {
46 blue = blue.saturating_add(4);
47 rainbow(&mut stdout, blue);
48 }
49 Key::Down => {
50 blue = blue.saturating_sub(4);
51 rainbow(&mut stdout, blue);
52 }
53 Key::Char('q') => break,
54 _ => {}
55 }
56 stdout.flush().unwrap();
57 }
58
59 write!(stdout, "{}", termion::cursor::Show).unwrap();
60 }