1
1
mirror of https://github.com/wez/wezterm.git synced 2024-12-23 13:21:38 +03:00
wezterm/termwiz/examples/hello.rs
Wez Furlong 5d360ae365 termwiz: Remove anyhow::Result from public API
It's been replaced with an opaque termwiz error type instead.

This is a bit of a more conservative approach than that in (refs: #407)
and has less of an impact on the surrounding code, which appeals to
me from a maintenance perspective.

refs: #406
refs: #407
2021-01-08 00:32:30 -08:00

67 lines
1.9 KiB
Rust

use termwiz::caps::Capabilities;
use termwiz::cell::AttributeChange;
use termwiz::color::AnsiColor;
use termwiz::input::{InputEvent, KeyCode, KeyEvent};
use termwiz::surface::{Change, Position, Surface};
use termwiz::terminal::buffered::BufferedTerminal;
use termwiz::terminal::{new_terminal, Terminal};
use termwiz::Error;
fn main() -> Result<(), Error> {
let caps = Capabilities::new_from_env()?;
let terminal = new_terminal(caps)?;
let mut buf = BufferedTerminal::new(terminal)?;
let mut block = Surface::new(5, 5);
block.add_change(Change::ClearScreen(AnsiColor::Blue.into()));
buf.draw_from_screen(&block, 10, 10);
buf.add_change(Change::Attribute(AttributeChange::Foreground(
AnsiColor::Maroon.into(),
)));
buf.add_change("Hello world\r\n");
buf.add_change(Change::Attribute(AttributeChange::Foreground(
AnsiColor::Red.into(),
)));
buf.add_change("and in red here\r\n");
buf.add_change(Change::CursorPosition {
x: Position::Absolute(0),
y: Position::Absolute(20),
});
buf.flush()?;
buf.terminal().set_raw_mode()?;
loop {
match buf.terminal().poll_input(None) {
Ok(Some(input)) => match input {
InputEvent::Key(KeyEvent {
key: KeyCode::Escape,
..
}) => {
break;
}
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;
}
}
}
Ok(())
}