Expand description
§winterm
A Rust library to create a pixelated window inside a terminal.
It uses crossterm as a backend.
§Adding winterm as a dependency
cargo add winterm@0.6.0
cargo add crossterm@0.25.0
§Create a window
use winterm::Window;
let mut window = Window::new(height, width)?;
§Render the next frame
use crossterm::style::Color;
window.set_pixel(0, 0, Color::Red);
window.set_pixel(
y,
x,
Color::Rgb {
r: 0x3E,
g: 0xB4,
b: 0x89,
},
);
window.redraw()?;
§React to events
use crossterm::event::KeyCode;
window.poll_events()?;
if window.get_key(KeyCode::Esc) {
// the Escape key has been pressed
}
if window.get_key(KeyCode::Char('w')) {
// the W key has been pressed
}
§Example
use crossterm::{event::KeyCode, style::Color, Result};
use winterm::Window;
fn main() -> Result<()> {
let mut window = Window::new(9, 16)?;
let mut color = Color::Black;
loop {
window.poll_events()?;
if window.get_key(KeyCode::Esc) {
break;
}
if window.get_key(KeyCode::Char('n')) {
color = match color {
Color::Black => Color::Red,
Color::Red => Color::Rgb {
r: 0x3E,
g: 0xB4,
b: 0x89,
},
_ => Color::Black,
}
}
for y in 0..window.height() {
for x in 0..window.width() {
window.set_pixel(y, x, color);
}
}
window.redraw()?;
}
Ok(())
}
§Debugging
Since winterm uses the terminal “alternate screen”, it can be complicated to debug using the print functions.
One way to deal with this problem is to use stderr (e.g. dbg!
, eprintln!
, …) and redirect it to a file :
cargo run 2> logs
You can now use cat logs
after execution or tail -f logs
in another terminal to get your output while the code is still running.
Structs§
- Window
- Window representation. Used for drawing and events handling.