sheetsui/src/main.rs

64 lines
1.6 KiB
Rust
Raw Normal View History

2024-10-30 14:34:45 -04:00
use std::{path::PathBuf, process::ExitCode};
2024-10-23 16:14:18 -04:00
use clap::Parser;
use crossterm::event;
2024-10-29 19:47:50 -04:00
use ratatui;
2024-11-30 19:43:22 -05:00
use serde_json::to_writer;
use std::io::Write;
2024-10-30 14:34:45 -04:00
use ui::Workspace;
2024-11-11 19:27:59 -05:00
mod book;
2024-11-23 21:53:13 -05:00
mod ui;
#[derive(Parser, Debug)]
#[command(version, about, long_about = None)]
pub struct Args {
#[arg()]
workbook: PathBuf,
2024-11-16 10:51:38 -05:00
#[arg(default_value_t=String::from("en"), short, long)]
locale_name: String,
#[arg(default_value_t=String::from("America/New_York"), short, long)]
timezone_name: String,
2024-11-30 19:43:22 -05:00
#[arg(long)]
log_input: Option<PathBuf>,
}
2024-11-30 19:43:22 -05:00
type ReadFn = Box<dyn FnMut() -> anyhow::Result<event::Event>>;
2024-11-16 10:51:38 -05:00
fn run(terminal: &mut ratatui::DefaultTerminal, args: Args) -> anyhow::Result<ExitCode> {
let mut ws = Workspace::load(&args.workbook, &args.locale_name, &args.timezone_name)?;
2024-11-30 19:43:22 -05:00
let mut read_func: ReadFn = if let Some(log_path) = args.log_input {
{
let log_file = std::fs::File::create(log_path)?;
Box::new(move || {
let evt = event::read()?;
to_writer(&log_file, &evt)?;
writeln!(&log_file, "")?;
Ok(evt)
})
}
} else {
Box::new(|| {
let evt = event::read()?;
Ok(evt)
})
};
loop {
terminal.draw(|frame| ui::render::draw(frame, &mut ws))?;
2024-11-30 19:43:22 -05:00
if let Some(code) = ws.handle_input(read_func()?)? {
2024-10-30 14:34:45 -04:00
return Ok(code);
}
}
}
2024-10-30 14:34:45 -04:00
fn main() -> anyhow::Result<ExitCode> {
2024-10-29 19:47:50 -04:00
let args = Args::parse();
let mut terminal = ratatui::init();
terminal.clear()?;
2024-11-16 10:51:38 -05:00
let app_result = run(&mut terminal, args);
ratatui::restore();
app_result
2024-10-23 16:14:18 -04:00
}