Add support for remembering the latest search across sessions

This commit is contained in:
Love 2023-03-01 22:53:09 +01:00
parent 0d02826b77
commit 3171e5568d
2 changed files with 42 additions and 8 deletions

View File

@ -9,7 +9,7 @@ use directories::ProjectDirs;
use serde::{Deserialize, Serialize};
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 APP_IDENTIFIER: [&'static str; 3] = ["com", "billenius", "iptvnator"];
@ -58,6 +58,11 @@ impl 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 {
let mut readline = Readline::new();
@ -78,6 +83,7 @@ pub struct Configuration {
pub playlist_path: PathBuf,
pub seen_links_path: PathBuf,
pub seen_links: Vec<String>,
config_file_path: PathBuf,
}
impl Configuration {
@ -86,11 +92,14 @@ impl Configuration {
ProjectDirs::from(APP_IDENTIFIER[0], APP_IDENTIFIER[1], APP_IDENTIFIER[2]).unwrap();
let config_dir = project_dirs.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
let cache_dir = project_dirs.cache_dir().to_path_buf();
@ -105,9 +114,18 @@ impl Configuration {
playlist_path,
seen_links,
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>> {
let reader = BufReader::new(File::open(&path).ok()?);
serde_json::from_reader(reader).ok()

View File

@ -13,8 +13,8 @@ async fn main() {
"BLAZINGLY FAST".italic()
);
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",
"r".bold(),"q".bold(),"d".bold(),"s".bold(),"a".bold(), "f".bold()
"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(),"l".bold(),"c".bold()
);
let config = Rc::new(Configuration::new().expect("Failed to write to configfile"));
@ -28,8 +28,10 @@ async fn main() {
loop {
// Dont't perform a search if user has just watched, instead present the previous search
if search_result.is_none() {
let search = readline.input("Search by name [ r/q/f ]: ").to_lowercase();
let search = search.trim();
let search = readline
.input("Search by name [ r/q/f/l/c ]: ")
.to_lowercase();
let mut search = search.trim();
// Special commands
match search {
@ -50,14 +52,28 @@ async fn main() {
);
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)));
if search_result.as_ref().unwrap().is_empty() {
println!("Nothing found");
search_result = None;
continue;
}
config.update_last_search_ugly(Some(search.to_owned()));
}
// Let them choose which one to stream