From 6adacc346a6b0406e69e77a24b204f2e74182471 Mon Sep 17 00:00:00 2001 From: Benedikt Peetz Date: Fri, 23 Aug 2024 18:21:38 +0200 Subject: fix(select/cmds): Accept the watch flags for every command This makes it easier to change the status of a video, without having to painstakingly remove the flags too. --- src/select/cmds.rs | 85 +++++++++++++++++++------------------- src/select/selection_file/help.str | 6 +-- 2 files changed, 45 insertions(+), 46 deletions(-) (limited to 'src/select') diff --git a/src/select/cmds.rs b/src/select/cmds.rs index c480bd9..2b36fe8 100644 --- a/src/select/cmds.rs +++ b/src/select/cmds.rs @@ -10,7 +10,7 @@ use crate::{ app::App, - cli::SelectCommand, + cli::{SelectCommand, SharedSelectionCommandArgs}, storage::video_database::{ getters::get_video_by_hash, setters::{set_video_options, set_video_status}, @@ -25,57 +25,25 @@ pub async fn handle_select_cmd( cmd: SelectCommand, line_number: Option, ) -> Result<()> { - fn compute_priority(line_number: Option, priority: Option) -> Option { - if let Some(pri) = priority { - Some(pri) - } else if let Some(pri) = line_number { - Some(pri) - } else { - None - } - } - match cmd { SelectCommand::Pick { shared } => { - let priority = compute_priority(line_number, shared.priority); - set_video_status( - app, - &shared.hash.realize(app).await?, - VideoStatus::Pick, - priority, - ) - .await? + handle_status_change(app, shared, line_number, VideoStatus::Pick).await?; } SelectCommand::Drop { shared } => { - let priority = compute_priority(line_number, shared.priority); - set_video_status( - app, - &shared.hash.realize(app).await?, - VideoStatus::Drop, - priority, - ) - .await? + handle_status_change(app, shared, line_number, VideoStatus::Drop).await?; } - SelectCommand::Watch { - shared, - subtitle_langs, - speed, - } => { - let hash = shared.hash.realize(&app).await?; - let video = get_video_by_hash(app, &hash).await?; - let video_options = VideoOptions::new( - subtitle_langs.unwrap_or(app.config.select.subtitle_langs.clone()), - speed.unwrap_or(app.config.select.playback_speed), - ); - let priority = compute_priority(line_number, shared.priority); + SelectCommand::Watched { shared } => { + handle_status_change(app, shared, line_number, VideoStatus::Watched).await?; + } + SelectCommand::Watch { shared } => { + let hash = shared.hash.clone().realize(&app).await?; + let video = get_video_by_hash(app, &hash).await?; if let Some(_) = video.cache_path { - set_video_status(app, &hash, VideoStatus::Cached, priority).await?; + handle_status_change(app, shared, line_number, VideoStatus::Cached).await?; } else { - set_video_status(app, &hash, VideoStatus::Watch, priority).await?; + handle_status_change(app, shared, line_number, VideoStatus::Watch).await?; } - - set_video_options(app, hash, &video_options).await?; } SelectCommand::Url { shared } => { @@ -88,3 +56,34 @@ pub async fn handle_select_cmd( } Ok(()) } + +async fn handle_status_change( + app: &App, + shared: SharedSelectionCommandArgs, + line_number: Option, + new_status: VideoStatus, +) -> Result<()> { + let hash = shared.hash.realize(&app).await?; + let video_options = VideoOptions::new( + shared + .subtitle_langs + .unwrap_or(app.config.select.subtitle_langs.clone()), + shared.speed.unwrap_or(app.config.select.playback_speed), + ); + let priority = compute_priority(line_number, shared.priority); + + set_video_status(app, &hash, new_status, priority).await?; + set_video_options(app, &hash, &video_options).await?; + + Ok(()) +} + +fn compute_priority(line_number: Option, priority: Option) -> Option { + if let Some(pri) = priority { + Some(pri) + } else if let Some(pri) = line_number { + Some(pri) + } else { + None + } +} diff --git a/src/select/selection_file/help.str b/src/select/selection_file/help.str index 295bc4d..9f4a896 100644 --- a/src/select/selection_file/help.str +++ b/src/select/selection_file/help.str @@ -1,8 +1,8 @@ # Commands: # w, watch [-p,-s,-l] Mark the video given by the hash to be watched -# d, drop [-p] Mark the video given by the hash to be dropped -# u, url [-p] Open the video URL in Firefox's `timesinks.youtube` profile -# p, pick [-p] Reset the videos status to 'Pick' +# d, drop [-p,-s,-l] Mark the video given by the hash to be dropped +# u, url [-p,-s,-l] Open the video URL in Firefox's `timesinks.youtube` profile +# p, pick [-p,-s,-l] Reset the videos status to 'Pick' # # See `yt select --help` for more help. # -- cgit 1.4.1