rusty-sweeper 0.4.0

A Linux disk usage management utility
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
436
437
438
439
440
441
442
443
444
445
446
447
448
use super::entry::DirEntry;
use super::size::format_size;

/// Format options for tree output
#[derive(Debug, Clone)]
pub struct FormatOptions {
    /// Maximum depth to display
    pub max_depth: Option<usize>,
    /// Show only top N entries per directory
    pub top_n: Option<usize>,
    /// Use colors in output
    pub colors: bool,
    /// Show file counts
    pub show_counts: bool,
}

impl Default for FormatOptions {
    fn default() -> Self {
        Self {
            max_depth: Some(3),
            top_n: Some(20),
            colors: true,
            show_counts: false,
        }
    }
}

impl FormatOptions {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn with_max_depth(mut self, depth: usize) -> Self {
        self.max_depth = Some(depth);
        self
    }

    pub fn with_top_n(mut self, n: usize) -> Self {
        self.top_n = Some(n);
        self
    }

    pub fn with_colors(mut self, enabled: bool) -> Self {
        self.colors = enabled;
        self
    }

    pub fn with_counts(mut self, show: bool) -> Self {
        self.show_counts = show;
        self
    }

    pub fn unlimited() -> Self {
        Self {
            max_depth: None,
            top_n: None,
            colors: false,
            show_counts: false,
        }
    }
}

/// Format entry as a tree string
pub fn format_tree(entry: &DirEntry, options: &FormatOptions) -> String {
    let mut output = String::new();
    format_tree_recursive(entry, &mut output, "", true, 0, options);
    output
}

fn format_tree_recursive(
    entry: &DirEntry,
    output: &mut String,
    prefix: &str,
    is_last: bool,
    depth: usize,
    options: &FormatOptions,
) {
    // Check depth limit
    if let Some(max_depth) = options.max_depth {
        if depth > max_depth {
            return;
        }
    }

    // Build the current line
    let connector = if depth == 0 {
        ""
    } else if is_last {
        "└── "
    } else {
        "├── "
    };

    let size_str = format_size(entry.size);
    let name = if entry.is_dir {
        format!("{}/", entry.name)
    } else {
        entry.name.clone()
    };

    // Error indicator
    let error_indicator = if entry.error.is_some() { " [!]" } else { "" };

    // Count indicator
    let count_str = if options.show_counts && entry.is_dir {
        format!(" ({} files)", entry.file_count)
    } else {
        String::new()
    };

    output.push_str(&format!(
        "{}{}{:>10}  {}{}{}\n",
        prefix, connector, size_str, name, count_str, error_indicator
    ));

    // Process children
    if entry.is_dir && !entry.children.is_empty() {
        let new_prefix = if depth == 0 {
            String::new()
        } else if is_last {
            format!("{}    ", prefix)
        } else {
            format!("{}│   ", prefix)
        };

        let children_to_show: Vec<_> = if let Some(top_n) = options.top_n {
            entry.children.iter().take(top_n).collect()
        } else {
            entry.children.iter().collect()
        };

        let total = children_to_show.len();
        let has_more = options.top_n.is_some_and(|n| entry.children.len() > n);

        for (i, child) in children_to_show.iter().enumerate() {
            let is_last_child = i == total - 1 && !has_more;
            format_tree_recursive(
                child,
                output,
                &new_prefix,
                is_last_child,
                depth + 1,
                options,
            );
        }

        // Show truncation indicator if needed
        if has_more {
            let remaining = entry.children.len() - options.top_n.unwrap();
            output.push_str(&format!(
                "{}└── ... and {} more entries\n",
                new_prefix, remaining
            ));
        }
    }
}

/// Format entry as a simple table
pub fn format_table(entry: &DirEntry, options: &FormatOptions) -> String {
    let mut output = String::new();

    output.push_str(&format!("{:>12}  {}\n", "SIZE", "PATH"));
    output.push_str(&format!("{:->12}  {:-<50}\n", "", ""));

    format_table_recursive(entry, &mut output, 0, options);

    output
}

fn format_table_recursive(
    entry: &DirEntry,
    output: &mut String,
    depth: usize,
    options: &FormatOptions,
) {
    if let Some(max_depth) = options.max_depth {
        if depth > max_depth {
            return;
        }
    }

    let size_str = format_size(entry.size);
    let indent = "  ".repeat(depth);

    output.push_str(&format!("{:>12}  {}{}\n", size_str, indent, entry.name));

    let children_to_show: Vec<_> = if let Some(top_n) = options.top_n {
        entry.children.iter().take(top_n).collect()
    } else {
        entry.children.iter().collect()
    };

    for child in children_to_show {
        format_table_recursive(child, output, depth + 1, options);
    }

    // Show truncation indicator if needed
    if let Some(top_n) = options.top_n {
        if entry.children.len() > top_n {
            let remaining = entry.children.len() - top_n;
            let indent = "  ".repeat(depth + 1);
            output.push_str(&format!("{:>12}  {}... {} more\n", "", indent, remaining));
        }
    }
}

/// Format entry as JSON
pub fn format_json(entry: &DirEntry, pretty: bool) -> Result<String, serde_json::Error> {
    if pretty {
        serde_json::to_string_pretty(entry)
    } else {
        serde_json::to_string(entry)
    }
}

/// Simplified JSON structure for large outputs
#[derive(serde::Serialize)]
pub struct SummarizedEntry {
    pub path: String,
    pub size: u64,
    pub size_human: String,
    pub file_count: u64,
    pub dir_count: u64,
    pub children: Vec<SummarizedEntry>,
}

impl From<&DirEntry> for SummarizedEntry {
    fn from(entry: &DirEntry) -> Self {
        Self {
            path: entry.path.to_string_lossy().to_string(),
            size: entry.size,
            size_human: format_size(entry.size),
            file_count: entry.file_count,
            dir_count: entry.dir_count,
            children: entry.children.iter().map(SummarizedEntry::from).collect(),
        }
    }
}

/// Format as summarized JSON (smaller output)
pub fn format_json_summary(entry: &DirEntry, pretty: bool) -> Result<String, serde_json::Error> {
    let summary = SummarizedEntry::from(entry);
    if pretty {
        serde_json::to_string_pretty(&summary)
    } else {
        serde_json::to_string(&summary)
    }
}

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

    fn create_test_entry() -> DirEntry {
        let mut root = DirEntry::new_dir(PathBuf::from("/test"), None);

        let mut subdir = DirEntry::new_dir(PathBuf::from("/test/subdir"), None);
        subdir.children.push(DirEntry::new_file(
            PathBuf::from("/test/subdir/file.txt"),
            1024,
            4096,
            None,
        ));
        subdir.recalculate_totals();

        root.children.push(DirEntry::new_file(
            PathBuf::from("/test/large.bin"),
            1048576,
            1048576,
            None,
        ));
        root.children.push(subdir);
        root.recalculate_totals();
        root.sort_by_size();

        root
    }

    #[test]
    fn test_format_tree_basic() {
        let entry = create_test_entry();
        let options = FormatOptions::default();
        let output = format_tree(&entry, &options);

        assert!(output.contains("test/"));
        assert!(output.contains("large.bin"));
        assert!(output.contains("subdir/"));
        assert!(output.contains("1.00 MB")); // large.bin
    }

    #[test]
    fn test_format_tree_contains_structure() {
        let entry = create_test_entry();
        let options = FormatOptions::unlimited();
        let output = format_tree(&entry, &options);

        // Should have tree characters
        assert!(output.contains("├──") || output.contains("└──"));
    }

    #[test]
    fn test_format_tree_depth_limit() {
        let entry = create_test_entry();
        let options = FormatOptions {
            max_depth: Some(1),
            ..Default::default()
        };
        let output = format_tree(&entry, &options);

        // Should show root and immediate children, but not nested file
        assert!(output.contains("subdir/"));
        assert!(!output.contains("file.txt"));
    }

    #[test]
    fn test_format_tree_with_counts() {
        let entry = create_test_entry();
        let options = FormatOptions::new().with_counts(true);
        let output = format_tree(&entry, &options);

        assert!(output.contains("files)"));
    }

    #[test]
    fn test_format_tree_top_n() {
        let mut root = DirEntry::new_dir(PathBuf::from("/test"), None);
        for i in 0..10 {
            root.children.push(DirEntry::new_file(
                PathBuf::from(format!("/test/file{}.txt", i)),
                1000 * (10 - i as u64),
                4096,
                None,
            ));
        }
        root.recalculate_totals();
        root.sort_by_size();

        let options = FormatOptions::new().with_top_n(3);
        let output = format_tree(&root, &options);

        assert!(output.contains("file0.txt")); // largest
        assert!(output.contains("file1.txt"));
        assert!(output.contains("file2.txt"));
        assert!(!output.contains("file9.txt")); // smallest, truncated
        assert!(output.contains("7 more entries"));
    }

    #[test]
    fn test_format_tree_error_entry() {
        let mut root = DirEntry::new_dir(PathBuf::from("/test"), None);
        root.children.push(DirEntry::new_error(
            PathBuf::from("/test/forbidden"),
            "Permission denied".to_string(),
        ));
        root.recalculate_totals();

        let options = FormatOptions::default();
        let output = format_tree(&root, &options);

        assert!(output.contains("[!]"));
    }

    #[test]
    fn test_format_table_basic() {
        let entry = create_test_entry();
        let options = FormatOptions::default();
        let output = format_table(&entry, &options);

        assert!(output.contains("SIZE"));
        assert!(output.contains("PATH"));
        assert!(output.contains("1.00 MB"));
        assert!(output.contains("test"));
    }

    #[test]
    fn test_format_table_indentation() {
        let entry = create_test_entry();
        let options = FormatOptions::unlimited();
        let output = format_table(&entry, &options);

        // Check that nested entries are indented
        let lines: Vec<&str> = output.lines().collect();
        // subdir should have more indentation than root
        let subdir_line = lines.iter().find(|l| l.contains("subdir")).unwrap();
        let root_line = lines
            .iter()
            .find(|l| l.contains("test") && !l.contains("subdir"))
            .unwrap();

        // subdir should appear after more spaces than root name
        assert!(subdir_line.find("subdir").unwrap() > root_line.find("test").unwrap());
    }

    #[test]
    fn test_format_json() {
        let entry = create_test_entry();
        let json = format_json(&entry, false).unwrap();

        assert!(json.contains("\"path\""));
        assert!(json.contains("\"size\""));
        assert!(json.contains("\"children\""));
    }

    #[test]
    fn test_format_json_pretty() {
        let entry = create_test_entry();
        let json = format_json(&entry, true).unwrap();

        // Pretty JSON should have newlines
        assert!(json.contains('\n'));
    }

    #[test]
    fn test_format_json_summary() {
        let entry = create_test_entry();
        let json = format_json_summary(&entry, false).unwrap();

        assert!(json.contains("size_human"));
        assert!(json.contains("1.00 MB") || json.contains("1 MB"));
    }

    #[test]
    fn test_format_json_summary_structure() {
        let entry = create_test_entry();
        let json = format_json_summary(&entry, true).unwrap();

        // Should have the expected fields
        assert!(json.contains("\"path\""));
        assert!(json.contains("\"size\""));
        assert!(json.contains("\"size_human\""));
        assert!(json.contains("\"file_count\""));
        assert!(json.contains("\"dir_count\""));
        assert!(json.contains("\"children\""));
    }

    #[test]
    fn test_format_json_parseable() {
        let entry = create_test_entry();
        let json = format_json(&entry, false).unwrap();

        // Verify it's valid JSON by parsing it
        let parsed: serde_json::Value = serde_json::from_str(&json).unwrap();
        assert!(parsed.is_object());
        assert!(parsed["path"].is_string());
        assert!(parsed["size"].is_number());
    }
}