diff --git a/src/ln/ln.rs b/src/ln/ln.rs index 8d3c230..e445bb8 100644 --- a/src/ln/ln.rs +++ b/src/ln/ln.rs @@ -1,13 +1,13 @@ use crate::ln::open_text::*; use crate::ln::scraper::*; -use std::fs::File; -use std::io::Write; - +use crate::ln::tracker::*; use crossterm::{ event::{self, DisableMouseCapture, EnableMouseCapture, Event, KeyCode}, execute, terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen}, }; +use std::fs::File; +use std::io::Write; use std::{error::Error, io}; use tui::{ backend::{Backend, CrosstermBackend}, @@ -115,6 +115,7 @@ impl<'a> App { pub fn ln_ui(chapter: u32) -> Result<(), Box> { // setup terminal + let _ = get_json(); enable_raw_mode()?; let mut stdout = io::stdout(); execute!(stdout, EnterAlternateScreen, EnableMouseCapture)?; @@ -203,6 +204,11 @@ fn run_app(terminal: &mut Terminal, mut app: App) -> io::Result<( .nth(selected.unwrap()) .unwrap() .to_string(); + if app.current_page_number == 1 { + let progress = get_ln_progress(&app.title); + app.current_page_number = progress.0; + app.messages.state.select(Some(progress.1)); + } let link = app.ln_links[selected.unwrap()].to_string(); let html = get_html(&link); app.ln_id = get_ln_id(&html).to_string(); @@ -242,6 +248,11 @@ fn run_app(terminal: &mut Terminal, mut app: App) -> io::Result<( }; terminal.clear()?; let _ = open_bat(); + write_ln_progress( + &app.title, + &app.current_page_number, + &app.messages.state.selected().unwrap(), + ); terminal.clear()?; } } diff --git a/src/ln/mod.rs b/src/ln/mod.rs index 2960c4c..d1879ca 100644 --- a/src/ln/mod.rs +++ b/src/ln/mod.rs @@ -1,3 +1,4 @@ pub mod ln; pub mod open_text; pub mod scraper; +pub mod tracker; diff --git a/src/ln/tracker.rs b/src/ln/tracker.rs new file mode 100644 index 0000000..b379559 --- /dev/null +++ b/src/ln/tracker.rs @@ -0,0 +1,50 @@ +use serde_json; +use std::fs; +// +// +pub fn get_json() -> serde_json::Value { + let config_path = dirs::config_dir().unwrap().join("kami"); + if !config_path.exists() { + fs::create_dir_all(&config_path).unwrap(); + } + let json_path = config_path.join("ln_progress.json"); + if !json_path.exists() { + fs::File::create(&json_path).unwrap(); + } + let json = fs::read_to_string(&json_path).unwrap(); + let json: serde_json::Value = serde_json::from_str(&json).unwrap_or(serde_json::Value::Null); + json +} + +pub fn write_ln_progress(title: &str, current_page: &u32, selected: &usize) { + let config_path = dirs::config_dir().unwrap().join("kami"); + let json_path = config_path.join("ln_progress.json"); + let json = fs::read_to_string(&json_path).unwrap(); + let mut json: serde_json::Value = + serde_json::from_str(&json).unwrap_or(serde_json::Value::Null); + let mut title_json = serde_json::Map::new(); + title_json.insert( + "current_page".to_string(), + serde_json::Value::from(current_page.clone()), + ); + title_json.insert( + "selected".to_string(), + serde_json::Value::from(selected.clone()), + ); + //insert title_json into json + if json[title].is_null() { + json[title] = serde_json::Value::from(title_json); + } else { + json[title]["current_page"] = serde_json::Value::from(current_page.clone()); + json[title]["selected"] = serde_json::Value::from(selected.clone()); + } + let json = serde_json::to_string_pretty(&json).unwrap(); + fs::write(&json_path, json).unwrap(); +} + +pub fn get_ln_progress(title: &str) -> (u32, usize) { + let json = get_json(); + let current_page = json[title]["current_page"].as_u64().unwrap_or(1) as u32; + let selected = json[title]["selected"].as_u64().unwrap_or(0) as usize; + (current_page, selected) +}