about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorBenedikt Peetz <benedikt.peetz@b-peetz.de>2024-08-24 11:37:17 +0200
committerBenedikt Peetz <benedikt.peetz@b-peetz.de>2024-08-24 11:45:31 +0200
commitb0ba7c7d5329148495d9a676c3314a9a35e4ca18 (patch)
tree732f955ea0d5399f4cf20166171615728d237a0a
parentfix(cli/selectCommand): Explicitly set the aliases (diff)
downloadyt-b0ba7c7d5329148495d9a676c3314a9a35e4ca18.tar.gz
yt-b0ba7c7d5329148495d9a676c3314a9a35e4ca18.zip
feat(cli/selectCommand/file): Allow re-use of the previous selection file
-rw-r--r--src/cli.rs9
-rw-r--r--src/main.rs7
-rw-r--r--src/select/mod.rs94
3 files changed, 54 insertions, 56 deletions
diff --git a/src/cli.rs b/src/cli.rs
index 895cfc0..42b6378 100644
--- a/src/cli.rs
+++ b/src/cli.rs
@@ -227,6 +227,10 @@ pub enum SelectCommand {
         /// Include done (watched, dropped) videos
         #[arg(long, short)]
         done: bool,
+
+        /// Use the last selection file (useful if you've spend time on it and want to get it again)
+        #[arg(long, short, conflicts_with = "done")]
+        use_last_selection: bool,
     },
 
     /// Mark the video given by the hash to be watched
@@ -266,7 +270,10 @@ pub enum SelectCommand {
 }
 impl Default for SelectCommand {
     fn default() -> Self {
-        Self::File { done: false }
+        Self::File {
+            done: false,
+            use_last_selection: false,
+        }
     }
 }
 
diff --git a/src/main.rs b/src/main.rs
index 2bd7571..0f22c22 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -97,7 +97,10 @@ async fn main() -> Result<()> {
             let cmd = cmd.unwrap_or(SelectCommand::default());
 
             match cmd {
-                SelectCommand::File { done } => select::select(&app, done).await?,
+                SelectCommand::File {
+                    done,
+                    use_last_selection,
+                } => select::select(&app, done, use_last_selection).await?,
                 _ => handle_select_cmd(&app, cmd, None).await?,
             }
         }
@@ -115,7 +118,6 @@ async fn main() -> Result<()> {
                 dbg!(video);
             }
         },
-
         Command::Update {
             max_backlog,
             subscriptions,
@@ -135,7 +137,6 @@ async fn main() -> Result<()> {
 
             update::update(&app, max_backlog, subscriptions, verbosity).await?;
         }
-
         Command::Subscriptions { cmd } => match cmd {
             SubscriptionCommand::Add { name, url } => {
                 subscribe::subscribe(&app, name, url)
diff --git a/src/select/mod.rs b/src/select/mod.rs
index 2288e1a..695e7ed 100644
--- a/src/select/mod.rs
+++ b/src/select/mod.rs
@@ -33,53 +33,7 @@ use tokio::process::Command;
 pub mod cmds;
 pub mod selection_file;
 
-pub async fn select(app: &App, done: bool) -> Result<()> {
-    let matching_videos = if done {
-        get_videos(
-            app,
-            &[
-                VideoStatus::Pick,
-                //
-                VideoStatus::Watch,
-                VideoStatus::Cached,
-                VideoStatus::Watched,
-                //
-                VideoStatus::Drop,
-                VideoStatus::Dropped,
-            ],
-            None,
-        )
-        .await?
-    } else {
-        get_videos(
-            app,
-            &[
-                VideoStatus::Pick,
-                //
-                VideoStatus::Watch,
-                VideoStatus::Cached,
-            ],
-            None,
-        )
-        .await?
-    };
-
-    // Warmup the cache for the display rendering of the videos.
-    // Otherwise the futures would all try to warm it up at the same time.
-    if let Some(vid) = matching_videos.get(0) {
-        let _ = vid.to_select_file_display(app).await?;
-    }
-
-    let lines: Vec<String> = join_all(
-        matching_videos
-            .iter()
-            .map(|vid| async { vid.to_select_file_display(app).await })
-            .collect::<Vec<_>>(),
-    )
-    .await
-    .into_iter()
-    .collect::<Result<Vec<String>>>()?;
-
+pub async fn select(app: &App, done: bool, use_last_selection: bool) -> Result<()> {
     let temp_file = Builder::new()
         .prefix("yt_video_select-")
         .suffix(".yts")
@@ -87,19 +41,55 @@ pub async fn select(app: &App, done: bool) -> Result<()> {
         .tempfile()
         .context("Failed to get tempfile")?;
 
-    {
+    if use_last_selection {
+        fs::copy(&app.config.paths.last_selection_path, &temp_file)?;
+    } else {
+        let matching_videos = if done {
+            get_videos(app, VideoStatus::ALL, None).await?
+        } else {
+            get_videos(
+                app,
+                &[
+                    VideoStatus::Pick,
+                    //
+                    VideoStatus::Watch,
+                    VideoStatus::Cached,
+                ],
+                None,
+            )
+            .await?
+        };
+
+        // Warmup the cache for the display rendering of the videos.
+        // Otherwise the futures would all try to warm it up at the same time.
+        if let Some(vid) = matching_videos.get(0) {
+            let _ = vid.to_select_file_display(app).await?;
+        }
+
         let mut edit_file = BufWriter::new(&temp_file);
 
-        lines.iter().for_each(|line| {
+        join_all(
+            matching_videos
+                .iter()
+                .map(|vid| async { vid.to_select_file_display(app).await })
+                .collect::<Vec<_>>(),
+        )
+        .await
+        .into_iter()
+        .try_for_each(|line| -> Result<()> {
+            let line = line?;
             edit_file
                 .write_all(line.as_bytes())
                 .expect("This write should not fail");
-        });
 
-        // edit_file.write_all(get_help().await?.as_bytes())?;
+            Ok(())
+        })?;
+
         edit_file.write_all(HELP_STR.as_bytes())?;
         edit_file.flush().context("Failed to flush edit file")?;
+    };
 
+    {
         let editor = env::var("EDITOR").unwrap_or("nvim".to_owned());
 
         let mut nvim = Command::new(editor);
@@ -158,7 +148,7 @@ pub async fn select(app: &App, done: bool) -> Result<()> {
 }
 
 // // FIXME: There should be no reason why we need to re-run yt, just to get the help string. But I've
-// // jet to find a way to do it with out the extra exec <2024-08-20>
+// // yet to find a way to do it with out the extra exec <2024-08-20>
 // async fn get_help() -> Result<String> {
 //     let binary_name = current_exe()?;
 //     let cmd = Command::new(binary_name)