diff --git a/src/app.rs b/src/app.rs index c431dba..42733f3 100644 --- a/src/app.rs +++ b/src/app.rs @@ -82,7 +82,6 @@ pub struct App { pub pending_list_state: ListState, /// UI state: active pane - #[serde(skip)] pub active_pane: ActivePane, pub snowballing_iteration: usize, diff --git a/src/crossterm.rs b/src/crossterm.rs index fc180af..7f2f592 100644 --- a/src/crossterm.rs +++ b/src/crossterm.rs @@ -15,7 +15,7 @@ use ratatui::{ use crate::{app::App, ui}; -pub fn run(app: App) -> Result<(), Box> { +pub fn run(app: App) -> Result> { // setup terminal enable_raw_mode()?; let mut stdout = io::stdout(); @@ -35,18 +35,17 @@ pub fn run(app: App) -> Result<(), Box> { )?; terminal.show_cursor()?; - if let Err(err) = app_result { + if let Err(err) = &app_result { println!("{err:?}"); } - Ok(()) + Ok(app_result?) } -// TODO: Implement save on quit fn run_app( terminal: &mut Terminal, mut app: App, -) -> io::Result<()> +) -> io::Result where io::Error: From, { @@ -60,26 +59,7 @@ where } if app.should_quit { - return Ok(()); + return Ok(app); } } } - -// pub fn run_app( -// terminal: &mut Terminal, -// mut app: App, -// ) -> io::Result -// where -// io::Error: From, -// { -// loop { -// terminal.draw(|f| ui(f, &app))?; -// -// if let Event::Key(key) = event::read()? { -// app.handle_key(key.code); -// if app.should_quit { -// return Ok(app); -// } -// } -// } -// } diff --git a/src/main.rs b/src/main.rs index d27e17c..4c6b87d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -23,7 +23,7 @@ mod snowballing; // Ok(()) // } -fn load_savefile(filename: &String) -> Result { +fn deserialize_savefile(filename: &String) -> Result { match std::fs::read_to_string(filename) { Ok(content) => { let mut app: App = serde_json::from_str(&content)?; @@ -40,6 +40,18 @@ fn load_savefile(filename: &String) -> Result { } } +fn serialize_savefile( + app: &App, + filename: &String, +) -> Result<(), serde_json::Error> { + if let Ok(serialized) = serde_json::to_string_pretty(&app) { + std::fs::write(filename, serialized) + .expect("We can't really deal with io errors ourselves"); + } + + Ok(()) +} + use clap::Parser; mod crossterm; use std::error::Error; @@ -54,9 +66,11 @@ struct Args { fn main() -> Result<(), Box> { let args = Args::parse(); - let app = load_savefile(&args.savefile)?; + let starting_app_state = deserialize_savefile(&args.savefile)?; - crate::crossterm::run(app)?; + let final_app_state = crate::crossterm::run(starting_app_state)?; + + serialize_savefile(&final_app_state, &args.savefile)?; Ok(()) }