elio 1.0.0

Terminal-native file manager with rich previews, inline images, and mouse 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
427
428
429
430
431
432
433
434
435
use super::*;
use crate::preview::{PreviewRequestOptions, PreviewWorkClass};
use std::{
    collections::{HashSet, VecDeque},
    path::PathBuf,
    sync::{
        Arc, Condvar, Mutex,
        atomic::{AtomicBool, Ordering},
        mpsc,
    },
    thread,
    time::{Instant, SystemTime},
};

const MAX_CONCURRENT_LOW_PRIORITY_HEAVY_PREVIEWS: usize = 2;

pub(in crate::app::jobs) struct PreviewPool {
    shared: Arc<PreviewShared>,
    workers: Vec<thread::JoinHandle<()>>,
    metrics: Arc<Mutex<SchedulerMetrics>>,
}

struct PreviewShared {
    state: Mutex<PreviewState>,
    available: Condvar,
}

struct PreviewState {
    pending_high: VecDeque<PreviewRequest>,
    pending_low: VecDeque<PreviewRequest>,
    queued_high_keys: HashSet<PreviewJobKey>,
    queued_low_keys: HashSet<PreviewJobKey>,
    active_keys: HashSet<PreviewJobKey>,
    active_jobs: Vec<ActivePreviewJob>,
    closed: bool,
    capacity: usize,
}

#[derive(Clone, Debug)]
struct ActivePreviewJob {
    key: PreviewJobKey,
    priority: PreviewPriority,
    work_class: PreviewWorkClass,
    canceled: Arc<AtomicBool>,
}

#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub(in crate::app::jobs) struct PreviewJobKey {
    pub(in crate::app::jobs) path: PathBuf,
    pub(in crate::app::jobs) size: u64,
    pub(in crate::app::jobs) modified: Option<SystemTime>,
    pub(in crate::app::jobs) variant: PreviewRequestOptions,
    pub(in crate::app::jobs) code_line_limit: usize,
    /// Included so that an initial partial render and its extension job are
    /// treated as distinct keys and not deduplicated against each other.
    pub(in crate::app::jobs) code_render_limit: usize,
}

impl PreviewPool {
    pub(in crate::app::jobs) fn new(
        worker_count: usize,
        capacity: usize,
        result_tx: mpsc::Sender<JobResult>,
        metrics: Arc<Mutex<SchedulerMetrics>>,
    ) -> Self {
        let shared = Arc::new(PreviewShared {
            state: Mutex::new(PreviewState {
                pending_high: VecDeque::new(),
                pending_low: VecDeque::new(),
                queued_high_keys: HashSet::new(),
                queued_low_keys: HashSet::new(),
                active_keys: HashSet::new(),
                active_jobs: Vec::new(),
                closed: false,
                capacity,
            }),
            available: Condvar::new(),
        });
        let mut workers = Vec::with_capacity(worker_count);
        for _ in 0..worker_count {
            let shared = Arc::clone(&shared);
            let result_tx = result_tx.clone();
            let metrics = Arc::clone(&metrics);
            workers.push(thread::spawn(move || {
                while let Some((request, canceled)) = PreviewShared::pop(&shared) {
                    let key = PreviewJobKey::from_request(&request);
                    let started_at = Instant::now();
                    let result = crate::preview::build_preview_with_options_and_code_line_limit(
                        &request.entry,
                        &request.variant,
                        request.code_line_limit,
                        request.code_render_limit,
                        request.ffprobe_available,
                        request.ffmpeg_available,
                        &|| canceled.load(Ordering::Relaxed),
                    );
                    PreviewShared::finish(&shared, &key);
                    lock_unpoison(&metrics).record_preview_completed(started_at.elapsed());
                    if canceled.load(Ordering::Relaxed) {
                        continue;
                    }
                    if result_tx
                        .send(JobResult::Preview(Box::new(PreviewBuild {
                            token: request.token,
                            entry: request.entry,
                            variant: request.variant,
                            code_line_limit: request.code_line_limit,
                            code_render_limit: request.code_render_limit,
                            result,
                        })))
                        .is_err()
                    {
                        break;
                    }
                }
            }));
        }
        Self {
            shared,
            workers,
            metrics,
        }
    }

    pub(in crate::app::jobs) fn submit(&self, request: PreviewRequest) -> bool {
        let key = PreviewJobKey::from_request(&request);
        let mut state = lock_unpoison(&self.shared.state);
        if state.closed {
            return false;
        }
        match request.priority {
            PreviewPriority::High => {
                if state.queued_high_keys.contains(&key) {
                    replace_preview_request(&mut state.pending_high, request, &key);
                    return true;
                }
                if state.queued_low_keys.remove(&key) {
                    remove_preview_request(&mut state.pending_low, &key);
                    lock_unpoison(&self.metrics).preview_promotions += 1;
                }
                let evicted = trim_preview_queue_for_high(&mut state);
                cancel_stale_active_previews(&state, &key);
                state.queued_high_keys.insert(key);
                state.pending_high.push_back(request);
                let mut metrics = lock_unpoison(&self.metrics);
                metrics.preview_jobs_submitted_high += 1;
                metrics.preview_low_priority_evictions += evicted;
            }
            PreviewPriority::Low => {
                if state.queued_high_keys.contains(&key) {
                    replace_preview_request_with_priority(
                        &mut state.pending_high,
                        request,
                        &key,
                        PreviewPriority::High,
                    );
                    return true;
                }
                if state.queued_low_keys.contains(&key) {
                    replace_preview_request(&mut state.pending_low, request, &key);
                    return true;
                }
                if preview_active_contains(&state, &key) {
                    return true;
                }
                let evicted = if preview_pending_len(&state) >= state.capacity {
                    u64::from(evict_oldest_low_priority_preview(&mut state))
                } else {
                    0
                };
                if preview_pending_len(&state) >= state.capacity && evicted == 0 {
                    return true;
                }
                state.queued_low_keys.insert(key);
                state.pending_low.push_back(request);
                let mut metrics = lock_unpoison(&self.metrics);
                metrics.preview_jobs_submitted_low += 1;
                metrics.preview_low_priority_evictions += evicted;
            }
        }
        self.shared.available.notify_one();
        true
    }

    pub(in crate::app::jobs) fn has_pending_work(&self) -> bool {
        let state = lock_unpoison(&self.shared.state);
        !state.pending_high.is_empty()
            || !state.pending_low.is_empty()
            || !state.active_keys.is_empty()
    }

    #[cfg(test)]
    pub(in crate::app::jobs) fn pending_keys(
        &self,
        priority: PreviewPriority,
    ) -> Vec<PreviewJobKey> {
        let state = lock_unpoison(&self.shared.state);
        let queue = match priority {
            PreviewPriority::High => &state.pending_high,
            PreviewPriority::Low => &state.pending_low,
        };
        queue.iter().map(PreviewJobKey::from_request).collect()
    }

    #[cfg(test)]
    pub(in crate::app::jobs) fn active_keys(&self) -> Vec<PreviewJobKey> {
        let mut keys = lock_unpoison(&self.shared.state)
            .active_jobs
            .iter()
            .map(|job| job.key.clone())
            .collect::<Vec<_>>();
        keys.sort_by(|left, right| left.path.cmp(&right.path));
        keys
    }

    #[cfg(test)]
    pub(in crate::app::jobs) fn pending_len(&self, priority: PreviewPriority) -> usize {
        let state = lock_unpoison(&self.shared.state);
        match priority {
            PreviewPriority::High => state.pending_high.len(),
            PreviewPriority::Low => state.pending_low.len(),
        }
    }

    #[cfg(test)]
    pub(in crate::app::jobs) fn active_len(&self) -> usize {
        lock_unpoison(&self.shared.state).active_jobs.len()
    }

    #[cfg(test)]
    pub(in crate::app::jobs) fn canceled_active_keys(&self) -> Vec<PreviewJobKey> {
        let mut keys = lock_unpoison(&self.shared.state)
            .active_jobs
            .iter()
            .filter(|job| job.canceled.load(Ordering::Relaxed))
            .map(|job| job.key.clone())
            .collect::<Vec<_>>();
        keys.sort_by(|left, right| left.path.cmp(&right.path));
        keys
    }

    #[cfg(test)]
    pub(in crate::app::jobs) fn pop_next_pending_for_tests(&self) -> Option<PreviewRequest> {
        let mut state = lock_unpoison(&self.shared.state);
        if let Some(request) = state.pending_high.pop_front() {
            let key = PreviewJobKey::from_request(&request);
            state.queued_high_keys.remove(&key);
            let _ = start_preview_request(&mut state, key, &request);
            return Some(request);
        }
        pop_low_priority_request(&mut state).map(|(request, _)| request)
    }
}

impl Drop for PreviewPool {
    fn drop(&mut self) {
        {
            let mut state = lock_unpoison(&self.shared.state);
            state.closed = true;
            state.pending_high.clear();
            state.pending_low.clear();
            state.queued_high_keys.clear();
            state.queued_low_keys.clear();
            for job in &state.active_jobs {
                job.canceled.store(true, Ordering::Relaxed);
            }
        }
        self.shared.available.notify_all();
        for worker in self.workers.drain(..) {
            let _ = worker.join();
        }
    }
}

impl PreviewShared {
    fn pop(shared: &Arc<Self>) -> Option<(PreviewRequest, Arc<AtomicBool>)> {
        let mut state = lock_unpoison(&shared.state);
        loop {
            if state.closed {
                return None;
            }
            if let Some(request) = state.pending_high.pop_front() {
                let key = PreviewJobKey::from_request(&request);
                state.queued_high_keys.remove(&key);
                let canceled = start_preview_request(&mut state, key, &request);
                return Some((request, canceled));
            }
            if let Some(request) = pop_low_priority_request(&mut state) {
                return Some(request);
            }
            state = wait_unpoison(&shared.available, state);
        }
    }

    fn finish(shared: &Arc<Self>, key: &PreviewJobKey) {
        let mut state = lock_unpoison(&shared.state);
        state.active_keys.remove(key);
        state.active_jobs.retain(|job| &job.key != key);
        shared.available.notify_all();
    }
}

impl PreviewJobKey {
    fn from_request(request: &PreviewRequest) -> Self {
        Self {
            path: request.entry.path.clone(),
            size: request.entry.size,
            modified: request.entry.modified,
            variant: request.variant.clone(),
            code_line_limit: request.code_line_limit,
            code_render_limit: request.code_render_limit,
        }
    }
}

fn remove_preview_request(queue: &mut VecDeque<PreviewRequest>, key: &PreviewJobKey) {
    if let Some(index) = queue
        .iter()
        .position(|request| PreviewJobKey::from_request(request) == *key)
    {
        queue.remove(index);
    }
}

fn start_preview_request(
    state: &mut PreviewState,
    key: PreviewJobKey,
    request: &PreviewRequest,
) -> Arc<AtomicBool> {
    let canceled = Arc::new(AtomicBool::new(false));
    state.active_keys.insert(key.clone());
    state.active_jobs.push(ActivePreviewJob {
        key,
        priority: request.priority,
        work_class: request.work_class,
        canceled: Arc::clone(&canceled),
    });
    canceled
}

fn pop_low_priority_request(state: &mut PreviewState) -> Option<(PreviewRequest, Arc<AtomicBool>)> {
    let heavy_limit_reached =
        active_low_priority_heavy_count(state) >= MAX_CONCURRENT_LOW_PRIORITY_HEAVY_PREVIEWS;
    let index = if heavy_limit_reached {
        state
            .pending_low
            .iter()
            .position(|request| request.work_class != PreviewWorkClass::Heavy)?
    } else {
        0
    };
    let request = state.pending_low.remove(index)?;
    let key = PreviewJobKey::from_request(&request);
    state.queued_low_keys.remove(&key);
    let canceled = start_preview_request(state, key, &request);
    Some((request, canceled))
}

fn active_low_priority_heavy_count(state: &PreviewState) -> usize {
    state
        .active_jobs
        .iter()
        .filter(|job| {
            job.priority == PreviewPriority::Low && job.work_class == PreviewWorkClass::Heavy
        })
        .count()
}

fn replace_preview_request(
    queue: &mut VecDeque<PreviewRequest>,
    request: PreviewRequest,
    key: &PreviewJobKey,
) {
    let priority = request.priority;
    replace_preview_request_with_priority(queue, request, key, priority);
}

fn replace_preview_request_with_priority(
    queue: &mut VecDeque<PreviewRequest>,
    mut request: PreviewRequest,
    key: &PreviewJobKey,
    priority: PreviewPriority,
) {
    request.priority = priority;
    if let Some(index) = queue
        .iter()
        .position(|queued| PreviewJobKey::from_request(queued) == *key)
    {
        queue[index] = request;
    }
}

fn preview_pending_len(state: &PreviewState) -> usize {
    state.pending_high.len() + state.pending_low.len()
}

fn preview_active_contains(state: &PreviewState, key: &PreviewJobKey) -> bool {
    state.active_jobs.iter().any(|job| job.key == *key)
}

fn cancel_stale_active_previews(state: &PreviewState, keep: &PreviewJobKey) {
    for job in &state.active_jobs {
        if &job.key != keep {
            job.canceled.store(true, Ordering::Relaxed);
        }
    }
}

fn evict_oldest_low_priority_preview(state: &mut PreviewState) -> bool {
    let Some(stale) = state.pending_low.pop_front() else {
        return false;
    };
    state
        .queued_low_keys
        .remove(&PreviewJobKey::from_request(&stale));
    true
}

fn trim_preview_queue_for_high(state: &mut PreviewState) -> u64 {
    let mut evicted = 0;
    while preview_pending_len(state) >= state.capacity {
        if evict_oldest_low_priority_preview(state) {
            evicted += 1;
            continue;
        }

        let Some(stale) = state.pending_high.pop_front() else {
            break;
        };
        state
            .queued_high_keys
            .remove(&PreviewJobKey::from_request(&stale));
    }
    evicted
}