edirstat 2.0.1

A fast, cross-platform disk usage analyzer and deduplicator—with work-stealing multithreading, zero-copy snapshots, and an interactive treemap GUI.
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
use std::{
    borrow::Cow,
    path::Path,
    sync::{Arc, mpsc::Sender},
};

use egui_table_kit::{
    error::TableError,
    operations::{OperationContext, TableOperation, TableOperationEnablement},
};

use crate::{arena::FileArenaSnapshot, coordinator::SharedState};

/// Decoupled actions sent from the `TableOperations` directly to the main `GuiApp` loop.
#[derive(Debug, Clone)]
pub enum AppCommand {
    RefreshSubtrees(Vec<u32>),
    ScrollToSelected,
    ShowTrashModal(Vec<u32>),
    ShowDeleteModal(Vec<u32>),
}

// Helper to retrieve the current snapshot safely
fn get_snapshot(shared_state: &Arc<SharedState>) -> Arc<FileArenaSnapshot> {
    shared_state.current_snapshot.load().clone()
}

// --- Up One Level ---
#[derive(Debug)]
pub struct UpOneLevelOp {
    shared_state: Arc<SharedState>,
    command_tx: Sender<AppCommand>,
}

impl UpOneLevelOp {
    pub const fn new(shared_state: Arc<SharedState>, command_tx: Sender<AppCommand>) -> Self {
        Self {
            shared_state,
            command_tx,
        }
    }
}

impl TableOperation for UpOneLevelOp {
    fn name(&self) -> Cow<'_, str> {
        Cow::Borrowed("Up One Level")
    }

    fn icon(&self) -> &'static str {
        ""
    }

    fn enabled(&self) -> TableOperationEnablement {
        TableOperationEnablement::OneSelected
    }

    fn exec(&mut self, ctx: &mut OperationContext<'_, '_>) -> Result<(), TableError> {
        let snapshot = get_snapshot(&self.shared_state);

        if let Some(idx) = ctx.data.selected_rows.iter().next()
            && (idx as usize) < snapshot.nodes.len()
        {
            let parent = snapshot.nodes[idx as usize].parent;
            if parent == crate::arena::NO_INDEX {
                crate::gui::toast_warning("Already at the root level");
            } else {
                ctx.data.selected_rows.clear();
                ctx.data.selected_rows.insert(parent);
                let _ = self.command_tx.send(AppCommand::ScrollToSelected);
                crate::gui::toast_info("Navigated up one level");
            }
        }
        Ok(())
    }
}

// --- Refresh Entire Scan (Root) ---
#[derive(Debug)]
pub struct RefreshRootOp {
    shared_state: Arc<SharedState>,
    command_tx: Sender<AppCommand>,
}

impl RefreshRootOp {
    pub const fn new(shared_state: Arc<SharedState>, command_tx: Sender<AppCommand>) -> Self {
        Self {
            shared_state,
            command_tx,
        }
    }
}

impl TableOperation for RefreshRootOp {
    fn name(&self) -> Cow<'_, str> {
        Cow::Borrowed("Refresh Entire Scan")
    }

    fn icon(&self) -> &'static str {
        "🔁"
    }

    // Always enabled, regardless of whether a row is selected
    fn enabled(&self) -> TableOperationEnablement {
        TableOperationEnablement::Always
    }

    fn exec(&mut self, _ctx: &mut OperationContext<'_, '_>) -> Result<(), TableError> {
        let snapshot = get_snapshot(&self.shared_state);

        // Safety check to ensure we only refresh if a tree is actually loaded
        if !snapshot.nodes.is_empty() {
            // The root node is always strictly at index 0 in the arena
            let _ = self.command_tx.send(AppCommand::RefreshSubtrees(vec![0]));
            crate::gui::toast_info("Refreshing entire scan...");
        }

        Ok(())
    }
}

// --- Refresh Directory ---
#[derive(Debug)]
pub struct RefreshDirectoryOp {
    shared_state: Arc<SharedState>,
    command_tx: Sender<AppCommand>,
}

impl RefreshDirectoryOp {
    pub const fn new(shared_state: Arc<SharedState>, command_tx: Sender<AppCommand>) -> Self {
        Self {
            shared_state,
            command_tx,
        }
    }
}

impl TableOperation for RefreshDirectoryOp {
    fn name(&self) -> Cow<'_, str> {
        Cow::Borrowed("Refresh Directory")
    }

    fn icon(&self) -> &'static str {
        "🔄"
    }

    fn enabled(&self) -> TableOperationEnablement {
        TableOperationEnablement::AtLeastOneSelected
    }

    fn exec(&mut self, ctx: &mut OperationContext<'_, '_>) -> Result<(), TableError> {
        let snapshot = get_snapshot(&self.shared_state);

        let dirs: Vec<u32> = ctx
            .data
            .selected_rows
            .iter()
            .filter(|&idx| {
                (idx as usize) < snapshot.nodes.len() && snapshot.nodes[idx as usize].is_directory()
            })
            .collect();

        if !dirs.is_empty() {
            let _ = self.command_tx.send(AppCommand::RefreshSubtrees(dirs));
            crate::gui::toast_info("Refreshing selected directory/directories...");
        }
        Ok(())
    }
}

// --- Open in File Manager ---
#[derive(Debug)]
pub struct OpenFileManagerOp {
    shared_state: Arc<SharedState>,
}

impl OpenFileManagerOp {
    pub const fn new(shared_state: Arc<SharedState>) -> Self {
        Self { shared_state }
    }
}

impl TableOperation for OpenFileManagerOp {
    fn name(&self) -> Cow<'_, str> {
        Cow::Borrowed("Open in File Manager")
    }

    fn icon(&self) -> &'static str {
        "🗁"
    }

    fn enabled(&self) -> TableOperationEnablement {
        TableOperationEnablement::OneSelected
    }

    fn exec(&mut self, ctx: &mut OperationContext<'_, '_>) -> Result<(), TableError> {
        let snapshot = get_snapshot(&self.shared_state);

        if let Some(idx) = ctx.data.selected_rows.iter().next() {
            let path_str = snapshot.get_full_path(idx);
            let path = Path::new(&path_str);
            let dir_to_open = if path.is_dir() {
                path
            } else {
                path.parent().map_or(path, |p| p)
            };
            match open::that(dir_to_open) {
                Ok(()) => {
                    let path_lossy = dir_to_open.to_string_lossy();
                    let cleaned_path = crate::model::arena::clean_unc_path(&path_lossy);
                    crate::gui::toast_info(format!("Opened in file manager: {cleaned_path}"));
                }
                Err(e) => crate::gui::toast_error(format!("Failed to open in file manager: {e}")),
            }
        }
        Ok(())
    }
}

// --- Open Terminal Here ---
#[derive(Debug)]
pub struct OpenTerminalOp {
    shared_state: Arc<SharedState>,
}

impl OpenTerminalOp {
    pub const fn new(shared_state: Arc<SharedState>) -> Self {
        Self { shared_state }
    }
}

impl TableOperation for OpenTerminalOp {
    fn name(&self) -> Cow<'_, str> {
        Cow::Borrowed("Open Terminal Here")
    }

    fn icon(&self) -> &'static str {
        "💻"
    }

    fn enabled(&self) -> TableOperationEnablement {
        TableOperationEnablement::OneSelected
    }

    fn exec(&mut self, ctx: &mut OperationContext<'_, '_>) -> Result<(), TableError> {
        let snapshot = get_snapshot(&self.shared_state);

        if let Some(idx) = ctx.data.selected_rows.iter().next()
            && (idx as usize) < snapshot.nodes.len()
            && snapshot.nodes[idx as usize].is_directory()
        {
            let path_str = snapshot.get_full_path(idx);
            match super::open_terminal_at(Path::new(&path_str)) {
                Ok(()) => crate::gui::toast_info(format!("Opened terminal at: {path_str}")),
                Err(e) => crate::gui::toast_error(format!("Failed to open terminal: {e}")),
            }
        }
        Ok(())
    }
}

// --- Copy Full Path ---
#[derive(Debug)]
pub struct CopyPathOp {
    shared_state: Arc<SharedState>,
}

impl CopyPathOp {
    pub const fn new(shared_state: Arc<SharedState>) -> Self {
        Self { shared_state }
    }
}

impl TableOperation for CopyPathOp {
    fn name(&self) -> Cow<'_, str> {
        Cow::Borrowed("Copy Path")
    }

    fn icon(&self) -> &'static str {
        "📎"
    }

    fn enabled(&self) -> TableOperationEnablement {
        TableOperationEnablement::AtLeastOneSelected
    }

    fn exec(&mut self, ctx: &mut OperationContext<'_, '_>) -> Result<(), TableError> {
        let snapshot = get_snapshot(&self.shared_state);

        let mut paths = Vec::new();
        let mut selected: Vec<u32> = ctx.data.selected_rows.iter().collect();
        selected.sort_unstable();
        for idx in selected {
            let path_str = snapshot.get_full_path(idx);
            paths.push(crate::model::arena::clean_unc_path(&path_str).into_owned());
        }

        let num_paths = paths.len();
        ctx.ui.ctx().copy_text(paths.join("\n"));
        crate::gui::toast_success(format!("Copied {num_paths} path(s) to clipboard"));
        Ok(())
    }
}

// --- Copy Name Only ---
#[derive(Debug)]
pub struct CopyNameOp {
    shared_state: Arc<SharedState>,
}

impl CopyNameOp {
    pub const fn new(shared_state: Arc<SharedState>) -> Self {
        Self { shared_state }
    }
}

impl TableOperation for CopyNameOp {
    fn name(&self) -> Cow<'_, str> {
        Cow::Borrowed("Copy Name")
    }

    fn icon(&self) -> &'static str {
        "📋"
    }

    fn enabled(&self) -> TableOperationEnablement {
        TableOperationEnablement::AtLeastOneSelected
    }

    fn exec(&mut self, ctx: &mut OperationContext<'_, '_>) -> Result<(), TableError> {
        let snapshot = get_snapshot(&self.shared_state);

        let mut names = Vec::new();
        let mut selected: Vec<u32> = ctx.data.selected_rows.iter().collect();
        selected.sort_unstable();
        for idx in selected {
            if (idx as usize) < snapshot.nodes.len() {
                let node = &snapshot.nodes[idx as usize];
                let name = snapshot.string_pool.get(node.name_id).unwrap_or("unknown");
                let cleaned_name = if node.parent_opt().is_none() {
                    crate::model::arena::clean_unc_path(name).into_owned()
                } else {
                    name.to_string()
                };
                names.push(cleaned_name);
            }
        }

        let num_names = names.len();
        ctx.ui.ctx().copy_text(names.join("\n"));
        crate::gui::toast_success(format!("Copied {num_names} name(s) to clipboard"));
        Ok(())
    }
}

// --- Trash Selected ---
#[derive(Debug)]
pub struct TrashSelectedOp {
    command_tx: Sender<AppCommand>,
}

impl TrashSelectedOp {
    #[must_use]
    pub const fn new(command_tx: Sender<AppCommand>) -> Self {
        Self { command_tx }
    }
}

impl TableOperation for TrashSelectedOp {
    fn name(&self) -> Cow<'_, str> {
        Cow::Borrowed("Move to Trash")
    }

    fn icon(&self) -> &'static str {
        ""
    }

    fn enabled(&self) -> TableOperationEnablement {
        TableOperationEnablement::AtLeastOneSelected
    }

    fn exec(&mut self, ctx: &mut OperationContext<'_, '_>) -> Result<(), TableError> {
        let targets: Vec<u32> = ctx.data.selected_rows.iter().collect();
        let _ = self.command_tx.send(AppCommand::ShowTrashModal(targets));
        Ok(())
    }
}

// --- Permanently Delete Selected ---
#[derive(Debug)]
pub struct DeleteSelectedOp {
    command_tx: Sender<AppCommand>,
}

impl DeleteSelectedOp {
    #[must_use]
    pub const fn new(command_tx: Sender<AppCommand>) -> Self {
        Self { command_tx }
    }
}

impl TableOperation for DeleteSelectedOp {
    fn name(&self) -> Cow<'_, str> {
        Cow::Borrowed("Permanently Delete")
    }

    fn icon(&self) -> &'static str {
        "🗑"
    }

    fn enabled(&self) -> TableOperationEnablement {
        TableOperationEnablement::AtLeastOneSelected
    }

    fn exec(&mut self, ctx: &mut OperationContext<'_, '_>) -> Result<(), TableError> {
        let targets: Vec<u32> = ctx.data.selected_rows.iter().collect();
        let _ = self.command_tx.send(AppCommand::ShowDeleteModal(targets));
        Ok(())
    }
}