about summary refs log tree commit diff stats
path: root/src/select/mod.rs
blob: 2663a04461656491f855a52990b280d5e436fe81 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
// yt - A fully featured command line YouTube client
//
// Copyright (C) 2024 Benedikt Peetz <benedikt.peetz@b-peetz.de>
// SPDX-License-Identifier: GPL-3.0-or-later
//
// This file is part of Yt.
//
// You should have received a copy of the License along with this program.
// If not, see <https://www.gnu.org/licenses/gpl-3.0.txt>.

use std::{
    env::{self},
    fs,
    io::{BufRead, Write},
    io::{BufReader, BufWriter},
};

use crate::{
    app::App,
    cli::CliArgs,
    constants::HELP_STR,
    storage::video_database::{getters::get_videos, VideoStatus},
};

use anyhow::{bail, Context, Result};
use clap::Parser;
use cmds::handle_select_cmd;
use futures::future::join_all;
use selection_file::process_line;
use tempfile::Builder;
use tokio::process::Command;

pub mod cmds;
pub mod selection_file;

pub async fn select(app: &App, done: bool, use_last_selection: bool) -> Result<()> {
    let temp_file = Builder::new()
        .prefix("yt_video_select-")
        .suffix(".yts")
        .rand_bytes(6)
        .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.first() {
            let _ = vid.to_select_file_display(app).await?;
        }

        let mut edit_file = BufWriter::new(&temp_file);

        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");

            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)
        }
    }

    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);

    let mut line_number = 0;
    for line in reader.lines() {
        let line = line.context("Failed to read a line")?;

        if let Some(line) = process_line(&line)? {
            line_number -= 1;

            // debug!(
            //     "Parsed command: `{}`",
            //     line.iter()
            //         .map(|val| format!("\"{}\"", val))
            //         .collect::<Vec<String>>()
            //         .join(" ")
            // );

            let arg_line = ["yt", "select"]
                .into_iter()
                .chain(line.iter().map(|val| val.as_str()));

            let args = CliArgs::parse_from(arg_line);

            let cmd = if let crate::cli::Command::Select { cmd } =
                args.command.expect("This will be some")
            {
                cmd
            } else {
                unreachable!("This is checked in the `filter_line` function")
            };

            handle_select_cmd(
                app,
                cmd.expect("This value should always be some here"),
                Some(line_number),
            )
            .await?
        }
    }

    Ok(())
}

// // 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 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)
//         .args(&["select", "--help"])
//         .output()
//         .await?;
//
//     assert_eq!(cmd.status.code(), Some(0));
//
//     let output = String::from_utf8(cmd.stdout).expect("Our help output was not utf8?");
//
//     let out = output
//         .lines()
//         .map(|line| format!("# {}\n", line))
//         .collect::<String>();
//
//     debug!("Returning help: '{}'", &out);
//
//     Ok(out)
// }