about summary refs log tree commit diff stats
path: root/crates
diff options
context:
space:
mode:
Diffstat (limited to 'crates')
-rw-r--r--crates/yt/src/select/mod.rs126
1 files changed, 75 insertions, 51 deletions
diff --git a/crates/yt/src/select/mod.rs b/crates/yt/src/select/mod.rs
index 8db9ae3..45aa05c 100644
--- a/crates/yt/src/select/mod.rs
+++ b/crates/yt/src/select/mod.rs
@@ -50,64 +50,71 @@ pub async fn select(app: &App, done: bool, use_last_selection: bool) -> Result<(
     if use_last_selection {
         fs::copy(&app.config.paths.last_selection_path, &temp_file)?;
     } else {
-        let matching_videos = if done {
-            get::videos(app, VideoStatusMarker::ALL).await?
-        } else {
-            get::videos(
-                app,
-                &[
-                    VideoStatusMarker::Pick,
-                    //
-                    VideoStatusMarker::Watch,
-                    VideoStatusMarker::Cached,
-                ],
-            )
-            .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.first() {
-            drop(vid.to_line_display(app).await?);
-        }
+        let matching_videos = get_videos(app, done).await?;
 
-        let mut edit_file = BufWriter::new(&temp_file);
-
-        matching_videos
-            .into_iter()
-            .map(|vid| to_select_file_display_owned(vid, app))
-            .collect::<FuturesOrdered<_>>()
-            .try_collect::<Vec<String>>()
-            .await?
-            .into_iter()
-            .try_for_each(|line| -> Result<()> {
-                edit_file
-                    .write_all(line.as_bytes())
-                    .context("Failed to write to `edit_file`")?;
-
-                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);
-        nvim.arg(temp_file.path());
-        let status = nvim.status().await.context("Falied to run nvim")?;
-        if !status.success() {
-            bail!("nvim exited with error status: {}", status)
-        }
+        write_videos_to_file(app, temp_file.as_file(), &matching_videos).await?;
     }
 
+    open_editor_at(temp_file.path()).await?;
+
     let read_file = temp_file.reopen()?;
     fs::copy(temp_file.path(), &app.config.paths.last_selection_path)
         .context("Failed to persist selection file")?;
 
-    let reader = BufReader::new(&read_file);
+    process_file(app, &read_file).await?;
+
+    Ok(())
+}
+
+async fn get_videos(app: &App, include_done: bool) -> Result<Vec<Video>> {
+    if include_done {
+        get::videos(app, VideoStatusMarker::ALL).await
+    } else {
+        get::videos(
+            app,
+            &[
+                VideoStatusMarker::Pick,
+                //
+                VideoStatusMarker::Watch,
+                VideoStatusMarker::Cached,
+            ],
+        )
+        .await
+    }
+}
+
+async fn write_videos_to_file(app: &App, file: &File, videos: &[Video]) -> Result<()> {
+    // Warm-up 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) = videos.first() {
+        drop(vid.to_line_display(app).await?);
+    }
+
+    let mut edit_file = BufWriter::new(file);
+
+    videos
+        .iter()
+        .map(|vid| vid.to_select_file_display(app))
+        .collect::<FuturesOrdered<_>>()
+        .try_collect::<Vec<String>>()
+        .await?
+        .into_iter()
+        .try_for_each(|line| -> Result<()> {
+            edit_file
+                .write_all(line.as_bytes())
+                .context("Failed to write to `edit_file`")?;
+
+            Ok(())
+        })?;
+
+    edit_file.write_all(HELP_STR.as_bytes())?;
+    edit_file.flush().context("Failed to flush edit file")?;
+
+    Ok(())
+}
+
+async fn process_file(app: &App, file: &File) -> Result<()> {
+    let reader = BufReader::new(file);
 
     let mut line_number = 0;
     for line in reader.lines() {
@@ -152,6 +159,23 @@ pub async fn select(app: &App, done: bool, use_last_selection: bool) -> Result<(
     Ok(())
 }
 
+async fn open_editor_at(path: &Path) -> Result<()> {
+    let editor = env::var("EDITOR").unwrap_or("nvim".to_owned());
+
+    let mut nvim = Command::new(&editor);
+    nvim.arg(path);
+    let status = nvim
+        .status()
+        .await
+        .with_context(|| format!("Falied to run editor: {editor}"))?;
+
+    if status.success() {
+        Ok(())
+    } else {
+        bail!("Editor ({editor}) exited with error status: {}", status)
+    }
+}
+
 // // FIXME: There should be no reason why we need to re-run yt, just to get the help string. But I've
 // // yet to find a way to do it without the extra exec <2024-08-20>
 // async fn get_help() -> Result<String> {