about summary refs log tree commit diff stats
path: root/src/select/cmds.rs
diff options
context:
space:
mode:
authorBenedikt Peetz <benedikt.peetz@b-peetz.de>2024-08-23 18:21:38 +0200
committerBenedikt Peetz <benedikt.peetz@b-peetz.de>2024-08-23 18:24:39 +0200
commit6adacc346a6b0406e69e77a24b204f2e74182471 (patch)
treed7c3c8bceb4be84a8e20703fa4b008f73f8ee3c5 /src/select/cmds.rs
parentfeat(videos): Init (diff)
downloadyt-6adacc346a6b0406e69e77a24b204f2e74182471.tar.gz
yt-6adacc346a6b0406e69e77a24b204f2e74182471.zip
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.
Diffstat (limited to 'src/select/cmds.rs')
-rw-r--r--src/select/cmds.rs85
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
+    }
+}