2018-07-18 05:04:23 +03:00
|
|
|
extern crate failure;
|
2018-07-19 22:31:31 +03:00
|
|
|
extern crate termwiz;
|
2018-07-18 05:04:23 +03:00
|
|
|
|
|
|
|
use failure::Error;
|
2018-07-19 16:58:48 +03:00
|
|
|
use termwiz::caps::Capabilities;
|
2018-07-21 23:19:38 +03:00
|
|
|
use termwiz::cell::AttributeChange;
|
2018-07-19 16:58:48 +03:00
|
|
|
use termwiz::color::AnsiColor;
|
2018-07-24 05:38:35 +03:00
|
|
|
use termwiz::input::{InputEvent, KeyCode, KeyEvent};
|
2018-07-22 20:41:34 +03:00
|
|
|
use termwiz::surface::{Change, Position, Surface};
|
2018-07-22 18:03:11 +03:00
|
|
|
use termwiz::terminal::buffered::BufferedTerminal;
|
2018-07-24 05:38:35 +03:00
|
|
|
use termwiz::terminal::{new_terminal, Blocking, Terminal};
|
2018-07-21 14:26:27 +03:00
|
|
|
|
2018-07-18 05:04:23 +03:00
|
|
|
fn main() -> Result<(), Error> {
|
2018-07-19 16:58:48 +03:00
|
|
|
let caps = Capabilities::new_from_env()?;
|
2018-07-19 20:52:29 +03:00
|
|
|
|
2018-07-24 05:38:35 +03:00
|
|
|
let terminal = new_terminal(caps)?;
|
2018-07-19 20:52:29 +03:00
|
|
|
|
2018-07-22 18:03:11 +03:00
|
|
|
let mut buf = BufferedTerminal::new(terminal)?;
|
2018-07-19 16:58:48 +03:00
|
|
|
|
2018-07-22 20:41:34 +03:00
|
|
|
let mut block = Surface::new(5, 5);
|
|
|
|
block.add_change(Change::ClearScreen(AnsiColor::Blue.into()));
|
|
|
|
buf.draw_from_screen(&block, 10, 10);
|
|
|
|
|
2018-07-22 18:03:11 +03:00
|
|
|
buf.add_change(Change::Attribute(AttributeChange::Foreground(
|
2018-07-19 16:58:48 +03:00
|
|
|
AnsiColor::Maroon.into(),
|
|
|
|
)));
|
2018-07-22 18:03:11 +03:00
|
|
|
buf.add_change("Hello world\r\n");
|
|
|
|
buf.add_change(Change::Attribute(AttributeChange::Foreground(
|
2018-07-21 18:54:38 +03:00
|
|
|
AnsiColor::Red.into(),
|
|
|
|
)));
|
2018-07-22 18:03:11 +03:00
|
|
|
buf.add_change("and in red here\r\n");
|
2018-07-22 20:41:34 +03:00
|
|
|
buf.add_change(Change::CursorPosition {
|
|
|
|
x: Position::Absolute(0),
|
|
|
|
y: Position::Absolute(20),
|
|
|
|
});
|
2018-07-19 16:58:48 +03:00
|
|
|
|
2018-07-22 18:03:11 +03:00
|
|
|
buf.flush()?;
|
2018-07-19 21:26:24 +03:00
|
|
|
|
2018-07-24 05:38:35 +03:00
|
|
|
buf.terminal().set_raw_mode()?;
|
|
|
|
loop {
|
|
|
|
match buf.terminal().poll_input(Blocking::Yes) {
|
|
|
|
Ok(Some(input)) => match input {
|
|
|
|
InputEvent::Key(KeyEvent {
|
2018-07-24 07:45:05 +03:00
|
|
|
key: KeyCode::Escape,
|
2018-07-24 05:38:35 +03:00
|
|
|
..
|
|
|
|
}) => {
|
|
|
|
break;
|
|
|
|
}
|
2018-07-24 07:45:05 +03:00
|
|
|
InputEvent::Key(KeyEvent {
|
|
|
|
key: KeyCode::Char(c),
|
|
|
|
..
|
|
|
|
}) => {
|
|
|
|
buf.add_change(format!("{}", c));
|
|
|
|
buf.flush()?;
|
|
|
|
}
|
2018-07-24 05:38:35 +03:00
|
|
|
_ => {
|
|
|
|
print!("{:?}\r\n", input);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
Ok(None) => {}
|
|
|
|
Err(e) => {
|
|
|
|
print!("{:?}\r\n", e);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-19 16:58:48 +03:00
|
|
|
Ok(())
|
2018-07-18 05:04:23 +03:00
|
|
|
}
|