1
1
mirror of https://github.com/wez/wezterm.git synced 2024-12-28 07:55:03 +03:00
wezterm/termwiz/examples/hello.rs

67 lines
1.9 KiB
Rust
Raw Normal View History

2018-07-19 16:58:48 +03:00
use termwiz::caps::Capabilities;
use termwiz::cell::AttributeChange;
2018-07-19 16:58:48 +03:00
use termwiz::color::AnsiColor;
use termwiz::input::{InputEvent, KeyCode, KeyEvent};
use termwiz::surface::{Change, Position, Surface};
2018-07-22 18:03:11 +03:00
use termwiz::terminal::buffered::BufferedTerminal;
use termwiz::terminal::{new_terminal, Terminal};
use termwiz::Error;
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()?;
let terminal = new_terminal(caps)?;
2018-07-22 18:03:11 +03:00
let mut buf = BufferedTerminal::new(terminal)?;
2018-07-19 16:58:48 +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");
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
buf.terminal().set_raw_mode()?;
loop {
match buf.terminal().poll_input(None) {
Ok(Some(input)) => match input {
InputEvent::Key(KeyEvent {
2018-07-24 07:45:05 +03:00
key: KeyCode::Escape,
..
}) => {
break;
}
2018-07-24 07:45:05 +03:00
InputEvent::Key(KeyEvent {
key: KeyCode::Char(c),
..
}) => {
buf.add_change(format!("{}", c));
buf.flush()?;
}
_ => {
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
}