forked from mars/breed
1
0
Fork 0

Better raw mode and screen handling

This commit is contained in:
mars 2023-04-11 16:04:29 -04:00
parent b03a5b47ea
commit 2b62862559
1 changed files with 15 additions and 10 deletions

View File

@ -1,4 +1,4 @@
use std::io::{stdin, stdout, Read, Write}; use std::io::{stdin, stdout, Read, Stdout, Write};
use crossterm::{cursor, event, terminal, ExecutableCommand, Result}; use crossterm::{cursor, event, terminal, ExecutableCommand, Result};
use event::{Event, KeyCode, KeyEvent}; use event::{Event, KeyCode, KeyEvent};
@ -248,19 +248,24 @@ impl State {
} }
} }
fn main() -> Result<()> { fn screen_main(stdout: &mut Stdout, mut state: State) -> Result<()> {
let text = include_str!("main.rs");
let mut state = State::from_str(text);
let mut stdout = stdout();
terminal::enable_raw_mode()?;
stdout.execute(terminal::EnterAlternateScreen)?;
while !state.quit { while !state.quit {
state.draw(&mut stdout)?; state.draw(stdout)?;
let event = event::read()?; let event = event::read()?;
state.on_event(event); state.on_event(event);
} }
stdout.execute(terminal::LeaveAlternateScreen)?;
Ok(()) Ok(())
} }
fn main() -> Result<()> {
let text = include_str!("main.rs");
let state = State::from_str(text);
let mut stdout = stdout();
terminal::enable_raw_mode()?;
stdout.execute(terminal::EnterAlternateScreen)?;
let result = screen_main(&mut stdout, state);
stdout.execute(terminal::LeaveAlternateScreen)?;
terminal::disable_raw_mode()?;
result
}