elio 1.3.0

Snappy, batteries-included terminal file manager with rich previews, inline images, bulk actions, and trash support.
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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
use super::*;
use std::{
    fs,
    path::{Path, PathBuf},
    sync::{
        Arc, Condvar, Mutex,
        atomic::{AtomicBool, AtomicU64, Ordering},
        mpsc,
    },
    thread,
    time::{Duration, Instant},
};

/// Minimum time between intermediate progress results sent to the UI.
/// Prevents a per-file result flood from turning into a constant redraw storm
/// when pasting or trashing large numbers of small files.
const PROGRESS_SEND_INTERVAL: Duration = Duration::from_millis(80);

pub(in crate::app::jobs) struct PastePool {
    shared: Arc<PasteShared>,
    workers: Vec<thread::JoinHandle<()>>,
}

struct PasteShared {
    state: Mutex<PasteState>,
    available: Condvar,
    /// Set by `Drop` — signals the worker to stop for shutdown.
    cancelled: AtomicBool,
    /// Token of the paste the user explicitly cancelled.  The worker stops
    /// when its request token matches this value.  A new paste carries a
    /// different token so it is never accidentally cancelled by an older
    /// cancellation request — no reset needed on new submits.
    cancel_token: AtomicU64,
}

struct PasteState {
    pending: Option<PasteRequest>,
    active: bool,
    closed: bool,
}

impl PastePool {
    pub(in crate::app::jobs) fn new(result_tx: mpsc::Sender<JobResult>) -> Self {
        let shared = Arc::new(PasteShared {
            state: Mutex::new(PasteState {
                pending: None,
                active: false,
                closed: false,
            }),
            available: Condvar::new(),
            cancelled: AtomicBool::new(false),
            cancel_token: AtomicU64::new(0), // 0 = "nothing cancelled" (tokens start at 1)
        });
        let shared_worker = Arc::clone(&shared);
        let worker = thread::spawn(move || {
            while let Some(request) = PasteShared::pop(&shared_worker) {
                PasteShared::set_active(&shared_worker, true);
                let (completed, errors, stopped_early) = run_paste(
                    &request,
                    &result_tx,
                    &shared_worker.cancelled,
                    &shared_worker.cancel_token,
                );
                PasteShared::set_active(&shared_worker, false);

                let verb = match request.op {
                    ClipOp::Yank => "Copied",
                    ClipOp::Cut => "Moved",
                };
                let status = if stopped_early {
                    match completed {
                        0 => "Paste cancelled".to_string(),
                        1 => format!("Paste cancelled — {verb} 1 item"),
                        n => format!("Paste cancelled — {verb} {n} items"),
                    }
                } else if errors.is_empty() {
                    match completed {
                        0 => "Nothing was pasted".to_string(),
                        1 => format!("{verb} 1 item"),
                        n => format!("{verb} {n} items"),
                    }
                } else if completed == 0 {
                    if errors.len() == 1 {
                        errors[0].clone()
                    } else {
                        format!("{} errors — first: {}", errors.len(), errors[0])
                    }
                } else {
                    format!(
                        "{verb} {completed} item(s); {} error(s) — first: {}",
                        errors.len(),
                        errors[0]
                    )
                };

                if result_tx
                    .send(JobResult::Paste(PasteBuild {
                        token: request.token,
                        completed,
                        done: true,
                        status: Some(status),
                    }))
                    .is_err()
                {
                    break;
                }
            }
        });
        Self {
            shared,
            workers: vec![worker],
        }
    }

    pub(in crate::app::jobs) fn submit(&self, request: PasteRequest) -> bool {
        let mut state = lock_unpoison(&self.shared.state);
        if state.closed {
            return false;
        }
        state.pending = Some(request);
        self.shared.available.notify_one();
        true
    }

    /// Signal the worker to stop after the current item if it is processing
    /// the paste with the given token.  A concurrent or future paste with a
    /// different token is unaffected.
    pub(in crate::app::jobs) fn cancel_paste(&self, token: u64) {
        self.shared.cancel_token.store(token, Ordering::Relaxed);
    }

    pub(in crate::app::jobs) fn has_pending_work(&self) -> bool {
        let state = lock_unpoison(&self.shared.state);
        state.pending.is_some() || state.active
    }
}

impl Drop for PastePool {
    fn drop(&mut self) {
        {
            let mut state = lock_unpoison(&self.shared.state);
            state.closed = true;
            state.pending = None;
        }
        self.shared.cancelled.store(true, Ordering::Relaxed);
        self.shared.available.notify_all();
        for worker in self.workers.drain(..) {
            let _ = worker.join();
        }
    }
}

impl PasteShared {
    fn pop(shared: &Arc<Self>) -> Option<PasteRequest> {
        let mut state = lock_unpoison(&shared.state);
        loop {
            if state.closed {
                return None;
            }
            if let Some(request) = state.pending.take() {
                return Some(request);
            }
            state = wait_unpoison(&shared.available, state);
        }
    }

    fn set_active(shared: &Arc<Self>, active: bool) {
        lock_unpoison(&shared.state).active = active;
    }
}

/// Execute the paste operation, sending throttled intermediate progress
/// results through `result_tx`.  Returns `(completed, errors, stopped_early)`.
///
/// `stopped_early` is `true` if the loop was cut short by a cancel flag rather
/// than running to completion.
fn run_paste(
    request: &PasteRequest,
    result_tx: &mpsc::Sender<JobResult>,
    cancelled: &AtomicBool,
    cancel_token: &AtomicU64,
) -> (usize, Vec<String>, bool) {
    let mut completed = 0usize;
    let mut errors: Vec<String> = Vec::new();
    let mut stopped_early = false;
    // Tracks when we last sent a progress result.  None = never sent, which
    // causes the first update to go through immediately.
    let mut last_progress_at: Option<Instant> = None;

    for src in &request.paths {
        if cancelled.load(Ordering::Relaxed)
            || cancel_token.load(Ordering::Relaxed) == request.token
        {
            stopped_early = true;
            break;
        }
        let Some(file_name) = src.file_name().and_then(|n| n.to_str()) else {
            errors.push(format!("Cannot determine name for {}", src.display()));
            if !send_paste_progress(result_tx, request.token, completed, &mut last_progress_at) {
                break;
            }
            continue;
        };

        if !src.exists() {
            errors.push(format!("\"{}\" no longer exists", file_name));
            if !send_paste_progress(result_tx, request.token, completed, &mut last_progress_at) {
                break;
            }
            continue;
        }

        // For cut: same-dir same-name is a no-op.
        if request.op == ClipOp::Cut {
            let natural = request.dest_dir.join(file_name);
            if natural == *src {
                completed += 1;
                if !send_paste_progress(result_tx, request.token, completed, &mut last_progress_at)
                {
                    break;
                }
                continue;
            }
        }

        let dest = unique_dest(&request.dest_dir, file_name);

        let ok = match request.op {
            ClipOp::Yank => match copy_recursive(src, &dest) {
                Ok(()) => true,
                Err(e) => {
                    errors.push(format!("\"{}\" could not be copied: {e}", file_name));
                    false
                }
            },
            ClipOp::Cut => match fs::rename(src, &dest) {
                Ok(()) => true,
                Err(e) if e.kind() == std::io::ErrorKind::CrossesDevices => {
                    match copy_recursive(src, &dest) {
                        Ok(()) => {
                            let del = if src.is_dir() {
                                fs::remove_dir_all(src)
                            } else {
                                fs::remove_file(src)
                            };
                            if let Err(de) = del {
                                errors.push(format!(
                                    "\"{}\" was copied but source could not be removed: {de}",
                                    file_name
                                ));
                            }
                            true
                        }
                        Err(ce) => {
                            let _ = if dest.is_dir() {
                                fs::remove_dir_all(&dest)
                            } else {
                                fs::remove_file(&dest)
                            };
                            errors.push(format!("\"{}\" could not be moved: {ce}", file_name));
                            false
                        }
                    }
                }
                Err(e) => {
                    errors.push(format!("\"{}\" could not be moved: {e}", file_name));
                    false
                }
            },
        };

        if ok {
            completed += 1;
        }

        if !send_paste_progress(result_tx, request.token, completed, &mut last_progress_at) {
            break;
        }
    }

    (completed, errors, stopped_early)
}

/// Send a throttled intermediate progress result for the paste worker.
/// Returns `false` if the receiver has been dropped (loop should break).
fn send_paste_progress(
    result_tx: &mpsc::Sender<JobResult>,
    token: u64,
    completed: usize,
    last_progress_at: &mut Option<Instant>,
) -> bool {
    let now = Instant::now();
    let due = last_progress_at.is_none_or(|t| now.duration_since(t) >= PROGRESS_SEND_INTERVAL);
    if due {
        *last_progress_at = Some(now);
        return result_tx
            .send(JobResult::Paste(PasteBuild {
                token,
                completed,
                done: false,
                status: None,
            }))
            .is_ok();
    }
    true
}

/// Return a destination path inside `dir` for an item named `name` that does
/// not collide with any existing file.
fn unique_dest(dir: &Path, name: &str) -> PathBuf {
    let first = dir.join(name);
    if !first.exists() {
        return first;
    }
    let base = Path::new(name);
    let stem = base.file_stem().and_then(|s| s.to_str()).unwrap_or(name);
    let ext = base.extension().and_then(|s| s.to_str());
    next_literal_dest(dir, stem, ext)
}

fn next_literal_dest(dir: &Path, stem: &str, ext: Option<&str>) -> PathBuf {
    for i in 1u32.. {
        let path = dir.join(format_copy_name(stem, ext, Some(i)));
        if !path.exists() {
            return path;
        }
    }
    dir.join(format_copy_name(stem, ext, None))
}

fn format_copy_name(stem: &str, ext: Option<&str>, suffix: Option<u32>) -> String {
    let name = match suffix {
        Some(index) => format!("{stem}_{index}"),
        None => stem.to_string(),
    };
    match ext {
        Some(ext) => format!("{name}.{ext}"),
        None => name,
    }
}

/// Recursively copy `src` to `dest`.
fn copy_recursive(src: &Path, dest: &Path) -> anyhow::Result<()> {
    if src.is_dir() {
        fs::create_dir_all(dest)
            .map_err(|e| anyhow::anyhow!("Cannot create directory \"{}\": {e}", dest.display()))?;
        for entry_result in fs::read_dir(src)
            .map_err(|e| anyhow::anyhow!("Cannot read \"{}\": {e}", src.display()))?
        {
            let child = entry_result
                .map_err(|e| anyhow::anyhow!("Cannot read entry in \"{}\": {e}", src.display()))?;
            copy_recursive(&child.path(), &dest.join(child.file_name()))?;
        }
    } else {
        fs::copy(src, dest).map_err(|e| {
            anyhow::anyhow!(
                "Cannot copy \"{}\" to \"{}\": {e}",
                src.display(),
                dest.display()
            )
        })?;
    }
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::{
        fs,
        time::{SystemTime, UNIX_EPOCH},
    };

    fn temp_path(label: &str) -> PathBuf {
        let unique = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .expect("system time should be after unix epoch")
            .as_nanos();
        std::env::temp_dir().join(format!("elio-paste-{label}-{unique}"))
    }

    #[test]
    fn duplicate_plain_name_uses_underscore_suffixes() {
        let dir = temp_path("plain");
        fs::create_dir_all(&dir).expect("failed to create temp dir");
        fs::write(dir.join("example"), "data").expect("failed to write source");

        assert_eq!(unique_dest(&dir, "example"), dir.join("example_1"));

        fs::remove_dir_all(&dir).expect("failed to remove temp dir");
    }

    #[test]
    fn duplicate_original_skips_to_next_available_suffix() {
        let dir = temp_path("next-available");
        fs::create_dir_all(&dir).expect("failed to create temp dir");
        fs::write(dir.join("example"), "base").expect("failed to write base file");
        fs::write(dir.join("example_1"), "copy").expect("failed to write suffixed file");

        assert_eq!(unique_dest(&dir, "example"), dir.join("example_2"));

        fs::remove_dir_all(&dir).expect("failed to remove temp dir");
    }

    #[test]
    fn duplicate_suffixed_name_stays_literal() {
        let dir = temp_path("nested-suffix");
        fs::create_dir_all(&dir).expect("failed to create temp dir");
        fs::write(dir.join("aur_1"), "report").expect("failed to write source");

        assert_eq!(unique_dest(&dir, "aur_1"), dir.join("aur_1_1"));

        fs::remove_dir_all(&dir).expect("failed to remove temp dir");
    }

    #[test]
    fn duplicate_suffixed_name_with_extension_stays_literal() {
        let dir = temp_path("nested-suffix-ext");
        fs::create_dir_all(&dir).expect("failed to create temp dir");
        fs::write(dir.join("aur_1.txt"), "copy").expect("failed to write source");

        assert_eq!(unique_dest(&dir, "aur_1.txt"), dir.join("aur_1_1.txt"));

        fs::remove_dir_all(&dir).expect("failed to remove temp dir");
    }
}