rustdrop 0.2.0

Privacy-first, blazing-fast peer-to-peer file transfer for your local network
Documentation
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
//! Terminal presentation: progress bars, spinners and prompts.
//!
//! All rendering goes to **stderr** so stdout stays clean for machine
//! consumable output (`--json`, pipes).

use std::io::IsTerminal;
use std::sync::Mutex;
use std::time::Duration;

use indicatif::{MultiProgress, ProgressBar, ProgressStyle};

use crate::transfer::{ProgressSink, TransferReport};

const SPINNER_CHARS: &str = "⠋⠙⠹⠸⠼⠴⠦⠧⠇⠏ ";
const BAR_CHARS: &str = "█░";
const TICK: Duration = Duration::from_millis(80);

/// [`ProgressSink`] implementation used by the CLI.
pub struct CliProgress {
    quiet: bool,
    verbose: bool,
    tty: bool,
    multi: MultiProgress,
    spinner: Mutex<Option<ProgressBar>>,
    overall: Mutex<Option<ProgressBar>>,
    file: Mutex<Option<ProgressBar>>,
}

impl CliProgress {
    pub fn new(quiet: bool, verbose: bool) -> Self {
        Self {
            quiet,
            verbose,
            tty: std::io::stderr().is_terminal(),
            multi: MultiProgress::new(),
            spinner: Mutex::new(None),
            overall: Mutex::new(None),
            file: Mutex::new(None),
        }
    }

    /// Print a standalone line that plays nicely with active bars.
    pub fn line(&self, msg: &str) {
        if self.quiet {
            return;
        }
        if self.tty {
            let _ = self.multi.println(msg);
        } else {
            eprintln!("{msg}");
        }
    }

    /// Same as [`Self::line`] but only in `--verbose` mode.
    pub fn debug_line(&self, msg: &str) {
        if self.verbose {
            self.line(&dim(msg));
        }
    }

    /// Always printed (even with `--quiet`): security-relevant information.
    pub fn security_line(&self, msg: &str) {
        if self.tty {
            let _ = self.multi.println(format!("\x1b[1m\x1b[33m{msg}\x1b[0m"));
        } else {
            eprintln!("{msg}");
        }
    }

    fn ensure_spinner(&self) -> Option<ProgressBar> {
        if !self.tty || self.quiet {
            return None;
        }
        let mut guard = self.spinner.lock().expect("spinner lock poisoned");
        if let Some(bar) = guard.as_ref() {
            return Some(bar.clone());
        }
        let style = ProgressStyle::with_template("{spinner:.cyan} {wide_msg}")
            .expect("valid spinner template")
            .tick_chars(SPINNER_CHARS);
        let bar = self.multi.add(ProgressBar::new_spinner());
        bar.set_style(style);
        bar.enable_steady_tick(TICK);
        *guard = Some(bar.clone());
        Some(bar)
    }

    fn clear_spinner(&self) {
        if let Ok(mut guard) = self.spinner.lock()
            && let Some(bar) = guard.take()
        {
            bar.finish_and_clear();
        }
    }

    fn ensure_bars(&self) {
        if !self.tty || self.quiet {
            return;
        }
        let mut overall = self.overall.lock().expect("overall lock poisoned");
        if overall.is_none() {
            let style = ProgressStyle::with_template(
                "{spinner:.green} {elapsed_precise} {bar:32.cyan/blue} \
                 {bytes}/{total_bytes} {bytes_per_sec} {eta}",
            )
            .expect("valid overall template")
            .progress_chars(BAR_CHARS);
            let bar = self.multi.add(ProgressBar::new(0));
            bar.set_style(style);
            *overall = Some(bar);
        }
        let mut file = self.file.lock().expect("file lock poisoned");
        if file.is_none() {
            let style = ProgressStyle::with_template(
                "{bar:32.yellow/dim} {bytes}/{total_bytes} {wide_msg}",
            )
            .expect("valid file template")
            .progress_chars(BAR_CHARS);
            let bar = self.multi.add(ProgressBar::new(0));
            bar.set_style(style);
            *file = Some(bar);
        }
    }
}

impl ProgressSink for CliProgress {
    fn status(&self, msg: &str) {
        if self.quiet {
            return;
        }
        if let Some(spinner) = self.ensure_spinner() {
            spinner.set_message(msg.to_string());
        } else {
            eprintln!("{msg}");
        }
    }

    fn discovered(&self, name: &str, addr: &str) {
        self.debug_line(&format!("discovered {name} at {addr}"));
    }

    fn connected(&self, peer: &str) {
        self.line(&format!("  connected to {peer}"));
    }

    fn trust_code(&self, code: &str) {
        self.security_line(&format!("  trust code: {code}"));
    }

    fn begin(&self, _files: usize, bytes: u64) {
        self.clear_spinner();
        self.ensure_bars();
        let guard = self.overall.lock().expect("overall lock poisoned");
        if let Some(bar) = guard.as_ref() {
            // Watch mode starts a fresh session after the previous one
            // finished, so the bar has to come back to life first.
            bar.reset();
            bar.set_length(bytes);
            bar.set_position(0);
        }
    }

    fn file(&self, path: &str, size: u64) {
        let guard = self.file.lock().expect("file lock poisoned");
        if let Some(bar) = guard.as_ref() {
            bar.reset();
            bar.set_length(size);
            bar.set_message(shorten(path, 48));
        }
        drop(guard);
        self.debug_line(&format!("sending {path}"));
    }

    fn advance(&self, bytes: u64) {
        let guard = self.overall.lock().expect("overall lock poisoned");
        if let Some(bar) = guard.as_ref() {
            bar.inc(bytes);
        }
        drop(guard);
        let guard = self.file.lock().expect("file lock poisoned");
        if let Some(bar) = guard.as_ref() {
            bar.inc(bytes);
        }
    }

    fn file_hash(&self, path: &str, hash: &str) {
        self.debug_line(&format!("blake3 {hash}  {path}"));
    }

    fn verify(&self, path: &str, ok: bool) {
        if ok {
            self.debug_line(&format!("verified {path}"));
        } else {
            self.line(&format!("  \x1b[31mchecksum mismatch: {path}\x1b[0m"));
        }
    }

    fn finish(&self, report: &TransferReport) {
        if let Ok(guard) = self.overall.lock()
            && let Some(bar) = guard.as_ref()
        {
            bar.finish_with_message("done");
        }
        if let Ok(guard) = self.file.lock()
            && let Some(bar) = guard.as_ref()
        {
            bar.finish_and_clear();
        }
        self.clear_spinner();
        if !self.quiet {
            self.line(&format!("  \x1b[32m✓\x1b[0m {report}"));
        }
    }
}

impl Drop for CliProgress {
    fn drop(&mut self) {
        self.clear_spinner();
    }
}

/// Render `text` as a QR code using truecolor background cells.
///
/// Every module becomes two spaces with a black or white background, one
/// line per module row. Unlike half-block glyphs this stays readable
/// black-on-white on both light and dark terminal palettes.
pub fn render_qr_ansi(text: &str) -> anyhow::Result<String> {
    use anyhow::Context as _;
    use qrcode::QrCode;

    let code = QrCode::new(text.as_bytes()).context("text does not fit in a QR code")?;
    let width = code.width();
    let colors = code.to_colors();
    const QUIET: usize = 4;
    const DARK: &str = "\x1b[48;2;0;0;0m  ";
    const LIGHT: &str = "\x1b[48;2;255;255;255m  ";

    let mut out = String::new();
    for y in 0..width + 2 * QUIET {
        for x in 0..width + 2 * QUIET {
            let inside = x >= QUIET && x < QUIET + width && y >= QUIET && y < QUIET + width;
            let dark = inside && colors[(y - QUIET) * width + (x - QUIET)] == qrcode::Color::Dark;
            out.push_str(if dark { DARK } else { LIGHT });
        }
        out.push_str("\x1b[0m\n");
    }
    Ok(out)
}

/// Interactive "do you accept this transfer?" prompt.
///
/// Returns `true` when the user confirms. When stdin is not a terminal the
/// prompt is refused: automation must be explicit (`--yes`).
pub fn confirm_transfer(peer: &str, preview: &crate::transfer::receiver::TransferPreview) -> bool {
    use dialoguer::Confirm;
    use dialoguer::theme::ColorfulTheme;

    if !std::io::stdin().is_terminal() {
        eprintln!("refusing to prompt: stdin is not a terminal (re-run with --yes)");
        return false;
    }

    let paths = if preview.first_paths.is_empty() {
        String::new()
    } else {
        let shown = preview.first_paths.join(", ");
        let more = preview.files.saturating_sub(preview.first_paths.len());
        if more > 0 {
            format!("\n            {shown} \x1b[2m(+{more} more)\x1b[0m")
        } else {
            format!("\n            {shown}")
        }
    };

    let prompt = format!(
        "accept {} file(s) ({}) from {peer}?{paths}",
        preview.files,
        crate::transfer::human_bytes(preview.bytes)
    );

    Confirm::with_theme(&ColorfulTheme::default())
        .with_prompt(prompt)
        .default(false)
        .interact()
        .unwrap_or(false)
}

/// Pick one receiver from a discovered list (interactive only).
pub fn select_receiver(
    receivers: &[crate::discovery::DiscoveredReceiver],
) -> Option<crate::discovery::DiscoveredReceiver> {
    use dialoguer::Select;
    use dialoguer::theme::ColorfulTheme;

    if receivers.is_empty() {
        return None;
    }
    if receivers.len() == 1 {
        return Some(receivers[0].clone());
    }
    if !std::io::stdin().is_terminal() {
        eprintln!("multiple receivers found; pass --name or --to disambiguate");
        return None;
    }

    let items: Vec<String> = receivers
        .iter()
        .map(|r| {
            let addr = r
                .addresses
                .first()
                .map(|a| a.to_string())
                .unwrap_or_else(|| "?".into());
            format!("{} ({addr})", r.device_name)
        })
        .collect();

    Select::with_theme(&ColorfulTheme::default())
        .with_prompt("choose a receiver")
        .items(&items)
        .default(0)
        .interact()
        .ok()
        .and_then(|i| receivers.get(i).cloned())
}

fn dim(s: &str) -> String {
    format!("\x1b[2m{s}\x1b[0m")
}

/// Middle-elide long paths so bars stay readable.
fn shorten(path: &str, max: usize) -> String {
    let chars: Vec<char> = path.chars().collect();
    if chars.len() <= max {
        return path.to_string();
    }
    if max < 4 {
        let cut: String = chars[..max].iter().collect();
        return cut;
    }
    let head_len = (max - 3) / 2;
    let tail_len = max - 3 - head_len;
    let head: String = chars[..head_len].iter().collect();
    let tail: String = chars[chars.len() - tail_len..].iter().collect();
    format!("{head}...{tail}")
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn shortening_is_stable() {
        assert_eq!(shorten("short.txt", 20), "short.txt");
        let long = "some/very/deep/nested/tree/of/files/filename.txt";
        let out = shorten(long, 20);
        assert_eq!(out.len(), 20);
        assert!(out.contains("..."));
    }

    #[test]
    fn quiet_progress_is_safe_to_construct() {
        let p = CliProgress::new(true, false);
        p.status("ignored");
        p.trust_code("ABCD-EFGH");
        p.finish(&TransferReport {
            peer: "x".into(),
            files: 0,
            bytes: 0,
            elapsed: Duration::from_secs(0),
        });
    }
}

#[cfg(test)]
mod qr_tests {
    use super::render_qr_ansi;

    #[test]
    fn qr_cells_form_a_square_with_a_white_quiet_zone() {
        let qr = render_qr_ansi("RUSTDROP").expect("short text fits");
        let lines: Vec<&str> = qr.lines().collect();
        assert!(
            lines[0].starts_with("\x1b[48;2;255;255;255m  "),
            "quiet zone starts white"
        );
        assert!(
            qr.contains("\x1b[48;2;0;0;0m  "),
            "payload has dark modules"
        );

        let cells: Vec<usize> = lines.iter().map(|l| l.matches("m  ").count()).collect();
        assert!(
            lines.iter().all(|l| l.ends_with("\x1b[0m")),
            "each row resets color"
        );
        assert!(
            cells.iter().all(|&c| c == cells[0]),
            "all rows have the same width"
        );
        assert_eq!(cells[0], lines.len(), "code plus quiet zone is square");
        assert!(cells[0] > 8, "4-module quiet zone on each side");
    }

    #[test]
    fn qr_rejects_oversized_payloads() {
        assert!(render_qr_ansi(&"x".repeat(5000)).is_err());
    }
}