gobang/src/main.rs

85 lines
2.0 KiB
Rust
Raw Normal View History

mod app;
mod cli;
mod clipboard;
mod components;
mod config;
mod database;
mod event;
mod ui;
2021-09-06 13:29:18 +00:00
mod version;
#[macro_use]
mod log;
use crate::app::App;
use crate::event::{Event, Key};
use anyhow::Result;
use crossterm::{
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
ExecutableCommand,
};
2021-08-29 03:18:31 +00:00
use std::io;
use tui::{backend::CrosstermBackend, Terminal};
#[tokio::main]
async fn main() -> anyhow::Result<()> {
let value = crate::cli::parse();
let config = config::Config::new(&value.config)?;
setup_terminal()?;
let backend = CrosstermBackend::new(io::stdout());
let mut terminal = Terminal::new(backend)?;
let events = event::Events::new(250);
2021-09-06 07:10:06 +00:00
let mut app = App::new(config.clone());
terminal.clear()?;
loop {
2021-09-06 07:10:06 +00:00
terminal.draw(|f| {
if let Err(err) = app.draw(f) {
outln!(config#Error, "error: {}", err.to_string());
std::process::exit(1);
}
})?;
match events.next()? {
Event::Input(key) => match app.event(key).await {
Ok(state) => {
if !state.is_consumed()
&& (key == app.config.key_config.quit || key == app.config.key_config.exit)
{
break;
}
2021-07-02 04:09:03 +00:00
}
Err(err) => app.error.set(err.to_string())?,
},
Event::Tick => (),
}
}
shutdown_terminal();
terminal.show_cursor()?;
Ok(())
2021-06-12 04:43:25 +00:00
}
fn setup_terminal() -> Result<()> {
enable_raw_mode()?;
io::stdout().execute(EnterAlternateScreen)?;
Ok(())
}
fn shutdown_terminal() {
let leave_screen = io::stdout().execute(LeaveAlternateScreen).map(|_f| ());
if let Err(e) = leave_screen {
eprintln!("leave_screen failed:\n{}", e);
}
let leave_raw_mode = disable_raw_mode();
if let Err(e) = leave_raw_mode {
eprintln!("leave_raw_mode failed:\n{}", e);
}
}