2021-06-22 18:40:27 +00:00
|
|
|
mod app;
|
2021-08-28 06:42:44 +00:00
|
|
|
mod cli;
|
2021-07-09 06:51:08 +00:00
|
|
|
mod clipboard;
|
2021-07-07 07:15:18 +00:00
|
|
|
mod components;
|
2021-07-31 16:03:39 +00:00
|
|
|
mod config;
|
2021-08-08 11:57:40 +00:00
|
|
|
mod database;
|
2021-06-22 18:40:27 +00:00
|
|
|
mod event;
|
|
|
|
mod ui;
|
2021-09-06 13:29:18 +00:00
|
|
|
mod version;
|
2021-06-22 18:40:27 +00:00
|
|
|
|
2021-07-08 14:08:04 +00:00
|
|
|
#[macro_use]
|
|
|
|
mod log;
|
|
|
|
|
|
|
|
use crate::app::App;
|
2021-06-22 18:40:27 +00:00
|
|
|
use crate::event::{Event, Key};
|
2021-07-08 16:42:41 +00:00
|
|
|
use anyhow::Result;
|
2021-06-22 18:40:27 +00:00
|
|
|
use crossterm::{
|
|
|
|
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
|
2021-07-08 16:42:41 +00:00
|
|
|
ExecutableCommand,
|
|
|
|
};
|
2021-08-29 03:18:31 +00:00
|
|
|
use std::io;
|
2021-06-22 18:40:27 +00:00
|
|
|
use tui::{backend::CrosstermBackend, Terminal};
|
|
|
|
|
|
|
|
#[tokio::main]
|
|
|
|
async fn main() -> anyhow::Result<()> {
|
2021-08-28 06:42:44 +00:00
|
|
|
let value = crate::cli::parse();
|
|
|
|
let config = config::Config::new(&value.config)?;
|
2021-06-22 18:40:27 +00:00
|
|
|
|
2021-07-08 16:42:41 +00:00
|
|
|
setup_terminal()?;
|
2021-06-22 18:40:27 +00:00
|
|
|
|
2021-08-28 06:42:44 +00:00
|
|
|
let backend = CrosstermBackend::new(io::stdout());
|
2021-06-22 18:40:27 +00:00
|
|
|
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());
|
2021-06-22 18:40:27 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
})?;
|
2021-06-22 18:40:27 +00:00
|
|
|
match events.next()? {
|
2021-07-18 15:50:39 +00:00
|
|
|
Event::Input(key) => match app.event(key).await {
|
|
|
|
Ok(state) => {
|
2021-07-31 16:03:39 +00:00
|
|
|
if !state.is_consumed()
|
|
|
|
&& (key == app.config.key_config.quit || key == app.config.key_config.exit)
|
|
|
|
{
|
2021-07-18 15:50:39 +00:00
|
|
|
break;
|
|
|
|
}
|
2021-07-02 04:09:03 +00:00
|
|
|
}
|
2021-07-31 16:03:39 +00:00
|
|
|
Err(err) => app.error.set(err.to_string())?,
|
2021-07-18 15:50:39 +00:00
|
|
|
},
|
2021-06-22 18:40:27 +00:00
|
|
|
Event::Tick => (),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-08 16:42:41 +00:00
|
|
|
shutdown_terminal();
|
2021-06-22 18:40:27 +00:00
|
|
|
terminal.show_cursor()?;
|
|
|
|
|
|
|
|
Ok(())
|
2021-06-12 04:43:25 +00:00
|
|
|
}
|
2021-07-08 16:42:41 +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);
|
|
|
|
}
|
|
|
|
}
|