about summary refs log tree commit diff stats
path: root/sys/nixpkgs/pkgs/yt/src/bin
diff options
context:
space:
mode:
authorSoispha <soispha@vhack.eu>2024-01-20 15:52:39 +0100
committerSoispha <soispha@vhack.eu>2024-01-20 15:57:56 +0100
commitba3273c9adffee3d8538aecee18b39374bf6e195 (patch)
treeeb292946e997102f0340f85a26f65f7f0f839f67 /sys/nixpkgs/pkgs/yt/src/bin
parentfix(sys/nixpkgs/yt/{yt,ytc}): Ignore sponsor block API access errors (diff)
downloadnixos-config-ba3273c9adffee3d8538aecee18b39374bf6e195.tar.gz
nixos-config-ba3273c9adffee3d8538aecee18b39374bf6e195.zip
feat(sys/nixpkgs/yt/{yt,ytc}): Persist old selection file
Diffstat (limited to 'sys/nixpkgs/pkgs/yt/src/bin')
-rw-r--r--sys/nixpkgs/pkgs/yt/src/bin/yt/main.rs73
1 files changed, 35 insertions, 38 deletions
diff --git a/sys/nixpkgs/pkgs/yt/src/bin/yt/main.rs b/sys/nixpkgs/pkgs/yt/src/bin/yt/main.rs
index 54d89daa..ae1bcacd 100644
--- a/sys/nixpkgs/pkgs/yt/src/bin/yt/main.rs
+++ b/sys/nixpkgs/pkgs/yt/src/bin/yt/main.rs
@@ -1,14 +1,14 @@
 use anyhow::{bail, Context, Result};
 use std::{
-    env,
+    env, fs,
     io::{BufRead, BufReader, BufWriter, Write},
     process::Command as StdCmd,
 };
-use tempfile::NamedTempFile;
+use tempfile::Builder;
 use yt::{
-    constants::HELP_STR,
+    constants::{last_select, HELP_STR},
     downloader::{Downloadable, Downloader},
-    ytcc_drop, Duration, Line, LineCommand, YtccListData,
+    ytcc_drop, Line, LineCommand, YtccListData,
 };
 
 fn main() -> Result<()> {
@@ -31,46 +31,43 @@ fn main() -> Result<()> {
         .context("Failed to deserialize json output")?
     };
 
-    let temp_file = NamedTempFile::new().context("Failed to get tempfile")?;
-    let mut edit_file = BufWriter::new(&temp_file);
+    let temp_file = Builder::new()
+        .prefix("yt_video_select")
+        .suffix(".yts")
+        .rand_bytes(6)
+        .tempfile()
+        .context("Failed to get tempfile")?;
 
-    json_map
-        .iter()
-        .map(|line| {
-            format!(
-                r#"pick {} "{}" "{}" "{}" "{}" "{}"{}"#,
-                line.id,
-                line.title.replace('"', "'"),
-                line.publish_date,
-                line.playlists
-                    .iter()
-                    .map(|p| p.name.replace('"', "'"))
-                    .collect::<Vec<String>>()
-                    .join(", "),
-                Duration::from(line.duration.trim()),
-                line.url.replace('"', "'"),
-                "\n"
-            )
-        })
-        .for_each(|line| {
-            edit_file
-                .write(line.as_bytes())
-                .expect("This write should not fail");
-        });
+    {
+        let mut edit_file = BufWriter::new(&temp_file);
 
-    edit_file.write(HELP_STR.as_bytes())?;
-    edit_file.flush().context("Failed to flush edit file")?;
+        json_map
+            .iter()
+            .map(|line| line.to_string())
+            .for_each(|line| {
+                edit_file
+                    .write(line.as_bytes())
+                    .expect("This write should not fail");
+            });
 
-    let read_file = temp_file.reopen()?;
-
-    let mut nvim = StdCmd::new("nvim");
-    nvim.arg(temp_file.path());
+        edit_file.write(HELP_STR.as_bytes())?;
+        edit_file.flush().context("Failed to flush edit file")?;
 
-    let status = nvim.status().context("Falied to run nvim")?;
-    if !status.success() {
-        bail!("nvim exited with error status: {}", status)
+        let mut nvim = StdCmd::new("nvim");
+        nvim.arg(temp_file.path());
+        let status = nvim.status().context("Falied to run nvim")?;
+        if !status.success() {
+            bail!("nvim exited with error status: {}", status)
+        }
     }
 
+    let read_file = temp_file.reopen()?;
+    fs::copy(
+        temp_file.path(),
+        last_select().context("Failed to get persistent the selection file path")?,
+    )
+    .context("Failed to persist selection file")?;
+
     let mut watching = Vec::new();
     let reader = BufReader::new(&read_file);
     for line in reader.lines() {