nomnom-app 0.1.1

YT-DLP GUI Wrapper, but it eats URLs and spits out videos. simple.
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
//! runner.rs - yt-dlp subprocess management and output streaming.
//!
//! This module provides:
//! - [`DownloadRequest`]     — All parameters for a single download invocation
//! - [`build_command_string`] — Human-readable command preview for the UI
//! - [`run_download`]         — Async subprocess with live log streaming
//! - [`run_raw_command`]      — Raw command execution for the terminal panel
//! - [`cancel_download`]      — Kill the active child process
//!
//! # Architecture
//!
//! `run_download` accepts a [`DownloadRequest`] value that bundles the type,
//! source, quality, flags, paths, and URL. This replaces the old flat signature
//! and makes the caller's intent explicit.
//!
//! The internal [`build_exec_args`] helper translates the request into a plain
//! `Vec<String>` that is passed to the shell via `"$@"` expansion — each arg is
//! a separate process argument, so no shell quoting or injection is possible.
//!
//! # Error Handling
//!
//! All errors are captured and written to the log as user-friendly messages.
//! The application never panics from subprocess failures.

use crate::core::{
    download_mode::{DownloadSource, DownloadType, Quality},
    flags::Flag,
};
use dioxus::prelude::*;
use std::{
    process::Stdio,
    sync::{Arc, Mutex},
};
use tokio::{
    io::{AsyncBufReadExt, BufReader},
    process::{Child, Command},
};

// -------------------------------------------- Types --------------------------------------------

/// Thread-safe slot holding the active child process, if any.
///
/// Shared between the async runner task and the UI stop button.
/// `Arc<Mutex<...>>` because it must cross async task + `onclick` handler boundaries.
pub type ChildHandle = Arc<Mutex<Option<Child>>>;

/// All parameters required to execute or preview a yt-dlp download.
///
/// This struct is the single contract between the UI layer and the runner.
/// Construct it in the download button handler and pass it to [`run_download`].
///
/// # Example
///
/// ```rust,ignore
/// let req = DownloadRequest {
///     url: "https://youtube.com/watch?v=abc".into(),
///     batch_file: String::new(),
///     archive_file: "/home/user/archive.txt".into(),
///     download_type: DownloadType::Video,
///     download_source: DownloadSource::Single,
///     quality: Quality::HD1080,
///     output_dir: "/home/user/Videos".into(),
///     extra_flags: active_flags.read().clone(),
/// };
/// runner::run_download(req, log_lines, is_running, child_handle).await;
/// ```
#[derive(Debug, Clone)]
pub struct DownloadRequest {
    /// Video/playlist/channel URL. Empty when source is [`DownloadSource::Batch`].
    pub url: String,
    /// Path to a text file with one URL per line. Used for [`DownloadSource::Batch`].
    pub batch_file: String,
    /// Path to a yt-dlp download archive file (`--download-archive`). Empty = disabled.
    pub archive_file: String,
    /// Whether to download video or extract audio.
    pub download_type: DownloadType,
    /// URL source and output folder organisation strategy.
    pub download_source: DownloadSource,
    /// Video resolution cap. Ignored for [`DownloadType::Audio`].
    pub quality: Quality,
    /// Root directory for all output files.
    pub output_dir: String,
    /// Additional flags toggled by the user in the flag panel.
    pub extra_flags: Vec<Flag>,
}

// -------------------------------------------- Public API --------------------------------------------

/// Builds a human-readable command string for the UI preview panel.
///
/// Produces the exact command a user would type in a terminal, with proper
/// quoting around arguments that contain spaces or special characters.
///
/// # Arguments
///
/// * `req` — The full download configuration.
///
/// # Returns
///
/// A formatted string like:
/// ```text
/// yt-dlp -f "bestvideo[height<=1080]+bestaudio/best[height<=1080]" \
///   -o "/home/user/Videos/%(title)s..." \
///   --embed-thumbnail --add-metadata "https://youtube.com/..."
/// ```
///
/// Returns a placeholder if no URL or batch file is specified.
pub fn build_command_string(req: &DownloadRequest) -> String {
    let has_input = !req.url.trim().is_empty() || !req.batch_file.trim().is_empty();
    if !has_input {
        return "yt-dlp [paste a URL or pick a batch file above]".to_string();
    }

    let exec_args = build_exec_args(req);

    // Quote any arg that contains a space, bracket, or percent sign for display.
    let display: Vec<String> = exec_args
        .iter()
        .map(|a| {
            if a.contains(' ') || a.contains('[') || a.contains('%') || a.contains('>') {
                format!("\"{}\"", a)
            } else {
                a.clone()
            }
        })
        .collect();

    format!("yt-dlp {}", display.join(" "))
}

/// Kills the active child process stored in the handle.
///
/// Called by the UI stop button. Locks the handle, takes the child,
/// and sends SIGKILL. Safe to call when no child is running (no-op).
///
/// # Arguments
///
/// * `handle`     — The shared child handle.
/// * `log_lines`  — Signal to write a cancellation message.
/// * `is_running` — Signal to reset running state.
pub fn cancel_download(
    handle: &ChildHandle,
    mut log_lines: Signal<Vec<String>>,
    mut is_running: Signal<bool>,
) {
    let mut lock = handle.lock().unwrap();
    if let Some(child) = lock.as_mut() {
        match child.start_kill() {
            Ok(_) => {
                log_lines
                    .write()
                    .push("⛔ Download cancelled by user.".to_string());
            }
            Err(e) => {
                log_lines
                    .write()
                    .push(format!("✗ Failed to kill process: {e}"));
            }
        }
        *lock = None;
    }
    is_running.set(false);
}

/// Spawns yt-dlp as a subprocess and streams output line-by-line to the log.
///
/// # Arguments
///
/// * `req`          — Full download configuration (type, source, quality, flags, paths).
/// * `log_lines`    — Signal to receive output lines (cleared before the download starts).
/// * `is_running`   — Signal tracking running state.
/// * `child_handle` — Shared slot to store the child process for cancellation.
pub async fn run_download(
    req: DownloadRequest,
    mut log_lines: Signal<Vec<String>>,
    mut is_running: Signal<bool>,
    child_handle: ChildHandle,
) {
    is_running.set(true);
    log_lines.write().clear();

    // Validate input before spawning.
    let has_input = !req.url.trim().is_empty()
        || (req.download_source == DownloadSource::Batch && !req.batch_file.trim().is_empty());

    if !has_input {
        log_lines
            .write()
            .push("⚠ Please enter a URL or pick a batch file first.".to_string());
        is_running.set(false);
        return;
    }

    log_lines.write().push("▶ Starting download…".to_string());
    log_lines
        .write()
        .push(format!("  {}", build_command_string(&req)));

    let args = build_exec_args(&req);
    let shell = std::env::var("SHELL").unwrap_or_else(|_| "/bin/bash".to_string());

    let result = Command::new(&shell)
        .arg("-i")
        .arg("-c")
        .arg("yt-dlp \"$@\"") // $@ safely expands each positional arg
        .arg("bash") // $0 — script name placeholder, not in $@
        .args(&args) // $1..n become $@
        .stdout(Stdio::piped())
        .stderr(Stdio::piped())
        .spawn();

    match result {
        Err(e) => {
            log_lines
                .write()
                .push(format!("✗ Failed to spawn yt-dlp: {e}"));
            log_lines
                .write()
                .push("  Make sure yt-dlp is installed and in your PATH.".to_string());
            is_running.set(false);
        }
        Ok(mut child) => {
            // Take stdout/stderr BEFORE storing the child (storing moves it).
            let stdout = child.stdout.take();
            let stderr = child.stderr.take();

            // Store child so the stop button can kill it.
            {
                let mut lock = child_handle.lock().unwrap();
                *lock = Some(child);
            }

            // Stream stdout.
            if let Some(stdout) = stdout {
                let mut reader = BufReader::new(stdout).lines();
                while let Ok(Some(line)) = reader.next_line().await {
                    log_lines.write().push(line);
                }
            }

            // Stream stderr (prefixed with warning symbol).
            if let Some(stderr) = stderr {
                let mut reader = BufReader::new(stderr).lines();
                while let Ok(Some(line)) = reader.next_line().await {
                    log_lines.write().push(format!("{line}"));
                }
            }

            // Take child back to call .wait().
            let child_opt = {
                let mut lock = child_handle.lock().unwrap();
                lock.take()
            };

            if let Some(mut child) = child_opt {
                match child.wait().await {
                    Ok(status) if status.success() => {
                        log_lines.write().push("✔ Done!".to_string());
                    }
                    Ok(status) => {
                        // Only show error if we weren't cancelled.
                        if *is_running.read() {
                            log_lines
                                .write()
                                .push(format!("✗ yt-dlp exited with: {status}"));
                        }
                    }
                    Err(e) => {
                        log_lines.write().push(format!("✗ Wait error: {e}"));
                    }
                }
            }
            // child_opt == None means the user cancelled — message already logged.

            is_running.set(false);
        }
    }
}

/// Executes an arbitrary command string from the terminal panel.
///
/// Bypasses the GUI flag selection — useful for power users and testing.
///
/// # Arguments
///
/// * `raw`          — The raw command string to execute.
/// * `log_lines`    — Signal to receive output lines.
/// * `is_running`   — Signal tracking running state.
/// * `child_handle` — Shared slot to store the child process for cancellation.
pub async fn run_raw_command(
    raw: String,
    mut log_lines: Signal<Vec<String>>,
    mut is_running: Signal<bool>,
    child_handle: ChildHandle,
) {
    is_running.set(true);
    log_lines.write().push(format!("$ {raw}"));

    if raw.trim().is_empty() {
        is_running.set(false);
        return;
    }

    let shell = std::env::var("SHELL").unwrap_or_else(|_| "/bin/bash".to_string());
    let result = Command::new(&shell)
        .args(["-i", "-c", &raw])
        .stdout(Stdio::piped())
        .stderr(Stdio::piped())
        .spawn();

    match result {
        Err(e) => {
            log_lines.write().push(format!("{e}"));
            is_running.set(false);
        }
        Ok(mut child) => {
            let stdout = child.stdout.take();
            let stderr = child.stderr.take();

            {
                let mut lock = child_handle.lock().unwrap();
                *lock = Some(child);
            }

            if let Some(stdout) = stdout {
                let mut reader = BufReader::new(stdout).lines();
                while let Ok(Some(line)) = reader.next_line().await {
                    log_lines.write().push(line);
                }
            }
            if let Some(stderr) = stderr {
                let mut reader = BufReader::new(stderr).lines();
                while let Ok(Some(line)) = reader.next_line().await {
                    log_lines.write().push(format!("{line}"));
                }
            }

            let child_opt = {
                let mut lock = child_handle.lock().unwrap();
                lock.take()
            };
            if let Some(mut child) = child_opt {
                let _ = child.wait().await;
            }

            is_running.set(false);
        }
    }
}

// -------------------------------------------- Private Helper Functions --------------------------------------------

/// Builds the execution argument list for a [`DownloadRequest`].
///
/// Returns a `Vec<String>` where each element is a separate process argument.
/// No shell quoting is applied — arguments are passed via `"$@"` expansion,
/// so the shell never interprets special characters.
///
/// # Argument Order
///
/// 1. Type flags (`-f FORMAT` for video, `-x` for audio)
/// 2. Output template (`-o TEMPLATE`)
/// 3. Download archive (`--download-archive PATH` if set)
/// 4. Extra user-selected flags
/// 5. Source (`--batch-file PATH` or the URL string)
fn build_exec_args(req: &DownloadRequest) -> Vec<String> {
    let mut args: Vec<String> = Vec::new();
    //note: Allow yt-dlp to download the scripts from GitHub automatically.
    // "--remote-components ejs:github"

    // ── 1. Type-specific format/extraction flags ───────────────────────────
    match req.download_type {
        DownloadType::Video => {
            args.push("-f".to_string());
            args.push(req.quality.format_string().to_string());
        }
        DownloadType::Audio => {
            args.push("-x".to_string());
        }
    }

    // ── 2. Output template ────────────────────────────────────────────────
    let template = req.download_source.output_template(&req.output_dir);
    args.push("-o".to_string());
    args.push(template); // passed as a single arg — no quoting needed

    // ── 3. Download archive (optional) ───────────────────────────────────
    if !req.archive_file.trim().is_empty() {
        args.push("--download-archive".to_string());
        args.push(req.archive_file.clone());
    }

    // ── 4. Extra flags from the flag panel ───────────────────────────────
    for flag in &req.extra_flags {
        // Some flag strings include a value: "--audio-format mp3" → two tokens.
        for token in flag.flag.split_whitespace() {
            args.push(token.to_string());
        }
    }

    // ── 5. URL or batch file ──────────────────────────────────────────────
    match &req.download_source {
        DownloadSource::Batch if !req.batch_file.trim().is_empty() => {
            args.push("--batch-file".to_string());
            args.push(req.batch_file.clone());
        }
        _ if !req.url.trim().is_empty() => {
            args.push(req.url.clone());
        }
        _ => {}
    }

    args
}