Restructuring code as "impl" + proper quitting
This commit is contained in:
parent
fa5cddb358
commit
5fc444519d
1 changed files with 65 additions and 52 deletions
117
src/main.rs
117
src/main.rs
|
@ -4,75 +4,88 @@ use crossterm::{
|
||||||
};
|
};
|
||||||
use std::panic;
|
use std::panic;
|
||||||
use std::{
|
use std::{
|
||||||
io::{stdout, Result, Write},
|
io::{stdout, Result, Write, Stdout},
|
||||||
time::Duration,
|
time::Duration,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
#[derive(Default)]
|
||||||
|
struct Nuudel {
|
||||||
|
quitting: bool,
|
||||||
|
cursorx: i32,
|
||||||
|
cursory: i32,
|
||||||
|
}
|
||||||
|
|
||||||
fn main() -> Result<()> {
|
fn main() -> Result<()> {
|
||||||
// Setup panic handling
|
// Setup panic handling
|
||||||
let default_panic = panic::take_hook();
|
let default_panic = panic::take_hook();
|
||||||
panic::set_hook(Box::new(move |info| {
|
panic::set_hook(Box::new(move |info| {
|
||||||
let _ = restore();
|
let _ = Nuudel::restore_terminal();
|
||||||
default_panic(info);
|
default_panic(info);
|
||||||
}));
|
}));
|
||||||
|
|
||||||
// Start terminaling
|
// Basic stuff
|
||||||
init()?;
|
let mut nuudel = Nuudel::default();
|
||||||
let mut stdout = stdout();
|
Nuudel::init_terminal()?;
|
||||||
print!("Press Q to quit");
|
nuudel.run(&mut stdout())?;
|
||||||
|
Nuudel::restore_terminal()?;
|
||||||
|
|
||||||
// Main loop
|
Ok(())
|
||||||
let quitting = false;
|
}
|
||||||
while !quitting {
|
|
||||||
//stdout.execute(terminal::BeginSynchronizedUpdate)?;
|
impl Nuudel {
|
||||||
handle_events()?;
|
|
||||||
stdout.flush()?;
|
pub fn run(&mut self, stdout: &mut Stdout) -> Result<()> {
|
||||||
//stdout.execute(terminal::EndSynchronizedUpdate)?;
|
print!("Press Q to quit");
|
||||||
|
while !self.quitting {
|
||||||
|
self.handle_events()?;
|
||||||
|
stdout.flush()?;
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
// Stop terminaling
|
fn init_terminal() -> Result<()> {
|
||||||
restore()?;
|
terminal::enable_raw_mode()?;
|
||||||
Ok(())
|
stdout().execute(terminal::EnterAlternateScreen)?;
|
||||||
}
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
fn init() -> Result<()> {
|
fn restore_terminal() -> Result<()> {
|
||||||
terminal::enable_raw_mode()?;
|
stdout().execute(terminal::LeaveAlternateScreen)?;
|
||||||
stdout().execute(terminal::EnterAlternateScreen)?;
|
terminal::disable_raw_mode()?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
fn quit(&mut self) {
|
||||||
|
self.quitting = true;
|
||||||
|
}
|
||||||
|
|
||||||
fn restore() -> Result<()> {
|
fn handle_events(&mut self) -> Result<()> {
|
||||||
stdout().execute(terminal::LeaveAlternateScreen)?;
|
if poll(Duration::from_secs(0))? {
|
||||||
terminal::disable_raw_mode()?;
|
match read()? {
|
||||||
Ok(())
|
Event::Key(event) => match event.code {
|
||||||
}
|
KeyCode::Char('q') => {
|
||||||
|
self.quit();
|
||||||
fn handle_events() -> Result<()> {
|
},
|
||||||
if poll(Duration::from_secs(0))? {
|
KeyCode::Char('a') => {
|
||||||
match read()? {
|
print!("█");
|
||||||
Event::Key(event) => match event.code {
|
},
|
||||||
KeyCode::Char('q') => {
|
KeyCode::Left => {
|
||||||
panic!("Just quitting, in a way...");
|
stdout().execute(cursor::MoveLeft(1))?;
|
||||||
},
|
},
|
||||||
KeyCode::Char('a') => {
|
KeyCode::Right => {
|
||||||
print!("█");
|
stdout().execute(cursor::MoveRight(1))?;
|
||||||
},
|
},
|
||||||
KeyCode::Left => {
|
KeyCode::Up => {
|
||||||
stdout().execute(cursor::MoveLeft(1))?;
|
stdout().execute(cursor::MoveUp(1))?;
|
||||||
},
|
},
|
||||||
KeyCode::Right => {
|
KeyCode::Down => {
|
||||||
stdout().execute(cursor::MoveRight(1))?;
|
stdout().execute(cursor::MoveDown(1))?;
|
||||||
},
|
},
|
||||||
KeyCode::Up => {
|
_ => {}
|
||||||
stdout().execute(cursor::MoveUp(1))?;
|
|
||||||
},
|
|
||||||
KeyCode::Down => {
|
|
||||||
stdout().execute(cursor::MoveDown(1))?;
|
|
||||||
},
|
},
|
||||||
_ => {}
|
_ => {}
|
||||||
},
|
}
|
||||||
_ => {}
|
|
||||||
}
|
}
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue