diff options
Diffstat (limited to '')
-rw-r--r-- | src/select/cmds.rs | 85 |
1 files changed, 42 insertions, 43 deletions
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<i64>, ) -> Result<()> { - fn compute_priority(line_number: Option<i64>, priority: Option<i64>) -> Option<i64> { - 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<i64>, + 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<i64>, priority: Option<i64>) -> Option<i64> { + if let Some(pri) = priority { + Some(pri) + } else if let Some(pri) = line_number { + Some(pri) + } else { + None + } +} |