Rust challenge 47/100 - hector tutorial
Table of content
What is this
The rules of the game are explained in my original post.
47th Challenge
Challenge
Tody I will be looking at chapter 2 of the hecto tutorial. Its the basics to get a DIY text editor working.
Solution
I learnt to read raw text input from stdio
and how it differs to none-raw text input. For now I’m just returning what is entered.
use std::io::stdout;
use crossterm::event::poll;
use crossterm::{
cursor::position,
event::{read, DisableMouseCapture, EnableMouseCapture, Event, KeyCode},
execute,
terminal::{disable_raw_mode, enable_raw_mode},
Result,
};
use crossterm::event::KeyCode::Char;
use crossterm::style::Print;
use std::time::Duration;
fn print_events() -> Result<()> {
loop {
let event = read()?;
if let Event::Key(x) = event {
match x.code {
Char('q') => return Ok(()),
Char(x) => {
execute!(stdout(), Print(x.to_string()));
},
_Enter => {
execute!(stdout(), Print("\n\r".to_string()));
}
_ => {
execute!(
stdout(),
Print("special character".to_string())
);
}
}
}
}
}
fn main() -> Result<()> {
enable_raw_mode()?;
if let Err(e) = print_events() {
println!("Error: {:?}\r", e);
}
disable_raw_mode()
}
See github for today’s simple snippet.