aboutsummaryrefslogtreecommitdiffstats
path: root/crates
diff options
context:
space:
mode:
authorBenedikt Peetz <benedikt.peetz@b-peetz.de>2025-06-15 17:15:52 +0200
committerBenedikt Peetz <benedikt.peetz@b-peetz.de>2025-06-15 17:15:52 +0200
commitfb49841e1ec14b3ab2de981e439d4f10f5494cf5 (patch)
treee110fdffa521597a4a5ad9b85fcb827c598e1651 /crates
parentfeat(yt/update): Specify subscriptions to update as positional args (diff)
downloadyt-fb49841e1ec14b3ab2de981e439d4f10f5494cf5.zip
refactor(yt/select): Split the `select::select` function up
Diffstat (limited to '')
-rw-r--r--crates/yt/src/select/mod.rs120
1 files changed, 72 insertions, 48 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?
- };
+ let matching_videos = get_videos(app, done).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?);
- }
+ write_videos_to_file(app, temp_file.as_file(), &matching_videos).await?;
+ }
- let mut edit_file = BufWriter::new(&temp_file);
+ open_editor_at(temp_file.path()).await?;
- 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`")?;
+ let read_file = temp_file.reopen()?;
+ fs::copy(temp_file.path(), &app.config.paths.last_selection_path)
+ .context("Failed to persist selection file")?;
- Ok(())
- })?;
+ process_file(app, &read_file).await?;
- edit_file.write_all(HELP_STR.as_bytes())?;
- edit_file.flush().context("Failed to flush edit file")?;
- };
+ Ok(())
+}
- {
- let editor = env::var("EDITOR").unwrap_or("nvim".to_owned());
+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
+ }
+}
- 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)
- }
+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 read_file = temp_file.reopen()?;
- fs::copy(temp_file.path(), &app.config.paths.last_selection_path)
- .context("Failed to persist selection file")?;
+ 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(())
+}
- let reader = BufReader::new(&read_file);
+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> {