summaryrefslogtreecommitdiffstats
path: root/third_party/rust/termion/examples
diff options
context:
space:
mode:
Diffstat (limited to 'third_party/rust/termion/examples')
-rw-r--r--third_party/rust/termion/examples/alternate_screen.rs17
-rw-r--r--third_party/rust/termion/examples/alternate_screen_raw.rs40
-rw-r--r--third_party/rust/termion/examples/async.rs39
-rw-r--r--third_party/rust/termion/examples/click.rs35
-rw-r--r--third_party/rust/termion/examples/color.rs10
-rw-r--r--third_party/rust/termion/examples/commie.rs51
-rw-r--r--third_party/rust/termion/examples/detect_color.rs19
-rw-r--r--third_party/rust/termion/examples/is_tty.rs11
-rw-r--r--third_party/rust/termion/examples/keys.rs44
-rw-r--r--third_party/rust/termion/examples/mouse.rs46
-rw-r--r--third_party/rust/termion/examples/rainbow.rs60
-rw-r--r--third_party/rust/termion/examples/read.rs23
-rw-r--r--third_party/rust/termion/examples/rustc_fun.rs24
-rw-r--r--third_party/rust/termion/examples/simple.rs42
-rw-r--r--third_party/rust/termion/examples/size.rs7
-rw-r--r--third_party/rust/termion/examples/truecolor.rs12
16 files changed, 480 insertions, 0 deletions
diff --git a/third_party/rust/termion/examples/alternate_screen.rs b/third_party/rust/termion/examples/alternate_screen.rs
new file mode 100644
index 0000000000..91d28b1702
--- /dev/null
+++ b/third_party/rust/termion/examples/alternate_screen.rs
@@ -0,0 +1,17 @@
+extern crate termion;
+
+use termion::screen::*;
+use std::io::{Write, stdout};
+use std::{time, thread};
+
+fn main() {
+ {
+ let mut screen = AlternateScreen::from(stdout());
+ write!(screen, "Welcome to the alternate screen.\n\nPlease wait patiently until we arrive back at the main screen in a about three seconds.").unwrap();
+ screen.flush().unwrap();
+
+ thread::sleep(time::Duration::from_secs(3));
+ }
+
+ println!("Phew! We are back.");
+}
diff --git a/third_party/rust/termion/examples/alternate_screen_raw.rs b/third_party/rust/termion/examples/alternate_screen_raw.rs
new file mode 100644
index 0000000000..7ba7888804
--- /dev/null
+++ b/third_party/rust/termion/examples/alternate_screen_raw.rs
@@ -0,0 +1,40 @@
+extern crate termion;
+
+use termion::event::Key;
+use termion::input::TermRead;
+use termion::raw::IntoRawMode;
+use termion::screen::*;
+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();
+ let mut screen = AlternateScreen::from(stdout().into_raw_mode().unwrap());
+ 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();
+ }
+ Key::Char('2') => {
+ write!(screen, "{}", ToAlternateScreen).unwrap();
+ write_alt_screen_msg(&mut screen);
+ }
+ _ => {}
+ }
+ screen.flush().unwrap();
+ }
+ write!(screen, "{}", termion::cursor::Show).unwrap();
+}
diff --git a/third_party/rust/termion/examples/async.rs b/third_party/rust/termion/examples/async.rs
new file mode 100644
index 0000000000..b16bb78618
--- /dev/null
+++ b/third_party/rust/termion/examples/async.rs
@@ -0,0 +1,39 @@
+extern crate termion;
+
+use termion::raw::IntoRawMode;
+use termion::async_stdin;
+use std::io::{Read, Write, stdout};
+use std::thread;
+use std::time::Duration;
+
+fn main() {
+ let stdout = stdout();
+ let mut stdout = stdout.lock().into_raw_mode().unwrap();
+ let mut stdin = async_stdin().bytes();
+
+ write!(stdout,
+ "{}{}",
+ termion::clear::All,
+ termion::cursor::Goto(1, 1))
+ .unwrap();
+
+ loop {
+ write!(stdout, "{}", termion::clear::CurrentLine).unwrap();
+
+ let b = stdin.next();
+ write!(stdout, "\r{:?} <- This demonstrates the async read input char. Between each update a 100 ms. is waited, simply to demonstrate the async fashion. \n\r", b).unwrap();
+ if let Some(Ok(b'q')) = b {
+ break;
+ }
+
+ stdout.flush().unwrap();
+
+ thread::sleep(Duration::from_millis(50));
+ stdout.write_all(b"# ").unwrap();
+ stdout.flush().unwrap();
+ thread::sleep(Duration::from_millis(50));
+ stdout.write_all(b"\r #").unwrap();
+ write!(stdout, "{}", termion::cursor::Goto(1, 1)).unwrap();
+ stdout.flush().unwrap();
+ }
+}
diff --git a/third_party/rust/termion/examples/click.rs b/third_party/rust/termion/examples/click.rs
new file mode 100644
index 0000000000..fe903d837f
--- /dev/null
+++ b/third_party/rust/termion/examples/click.rs
@@ -0,0 +1,35 @@
+extern crate termion;
+
+use termion::event::{Key, Event, MouseEvent};
+use termion::input::{TermRead, MouseTerminal};
+use termion::raw::IntoRawMode;
+use std::io::{Write, stdout, stdin};
+
+fn main() {
+ let stdin = stdin();
+ let mut stdout = MouseTerminal::from(stdout().into_raw_mode().unwrap());
+
+ write!(stdout,
+ "{}{}q to exit. Click, click, click!",
+ termion::clear::All,
+ termion::cursor::Goto(1, 1))
+ .unwrap();
+ stdout.flush().unwrap();
+
+ for c in stdin.events() {
+ let evt = c.unwrap();
+ match evt {
+ Event::Key(Key::Char('q')) => break,
+ Event::Mouse(me) => {
+ match me {
+ MouseEvent::Press(_, x, y) => {
+ write!(stdout, "{}x", termion::cursor::Goto(x, y)).unwrap();
+ }
+ _ => (),
+ }
+ }
+ _ => {}
+ }
+ stdout.flush().unwrap();
+ }
+}
diff --git a/third_party/rust/termion/examples/color.rs b/third_party/rust/termion/examples/color.rs
new file mode 100644
index 0000000000..16071be993
--- /dev/null
+++ b/third_party/rust/termion/examples/color.rs
@@ -0,0 +1,10 @@
+extern crate termion;
+
+use termion::{color, style};
+
+fn main() {
+ println!("{}Red", color::Fg(color::Red));
+ println!("{}Blue", color::Fg(color::Blue));
+ println!("{}Blue'n'Bold{}", style::Bold, style::Reset);
+ println!("{}Just plain italic", style::Italic);
+}
diff --git a/third_party/rust/termion/examples/commie.rs b/third_party/rust/termion/examples/commie.rs
new file mode 100644
index 0000000000..32f8820e50
--- /dev/null
+++ b/third_party/rust/termion/examples/commie.rs
@@ -0,0 +1,51 @@
+extern crate termion;
+
+use termion::{clear, color, cursor};
+
+use std::{time, thread};
+
+const COMMUNISM: &'static str = r#"
+ !######### #
+ !########! ##!
+ !########! ###
+ !########## ####
+ ######### ##### ######
+ !###! !####! ######
+ ! ##### ######!
+ !####! #######
+ ##### #######
+ !####! #######!
+ ####!########
+ ## ##########
+ ,######! !#############
+ ,#### ########################!####!
+ ,####' ##################!' #####
+ ,####' ####### !####!
+ ####' #####
+ ~## ##~
+"#;
+
+fn main() {
+ let mut state = 0;
+
+ println!("\n{}{}{}{}{}{}",
+ cursor::Hide,
+ clear::All,
+ cursor::Goto(1, 1),
+ color::Fg(color::Black),
+ color::Bg(color::Red),
+ COMMUNISM);
+ loop {
+ println!("{}{} ☭ GAY ☭ SPACE ☭ COMMUNISM ☭ ",
+ cursor::Goto(1, 1),
+ color::Bg(color::AnsiValue(state)));
+ println!("{}{} WILL PREVAIL, COMRADES! ",
+ cursor::Goto(1, 20),
+ color::Bg(color::AnsiValue(state)));
+
+ state += 1;
+ state %= 8;
+
+ thread::sleep(time::Duration::from_millis(90));
+ }
+}
diff --git a/third_party/rust/termion/examples/detect_color.rs b/third_party/rust/termion/examples/detect_color.rs
new file mode 100644
index 0000000000..ecfdd5b45a
--- /dev/null
+++ b/third_party/rust/termion/examples/detect_color.rs
@@ -0,0 +1,19 @@
+extern crate termion;
+
+use termion::color::{DetectColors, AnsiValue, Bg};
+use termion::raw::IntoRawMode;
+use std::io::stdout;
+
+fn main() {
+ let count;
+ {
+ let mut term = stdout().into_raw_mode().unwrap();
+ count = term.available_colors().unwrap();
+ }
+
+ println!("This terminal supports {} colors.", count);
+ for i in 0..count {
+ print!("{} {}", Bg(AnsiValue(i as u8)), Bg(AnsiValue(0)));
+ }
+ println!();
+}
diff --git a/third_party/rust/termion/examples/is_tty.rs b/third_party/rust/termion/examples/is_tty.rs
new file mode 100644
index 0000000000..52d1bc1d2d
--- /dev/null
+++ b/third_party/rust/termion/examples/is_tty.rs
@@ -0,0 +1,11 @@
+extern crate termion;
+
+use std::fs;
+
+fn main() {
+ if termion::is_tty(&fs::File::create("/dev/stdout").unwrap()) {
+ println!("This is a TTY!");
+ } else {
+ println!("This is not a TTY :(");
+ }
+}
diff --git a/third_party/rust/termion/examples/keys.rs b/third_party/rust/termion/examples/keys.rs
new file mode 100644
index 0000000000..272ffd1b9a
--- /dev/null
+++ b/third_party/rust/termion/examples/keys.rs
@@ -0,0 +1,44 @@
+extern crate termion;
+
+use termion::event::Key;
+use termion::input::TermRead;
+use termion::raw::IntoRawMode;
+use std::io::{Write, stdout, stdin};
+
+fn main() {
+ let stdin = stdin();
+ let mut stdout = stdout().into_raw_mode().unwrap();
+
+ write!(stdout,
+ "{}{}q to exit. Type stuff, use alt, and so on.{}",
+ termion::clear::All,
+ termion::cursor::Goto(1, 1),
+ termion::cursor::Hide)
+ .unwrap();
+ stdout.flush().unwrap();
+
+ for c in stdin.keys() {
+ write!(stdout,
+ "{}{}",
+ termion::cursor::Goto(1, 1),
+ termion::clear::CurrentLine)
+ .unwrap();
+
+ match c.unwrap() {
+ Key::Char('q') => break,
+ Key::Char(c) => println!("{}", c),
+ Key::Alt(c) => println!("^{}", c),
+ Key::Ctrl(c) => println!("*{}", c),
+ Key::Esc => println!("ESC"),
+ Key::Left => println!("←"),
+ Key::Right => println!("→"),
+ Key::Up => println!("↑"),
+ Key::Down => println!("↓"),
+ Key::Backspace => println!("×"),
+ _ => {}
+ }
+ stdout.flush().unwrap();
+ }
+
+ write!(stdout, "{}", termion::cursor::Show).unwrap();
+}
diff --git a/third_party/rust/termion/examples/mouse.rs b/third_party/rust/termion/examples/mouse.rs
new file mode 100644
index 0000000000..cbe8baf464
--- /dev/null
+++ b/third_party/rust/termion/examples/mouse.rs
@@ -0,0 +1,46 @@
+extern crate termion;
+
+use termion::event::*;
+use termion::cursor::{self, DetectCursorPos};
+use termion::input::{TermRead, MouseTerminal};
+use termion::raw::IntoRawMode;
+use std::io::{self, Write};
+
+fn main() {
+ let stdin = io::stdin();
+ let mut stdout = MouseTerminal::from(io::stdout().into_raw_mode().unwrap());
+
+ writeln!(stdout,
+ "{}{}q to exit. Type stuff, use alt, click around...",
+ termion::clear::All,
+ termion::cursor::Goto(1, 1))
+ .unwrap();
+
+ for c in stdin.events() {
+ let evt = c.unwrap();
+ match evt {
+ Event::Key(Key::Char('q')) => break,
+ Event::Mouse(me) => {
+ match me {
+ MouseEvent::Press(_, a, b) |
+ MouseEvent::Release(a, b) |
+ MouseEvent::Hold(a, b) => {
+ write!(stdout, "{}", cursor::Goto(a, b)).unwrap();
+ let (x, y) = stdout.cursor_pos().unwrap();
+ write!(stdout,
+ "{}{}Cursor is at: ({},{}){}",
+ cursor::Goto(5, 5),
+ termion::clear::UntilNewline,
+ x,
+ y,
+ cursor::Goto(a, b))
+ .unwrap();
+ }
+ }
+ }
+ _ => {}
+ }
+
+ stdout.flush().unwrap();
+ }
+}
diff --git a/third_party/rust/termion/examples/rainbow.rs b/third_party/rust/termion/examples/rainbow.rs
new file mode 100644
index 0000000000..4ee4000d01
--- /dev/null
+++ b/third_party/rust/termion/examples/rainbow.rs
@@ -0,0 +1,60 @@
+extern crate termion;
+
+use termion::event::Key;
+use termion::input::TermRead;
+use termion::raw::IntoRawMode;
+use std::io::{Write, stdout, stdin};
+
+fn rainbow<W: Write>(stdout: &mut W, blue: u8) {
+ write!(stdout,
+ "{}{}",
+ termion::cursor::Goto(1, 1),
+ termion::clear::All)
+ .unwrap();
+
+ for red in 0..32 {
+ let red = red * 8;
+ for green in 0..64 {
+ let green = green * 4;
+ write!(stdout,
+ "{} ",
+ termion::color::Bg(termion::color::Rgb(red, green, blue)))
+ .unwrap();
+ }
+ write!(stdout, "\n\r").unwrap();
+ }
+
+ writeln!(stdout, "{}b = {}", termion::style::Reset, blue).unwrap();
+}
+
+fn main() {
+ let stdin = stdin();
+ let mut stdout = stdout().into_raw_mode().unwrap();
+
+ writeln!(stdout,
+ "{}{}{}Use the up/down arrow keys to change the blue in the rainbow.",
+ termion::clear::All,
+ termion::cursor::Goto(1, 1),
+ termion::cursor::Hide)
+ .unwrap();
+
+ let mut blue = 172u8;
+
+ for c in stdin.keys() {
+ match c.unwrap() {
+ Key::Up => {
+ blue = blue.saturating_add(4);
+ rainbow(&mut stdout, blue);
+ }
+ Key::Down => {
+ blue = blue.saturating_sub(4);
+ rainbow(&mut stdout, blue);
+ }
+ Key::Char('q') => break,
+ _ => {}
+ }
+ stdout.flush().unwrap();
+ }
+
+ write!(stdout, "{}", termion::cursor::Show).unwrap();
+}
diff --git a/third_party/rust/termion/examples/read.rs b/third_party/rust/termion/examples/read.rs
new file mode 100644
index 0000000000..8d53e1bf72
--- /dev/null
+++ b/third_party/rust/termion/examples/read.rs
@@ -0,0 +1,23 @@
+extern crate termion;
+
+use termion::input::TermRead;
+use std::io::{Write, stdout, stdin};
+
+fn main() {
+ let stdout = stdout();
+ let mut stdout = stdout.lock();
+ let stdin = stdin();
+ let mut stdin = stdin.lock();
+
+ stdout.write_all(b"password: ").unwrap();
+ stdout.flush().unwrap();
+
+ let pass = stdin.read_passwd(&mut stdout);
+
+ if let Ok(Some(pass)) = pass {
+ stdout.write_all(pass.as_bytes()).unwrap();
+ stdout.write_all(b"\n").unwrap();
+ } else {
+ stdout.write_all(b"Error\n").unwrap();
+ }
+}
diff --git a/third_party/rust/termion/examples/rustc_fun.rs b/third_party/rust/termion/examples/rustc_fun.rs
new file mode 100644
index 0000000000..734e89f42b
--- /dev/null
+++ b/third_party/rust/termion/examples/rustc_fun.rs
@@ -0,0 +1,24 @@
+extern crate termion;
+
+use termion::{color, style};
+
+fn main() {
+ println!("{lighgreen}-- src/test/ui/borrow-errors.rs at 82:18 --\n\
+ {red}error: {reset}{bold}two closures require unique access to `vec` at the same time {reset}{bold}{magenta}[E0524]{reset}\n\
+ {line_num_fg}{line_num_bg}79 {reset} let append = |e| {{\n\
+ {line_num_fg}{line_num_bg}{info_line}{reset} {red}^^^{reset} {error_fg}first closure is constructed here\n\
+ {line_num_fg}{line_num_bg}80 {reset} vec.push(e)\n\
+ {line_num_fg}{line_num_bg}{info_line}{reset} {red}^^^{reset} {error_fg}previous borrow occurs due to use of `vec` in closure\n\
+ {line_num_fg}{line_num_bg}84 {reset} }};\n\
+ {line_num_fg}{line_num_bg}85 {reset} }}\n\
+ {line_num_fg}{line_num_bg}{info_line}{reset} {red}^{reset} {error_fg}borrow from first closure ends here",
+ lighgreen = color::Fg(color::LightGreen),
+ red = color::Fg(color::Red),
+ bold = style::Bold,
+ reset = style::Reset,
+ magenta = color::Fg(color::Magenta),
+ line_num_bg = color::Bg(color::AnsiValue::grayscale(3)),
+ line_num_fg = color::Fg(color::AnsiValue::grayscale(18)),
+ info_line = "| ",
+ error_fg = color::Fg(color::AnsiValue::grayscale(17)))
+}
diff --git a/third_party/rust/termion/examples/simple.rs b/third_party/rust/termion/examples/simple.rs
new file mode 100644
index 0000000000..93ef1df3ab
--- /dev/null
+++ b/third_party/rust/termion/examples/simple.rs
@@ -0,0 +1,42 @@
+extern crate termion;
+
+use termion::color;
+use termion::raw::IntoRawMode;
+use std::io::{Read, Write, stdout, stdin};
+
+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(5, 0, 0))),
+ // Write it to stdout.
+ a => write!(stdout, "{}", a),
+ }
+ .unwrap();
+
+ stdout.flush().unwrap();
+ }
+}
diff --git a/third_party/rust/termion/examples/size.rs b/third_party/rust/termion/examples/size.rs
new file mode 100644
index 0000000000..e91fa54308
--- /dev/null
+++ b/third_party/rust/termion/examples/size.rs
@@ -0,0 +1,7 @@
+extern crate termion;
+
+use termion::terminal_size;
+
+fn main() {
+ println!("Size is {:?}", terminal_size().unwrap())
+}
diff --git a/third_party/rust/termion/examples/truecolor.rs b/third_party/rust/termion/examples/truecolor.rs
new file mode 100644
index 0000000000..ba5c8685b8
--- /dev/null
+++ b/third_party/rust/termion/examples/truecolor.rs
@@ -0,0 +1,12 @@
+extern crate termion;
+
+use termion::{color, cursor, clear};
+use std::{thread, time};
+
+fn main() {
+ for r in 0..255 {
+ let c = color::Rgb(r, !r, 2 * ((r % 128) as i8 - 64).abs() as u8);
+ println!("{}{}{}wow", cursor::Goto(1, 1), color::Bg(c), clear::All);
+ thread::sleep(time::Duration::from_millis(100));
+ }
+}