Rewrite the structure of the gui and cli interfaces
Added a more modular way to add windows
This commit is contained in:
55
src/ui/cli/add.rs
Normal file
55
src/ui/cli/add.rs
Normal file
@@ -0,0 +1,55 @@
|
||||
use std::str::FromStr;
|
||||
|
||||
use anyhow::bail;
|
||||
|
||||
use crate::{config::ConfigWrapper, downloader::Downloader, manifest::{song::Song, Manifest}, util::is_supported_host};
|
||||
|
||||
|
||||
|
||||
pub async fn add(cfg: &ConfigWrapper, manifest: &mut Manifest, downloader: &mut Downloader, url: &String, name: &String, playlist: &String) -> anyhow::Result<()> {
|
||||
|
||||
let mut playlists = manifest.get_playlists().keys().map(|f| f.clone()).collect::<Vec<String>>();
|
||||
|
||||
playlists.sort();
|
||||
|
||||
if !is_supported_host(url::Url::from_str(&url)?) {
|
||||
log::error!("Invalid or unsupported host name");
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
|
||||
|
||||
let song = Song::from_url_str(url.clone())?;
|
||||
manifest.add_song(playlist, name.clone(), song.clone());
|
||||
manifest.save(None)?;
|
||||
|
||||
let should_download = crate::prompt::prompt_bool("Download song now?", Some(false));
|
||||
|
||||
if should_download {
|
||||
downloader.download_song(cfg, &name, &song, &playlist, manifest.get_format())?;
|
||||
crate::process_manager::wait_for_procs_untill(0)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn add_playlist(cfg: &ConfigWrapper, manifest: &mut Manifest, downloader: &mut Downloader, url: &String, name: &String) -> anyhow::Result<()> {
|
||||
let songs = downloader.download_playlist_nb(cfg, url, name, manifest.get_format())?;
|
||||
|
||||
if manifest.get_playlist(name).is_some() {
|
||||
log::error!("Playlist {name} already exists");
|
||||
bail!("")
|
||||
}
|
||||
|
||||
manifest.add_playlist(name.clone());
|
||||
|
||||
let playlist = manifest.get_playlist_mut(name).expect("Unreachable");
|
||||
|
||||
for (sname, song) in songs {
|
||||
playlist.add_song(sname, song);
|
||||
}
|
||||
manifest.save(None)?;
|
||||
|
||||
while downloader.download_all_nb_poll(cfg)?.is_some() {};
|
||||
Ok(())
|
||||
}
|
||||
46
src/ui/cli/mod.rs
Normal file
46
src/ui/cli/mod.rs
Normal file
@@ -0,0 +1,46 @@
|
||||
mod add;
|
||||
|
||||
use crate::{config::{cli::CliCommand, ConfigWrapper}, downloader::Downloader, manifest::Manifest, ui::gui};
|
||||
|
||||
|
||||
|
||||
pub async fn command_run(cfg: &ConfigWrapper, manifest: &mut Manifest) -> anyhow::Result<()> {
|
||||
log::info!("Is in term: {}", cfg.isatty);
|
||||
//std::fs::write("./isatty", format!("{}\n", cfg.isatty))?;
|
||||
|
||||
let mut downloader = Downloader::new();
|
||||
match (&cfg.cli.command, cfg.isatty) {
|
||||
(None | Some(CliCommand::Download), true) => {
|
||||
match downloader.download_all(manifest, &cfg).await {
|
||||
Ok(count) => log::info!("Downloaded {count} songs"),
|
||||
Err(e) => {
|
||||
log::error!("Failed to download songs: {e}");
|
||||
return Ok(());
|
||||
}
|
||||
}
|
||||
},
|
||||
(Some(c), _) => {
|
||||
match c {
|
||||
CliCommand::Download => unreachable!(),
|
||||
CliCommand::AddPlaylist { url, name } => {
|
||||
if let Err(e) = add::add_playlist(cfg, manifest, &mut downloader, url, name).await {
|
||||
log::error!("Failed to run 'add-playlist' commmand: {e}");
|
||||
}
|
||||
}
|
||||
CliCommand::Add { url, name, playlist } => {
|
||||
if let Err(e) = add::add(cfg, manifest, &mut downloader, url, name, playlist).await {
|
||||
log::error!("Failed to run 'add' command: {e}");
|
||||
}
|
||||
}
|
||||
CliCommand::Gui => {
|
||||
gui::Gui::start(manifest.clone(), downloader, cfg.clone())?;
|
||||
},
|
||||
}
|
||||
}
|
||||
(None, false) => {
|
||||
gui::Gui::start(manifest.clone(), downloader, cfg.clone())?;
|
||||
},
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
Reference in New Issue
Block a user