Add support for remembering the latest search across sessions

This commit is contained in:
Love 2023-03-01 22:53:09 +01:00
parent 36b79fd01f
commit ec2fba9ae2
2 changed files with 42 additions and 8 deletions

View File

@ -9,7 +9,7 @@ use directories::ProjectDirs;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use serde_json; use serde_json;
use crate::{download_with_progress, Readline}; use crate::{download_with_progress, get_mut_ref, Readline};
const JSON_CONFIG_FILENAME: &'static str = "iptvnator_config.json"; const JSON_CONFIG_FILENAME: &'static str = "iptvnator_config.json";
const APP_IDENTIFIER: [&'static str; 3] = ["com", "billenius", "iptvnator"]; const APP_IDENTIFIER: [&'static str; 3] = ["com", "billenius", "iptvnator"];
@ -58,6 +58,11 @@ impl Conf {
Ok(conf) Ok(conf)
} }
fn write_configfile(&self, path: &Path) -> Result<(), io::Error> {
fs::write(path, serde_json::to_string(&self)?)?;
Ok(())
}
fn user_setup() -> String { fn user_setup() -> String {
let mut readline = Readline::new(); let mut readline = Readline::new();
@ -78,6 +83,7 @@ pub struct Configuration {
pub playlist_path: PathBuf, pub playlist_path: PathBuf,
pub seen_links_path: PathBuf, pub seen_links_path: PathBuf,
pub seen_links: Vec<String>, pub seen_links: Vec<String>,
config_file_path: PathBuf,
} }
impl Configuration { impl Configuration {
@ -86,11 +92,14 @@ impl Configuration {
ProjectDirs::from(APP_IDENTIFIER[0], APP_IDENTIFIER[1], APP_IDENTIFIER[2]).unwrap(); ProjectDirs::from(APP_IDENTIFIER[0], APP_IDENTIFIER[1], APP_IDENTIFIER[2]).unwrap();
let config_dir = project_dirs.config_dir(); let config_dir = project_dirs.config_dir();
let _ = fs::create_dir_all(config_dir); let _ = fs::create_dir_all(config_dir);
let config_file = config_dir.join(JSON_CONFIG_FILENAME); let config_file_path = config_dir.join(JSON_CONFIG_FILENAME).to_path_buf();
let configuration = Conf::new(&config_file)?; let configuration = Conf::new(&config_file_path)?;
fs::write(config_file, serde_json::to_string(&configuration).unwrap())?; fs::write(
&config_file_path,
serde_json::to_string(&configuration).unwrap(),
)?;
// Setup dirs for playlist // Setup dirs for playlist
let cache_dir = project_dirs.cache_dir().to_path_buf(); let cache_dir = project_dirs.cache_dir().to_path_buf();
@ -105,9 +114,18 @@ impl Configuration {
playlist_path, playlist_path,
seen_links, seen_links,
seen_links_path, seen_links_path,
config_file_path,
}) })
} }
pub fn update_last_search_ugly(&self, last_search: Option<String>) {
unsafe { get_mut_ref(&self.conf).last_search = last_search }
if let Err(e) = self.write_configfile(&self.config_file_path) {
println!("Failed to write to configfile, {:?}", e);
}
}
fn get_watched(path: &Path) -> Option<Vec<String>> { fn get_watched(path: &Path) -> Option<Vec<String>> {
let reader = BufReader::new(File::open(&path).ok()?); let reader = BufReader::new(File::open(&path).ok()?);
serde_json::from_reader(reader).ok() serde_json::from_reader(reader).ok()

View File

@ -13,8 +13,8 @@ async fn main() {
"BLAZINGLY FAST".italic() "BLAZINGLY FAST".italic()
); );
println!( println!(
"There will be some options along the way \n {} is to refresh the local iptvfile.\n {} is to quit and save watched fields\n {} is to download fields\n {} is to perform a new search\n {} is to select all\n {} is to toggtle fullscreen for mpv", "There will be some options along the way \n {} is to refresh the local iptvfile.\n {} is to quit and save watched fields\n {} is to download fields\n {} is to perform a new search\n {} is to select all\n {} is to toggtle fullscreen for mpv\n {} is to redo the last search (mainly for use in the last session)\n {} to forget the latest search",
"r".bold(),"q".bold(),"d".bold(),"s".bold(),"a".bold(), "f".bold() "r".bold(),"q".bold(),"d".bold(),"s".bold(),"a".bold(), "f".bold(),"l".bold(),"c".bold()
); );
let config = Rc::new(Configuration::new().expect("Failed to write to configfile")); let config = Rc::new(Configuration::new().expect("Failed to write to configfile"));
@ -28,8 +28,10 @@ async fn main() {
loop { loop {
// Dont't perform a search if user has just watched, instead present the previous search // Dont't perform a search if user has just watched, instead present the previous search
if search_result.is_none() { if search_result.is_none() {
let search = readline.input("Search by name [ r/q/f ]: ").to_lowercase(); let search = readline
let search = search.trim(); .input("Search by name [ r/q/f/l/c ]: ")
.to_lowercase();
let mut search = search.trim();
// Special commands // Special commands
match search { match search {
@ -50,14 +52,28 @@ async fn main() {
); );
continue; continue;
} }
"l" => {
search = if let Some(s) = config.last_search.as_ref() {
s
} else {
println!("There is no search saved from earlier");
continue;
};
}
"c" => {
config.update_last_search_ugly(None);
continue;
}
_ => {} _ => {}
} }
search_result = Some(Rc::new(parser.find(search))); search_result = Some(Rc::new(parser.find(search)));
if search_result.as_ref().unwrap().is_empty() { if search_result.as_ref().unwrap().is_empty() {
println!("Nothing found"); println!("Nothing found");
search_result = None;
continue; continue;
} }
config.update_last_search_ugly(Some(search.to_owned()));
} }
// Let them choose which one to stream // Let them choose which one to stream