diff --git a/src/grandmother.rs b/src/grandmother.rs index 5347039..9d8b51e 100644 --- a/src/grandmother.rs +++ b/src/grandmother.rs @@ -31,6 +31,11 @@ impl GrandMother { }) } + pub fn demote_to_offline(&mut self) { + let offline_mother = GrandMother::new_offline(self.config.clone()); + (self.parser, self.playlist) = (offline_mother.parser, offline_mother.playlist); + } + pub async fn promote_to_online(&mut self) -> Result<(), Error> { let online_mother = GrandMother::new_online(self.config.clone()).await?; (self.parser, self.playlist) = (online_mother.parser, online_mother.playlist); diff --git a/src/lib.rs b/src/lib.rs index 4a74237..2ff841b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -8,7 +8,10 @@ mod opt; pub mod parser; mod playlist; -use std::{io::{stdin, stdout, Stdin, StdoutLock, Write}, rc::Rc}; +use std::{ + io::{stdin, stdout, Stdin, StdoutLock, Write}, + rc::Rc, +}; use async_recursion::async_recursion; pub use config::Configuration; @@ -65,10 +68,10 @@ pub async fn get_gm( mode: Mode, readline: &mut Readline<'_>, config: Rc, -) -> Result { +) -> Result<(GrandMother, bool), String> { match mode { - Mode::Online => GrandMother::new_online(config).await, - Mode::Offline => Ok(GrandMother::new_offline(config)), + Mode::Online => Ok((GrandMother::new_online(config).await?, true)), + Mode::Offline => Ok((GrandMother::new_offline(config), false)), Mode::Ask => loop { let input = readline .input("Online/Offline mode? [1/2] ") diff --git a/src/main.rs b/src/main.rs index b94481b..d6ce1fb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -31,7 +31,7 @@ async fn main() { " {} is to make entries availibe for offline use later on in the program", "o".bold() ), - format!(" {} is to switch to online mode", "m".bold()), + format!(" {} is to switch between modes (toggle)", "m".bold()), format!(" {} is to perform a new search", "s".bold()), format!(" {} is to select all", "a".bold()), format!(" {} is to toggtle fullscreen for mpv", "f".bold()), @@ -47,7 +47,7 @@ async fn main() { let mut mpv_fs = false; let mut search_result: Option>> = None; let mut readline = Readline::new(); - let gm = get_gm( + let (gm, mut in_online) = get_gm( opt.mode, &mut readline, Rc::new(Configuration::new().expect("Failed to write to configfile")), @@ -100,10 +100,19 @@ async fn main() { continue; } "m" => { - let result = unsafe { get_mut_ref(&gm) }.promote_to_online().await; - if let Err(e) = result { - println!("Failed to switch to onlinemode {:?}", e); + if in_online { + unsafe { get_mut_ref(&gm) }.demote_to_offline(); + println!("Switched to offline mode"); + } else { + let result = unsafe { get_mut_ref(&gm) }.promote_to_online().await; + if let Err(e) = result { + println!("Failed to switch to onlinemode {:?}", e); + } else { + println!("Switched to online mode"); + continue; + } } + in_online = !in_online; continue; } _ => {}