added light novel tracking

This commit is contained in:
Zastian Pretorius
2022-10-29 18:16:09 +01:00
parent e9de97e4da
commit 8363c57f73
3 changed files with 65 additions and 3 deletions

View File

@@ -1,13 +1,13 @@
use crate::ln::open_text::*; use crate::ln::open_text::*;
use crate::ln::scraper::*; use crate::ln::scraper::*;
use std::fs::File; use crate::ln::tracker::*;
use std::io::Write;
use crossterm::{ use crossterm::{
event::{self, DisableMouseCapture, EnableMouseCapture, Event, KeyCode}, event::{self, DisableMouseCapture, EnableMouseCapture, Event, KeyCode},
execute, execute,
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen}, terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
}; };
use std::fs::File;
use std::io::Write;
use std::{error::Error, io}; use std::{error::Error, io};
use tui::{ use tui::{
backend::{Backend, CrosstermBackend}, backend::{Backend, CrosstermBackend},
@@ -115,6 +115,7 @@ impl<'a> App {
pub fn ln_ui(chapter: u32) -> Result<(), Box<dyn Error>> { pub fn ln_ui(chapter: u32) -> Result<(), Box<dyn Error>> {
// setup terminal // setup terminal
let _ = get_json();
enable_raw_mode()?; enable_raw_mode()?;
let mut stdout = io::stdout(); let mut stdout = io::stdout();
execute!(stdout, EnterAlternateScreen, EnableMouseCapture)?; execute!(stdout, EnterAlternateScreen, EnableMouseCapture)?;
@@ -203,6 +204,11 @@ fn run_app<B: Backend>(terminal: &mut Terminal<B>, mut app: App) -> io::Result<(
.nth(selected.unwrap()) .nth(selected.unwrap())
.unwrap() .unwrap()
.to_string(); .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 link = app.ln_links[selected.unwrap()].to_string();
let html = get_html(&link); let html = get_html(&link);
app.ln_id = get_ln_id(&html).to_string(); app.ln_id = get_ln_id(&html).to_string();
@@ -242,6 +248,11 @@ fn run_app<B: Backend>(terminal: &mut Terminal<B>, mut app: App) -> io::Result<(
}; };
terminal.clear()?; terminal.clear()?;
let _ = open_bat(); let _ = open_bat();
write_ln_progress(
&app.title,
&app.current_page_number,
&app.messages.state.selected().unwrap(),
);
terminal.clear()?; terminal.clear()?;
} }
} }

View File

@@ -1,3 +1,4 @@
pub mod ln; pub mod ln;
pub mod open_text; pub mod open_text;
pub mod scraper; pub mod scraper;
pub mod tracker;

50
src/ln/tracker.rs Normal file
View File

@@ -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)
}