aboutsummaryrefslogtreecommitdiffstats
path: root/sys/nixpkgs/pkgs/yt/src/bin
diff options
context:
space:
mode:
Diffstat (limited to '')
-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() {