about summary refs log tree commit diff stats
path: root/src/select
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 /src/select
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
Diffstat (limited to '')
-rw-r--r--src/select/mod.rs94
1 files changed, 42 insertions, 52 deletions
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)