ls-tree 0.2.0

A lightweight, dependency-free clone of the Unix `tree` command, written in Rust.
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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
//! Recursively render a directory hierarchy using the classic `tree` drawing
//! characters (`├──`, `└──`, `│   `) or output machine-readable JSON.
//! Supports colours, file sizes, `.gitignore` awareness, sorting, pruning, and
//! file-type icons. The render target is a generic [`std::io::Write`] so the
//! library can be tested without a terminal or the filesystem.

use std::fs;
use std::io::{self, Write};
use std::path::Path;

use ignore::gitignore::{Gitignore, GitignoreBuilder};
use serde::Serialize;

/// Controls how the tree is gathered and rendered.
#[derive(Debug, Clone)]
pub struct Options {
    pub show_hidden: bool,
    pub max_depth: Option<usize>,
    /// Deprecated: use [`format`](Self::format) instead. When `true` it forces
    /// [`Format::Json`] regardless of `format`.
    pub json: bool,
    pub format: Format,
    pub show_size: bool,
    pub human_readable: bool,
    pub color: bool,
    pub git_ignore: bool,
    pub dirs_only: bool,
    pub sort_by: SortField,
    pub prune: bool,
    pub show_icons: bool,
    pub show_total_size: bool,
}

impl Options {
    /// Return the effective output format, respecting the legacy `json` flag.
    pub fn effective_format(&self) -> Format {
        if self.json {
            Format::Json
        } else {
            self.format
        }
    }
}

impl Default for Options {
    fn default() -> Self {
        let color = std::io::IsTerminal::is_terminal(&std::io::stdout())
            && std::env::var("NO_COLOR").is_err();
        Self {
            show_hidden: false,
            max_depth: None,
            json: false,
            format: Format::default(),
            show_size: false,
            human_readable: false,
            color,
            git_ignore: false,
            dirs_only: false,
            sort_by: SortField::Name,
            prune: false,
            show_icons: false,
            show_total_size: false,
        }
    }
}

#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
pub enum SortField {
    #[default]
    Name,
    Size,
    Time,
}

/// Controls which output format is used to render the tree.
///
/// The default is [`Tree`](Format::Tree). Use [`Flat`](Format::Flat) for a
/// find-style listing or [`Json`](Format::Json) for machine-readable output.
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
pub enum Format {
    #[default]
    Tree,
    Flat,
    Json,
}

#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
pub struct TreeStats {
    pub directories: u64,
    pub files: u64,
}

#[derive(Debug, Serialize)]
pub struct TreeNode {
    name: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    size: Option<u64>,
    #[serde(rename = "type")]
    entry_type: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    target: Option<String>,
    #[serde(skip_serializing_if = "Vec::is_empty")]
    children: Vec<TreeNode>,
}

/// Convenience entry point that selects the built-in formatter based on
/// [`Options::effective_format`] and delegates to [`generate_with_formatter`].
pub fn generate(root: &Path, out: &mut impl Write, opts: &Options) -> io::Result<TreeStats> {
    match opts.effective_format() {
        Format::Tree => generate_with_formatter(root, out, opts, &TreeFormatter),
        Format::Flat => generate_with_formatter(root, out, opts, &FlatFormatter),
        Format::Json => generate_with_formatter(root, out, opts, &JsonFormatter),
    }
}

/// Build the tree and hand it off to a custom [`OutputFormatter`].
///
/// This is the primitive that [`generate`] calls internally. Use it directly
/// when you want to supply your own formatter implementation.
pub fn generate_with_formatter(
    root: &Path,
    out: &mut impl Write,
    opts: &Options,
    formatter: &dyn OutputFormatter,
) -> io::Result<TreeStats> {
    let canonical_root = root.canonicalize().unwrap_or_else(|_| root.to_path_buf());

    let name = root_display_name(root)?;

    match fs::metadata(root) {
        Ok(meta) if meta.is_dir() => {
            let gi = if opts.git_ignore {
                build_gitignore(&canonical_root)
            } else {
                None
            };

            let root_node = match build_tree(root, &canonical_root, opts, 0, &gi) {
                Ok(node) => node,
                Err(e) if e.kind() == io::ErrorKind::PermissionDenied => {
                    writeln!(out, "{}", name)?;
                    writeln!(out, "└── [Access Denied]")?;
                    return Ok(TreeStats {
                        directories: 1,
                        files: 0,
                    });
                }
                Err(e) => return Err(e),
            };

            formatter.write_tree(&root_node, &name, opts, out)
        }
        Ok(_) => Err(io::Error::new(
            io::ErrorKind::InvalidInput,
            "not a directory",
        )),
        Err(ref e) if e.kind() == io::ErrorKind::PermissionDenied => {
            writeln!(out, "{}", name)?;
            writeln!(out, "└── [Access Denied]")?;
            Ok(TreeStats {
                directories: 1,
                files: 0,
            })
        }
        Err(e) => Err(e),
    }
}

fn root_display_name(root: &Path) -> io::Result<String> {
    let empty = root.as_os_str().is_empty() || root.as_os_str() == ".";
    if empty {
        let cwd = std::env::current_dir()?;
        Ok(cwd
            .file_name()
            .and_then(|n| n.to_str())
            .unwrap_or(".")
            .to_string())
    } else {
        Ok(match root.file_name().and_then(|n| n.to_str()) {
            Some(s) => s.to_string(),
            None => root.to_string_lossy().into_owned(),
        })
    }
}

fn build_tree(
    dir: &Path,
    root: &Path,
    opts: &Options,
    depth: usize,
    gitignore: &Option<Gitignore>,
) -> io::Result<TreeNode> {
    let dir_name = dir
        .file_name()
        .and_then(|n| n.to_str())
        .map(|s| s.to_string())
        .unwrap_or_else(|| dir.to_string_lossy().into_owned());

    let read_dir = match fs::read_dir(dir) {
        Ok(rd) => rd,
        Err(e) if e.kind() == io::ErrorKind::PermissionDenied && depth > 0 => {
            return Ok(TreeNode {
                name: dir_name,
                size: None,
                entry_type: "directory".to_string(),
                target: None,
                children: vec![TreeNode {
                    name: "[Access Denied]".to_string(),
                    size: None,
                    entry_type: "file".to_string(),
                    target: None,
                    children: vec![],
                }],
            });
        }
        Err(e) => return Err(e),
    };
    let mut raw: Vec<fs::DirEntry> = read_dir.filter_map(|r| r.ok()).collect();

    if !opts.show_hidden {
        raw.retain(|e| !e.file_name().to_string_lossy().starts_with('.'));
    }

    if let Some(ref gi) = *gitignore {
        raw.retain(|e| {
            let path = e.path();
            let is_dir = e.file_type().map(|t| t.is_dir()).unwrap_or(false);
            !is_ignored(&path, is_dir, root, gi)
        });
    }

    if opts.dirs_only {
        raw.retain(|e| e.file_type().map(|t| t.is_dir()).unwrap_or(false));
    }

    sort_entries(&mut raw, opts.sort_by);

    let mut children: Vec<TreeNode> = Vec::new();

    for entry in &raw {
        let ft = match entry.file_type() {
            Ok(t) => t,
            Err(_) => continue,
        };
        let meta = match entry.metadata() {
            Ok(m) => m,
            Err(_) => continue,
        };
        let name = entry.file_name().to_string_lossy().into_owned();

        if ft.is_symlink() {
            let target = fs::read_link(entry.path())
                .map(|t| t.to_string_lossy().into_owned())
                .unwrap_or_else(|_| "<unknown>".to_string());
            children.push(TreeNode {
                name: format!("{} -> {}", name, target),
                size: if opts.show_size {
                    Some(meta.len())
                } else {
                    None
                },
                entry_type: "symlink".to_string(),
                target: Some(target),
                children: vec![],
            });
        } else if ft.is_dir() {
            let descend = opts.max_depth.is_none_or(|max| depth + 1 < max);
            if descend {
                let child = build_tree(&entry.path(), root, opts, depth + 1, gitignore)?;
                if opts.prune && child.children.is_empty() {
                    continue;
                }
                let child_size = child.size.unwrap_or(0);
                children.push(TreeNode {
                    name,
                    size: if opts.show_size || opts.show_total_size {
                        Some(child_size)
                    } else {
                        None
                    },
                    entry_type: "directory".to_string(),
                    target: None,
                    children: child.children,
                });
            } else {
                children.push(TreeNode {
                    name,
                    size: if opts.show_size {
                        Some(meta.len())
                    } else {
                        None
                    },
                    entry_type: "directory".to_string(),
                    target: None,
                    children: vec![],
                });
            }
        } else {
            children.push(TreeNode {
                name,
                size: if opts.show_size {
                    Some(meta.len())
                } else {
                    None
                },
                entry_type: "file".to_string(),
                target: None,
                children: vec![],
            });
        }
    }

    let total_size: u64 = children.iter().filter_map(|c| c.size).sum();

    Ok(TreeNode {
        name: dir_name,
        size: if opts.show_total_size || (opts.show_size && !children.is_empty()) {
            Some(total_size)
        } else {
            None
        },
        entry_type: "directory".to_string(),
        target: None,
        children,
    })
}

fn count_nodes(node: &TreeNode) -> TreeStats {
    let mut dirs = 0u64;
    let mut files = 0u64;
    if node.entry_type == "directory" {
        dirs += 1;
    } else {
        files += 1;
    }
    for child in &node.children {
        let s = count_nodes(child);
        dirs += s.directories;
        files += s.files;
    }
    TreeStats {
        directories: dirs,
        files,
    }
}

// ---------------------------------------------------------------------------
// Output formatter trait + built-in implementations
// ---------------------------------------------------------------------------

/// Renders a complete [`TreeNode`] tree into the supplied writer.
///
/// Each implementation produces a different output format (tree, flat, JSON).
/// Library consumers can implement this trait to supply custom formats.
pub trait OutputFormatter {
    /// Render a complete tree into `out`.
    ///
    /// Implementations receive the root [`TreeNode`] (already fully built), the
    /// display name of the root path, the active options, and the output writer.
    fn write_tree(
        &self,
        root: &TreeNode,
        root_name: &str,
        opts: &Options,
        out: &mut dyn Write,
    ) -> io::Result<TreeStats>;
}

/// Classic `├──` / `└──` / `│   ` tree output.
pub struct TreeFormatter;

/// Find-style flat listing (one path per line).
pub struct FlatFormatter;

/// Machine-readable JSON output.
pub struct JsonFormatter;

impl OutputFormatter for TreeFormatter {
    fn write_tree(
        &self,
        root: &TreeNode,
        root_name: &str,
        opts: &Options,
        out: &mut dyn Write,
    ) -> io::Result<TreeStats> {
        writeln!(out, "{}", root_name)?;
        let mut stats = TreeStats {
            directories: 1,
            files: 0,
        };
        render_children(&root.children, "", opts, out, &mut stats)?;
        Ok(stats)
    }
}

impl OutputFormatter for JsonFormatter {
    fn write_tree(
        &self,
        root: &TreeNode,
        _root_name: &str,
        _opts: &Options,
        out: &mut dyn Write,
    ) -> io::Result<TreeStats> {
        serde_json::to_writer_pretty(&mut *out, root)?;
        writeln!(out)?;
        Ok(count_nodes(root))
    }
}

impl OutputFormatter for FlatFormatter {
    fn write_tree(
        &self,
        root: &TreeNode,
        root_name: &str,
        opts: &Options,
        out: &mut dyn Write,
    ) -> io::Result<TreeStats> {
        writeln!(out, "{}", root_name)?;
        let mut stats = TreeStats {
            directories: 1,
            files: 0,
        };
        flatten_children(root, "", opts, out, &mut stats)?;
        Ok(stats)
    }
}

/// Recursively print child entries as flat relative paths.
fn flatten_children(
    node: &TreeNode,
    prefix: &str,
    _opts: &Options,
    out: &mut dyn Write,
    stats: &mut TreeStats,
) -> io::Result<()> {
    for child in &node.children {
        let path = if prefix.is_empty() {
            child.name.clone()
        } else {
            format!("{}/{}", prefix, child.name)
        };

        match child.entry_type.as_str() {
            "directory" => {
                writeln!(out, "{}/", path)?;
                stats.directories += 1;
                flatten_children(child, &path, _opts, out, stats)?;
            }
            _ => {
                writeln!(out, "{}", path)?;
                stats.files += 1;
            }
        }
    }
    Ok(())
}

fn render_children(
    children: &[TreeNode],
    prefix: &str,
    opts: &Options,
    out: &mut dyn Write,
    stats: &mut TreeStats,
) -> io::Result<()> {
    let last = children.len().saturating_sub(1);
    for (i, child) in children.iter().enumerate() {
        let is_last = i == last;
        let connector = if is_last { "└── " } else { "├── " };
        let child_prefix = format!("{}{}", prefix, if is_last { "    " } else { "│   " });

        let display = format_entry(child, opts);

        match child.entry_type.as_str() {
            "directory" => {
                writeln!(out, "{}{}{}/", prefix, connector, display)?;
                stats.directories += 1;
                render_children(&child.children, &child_prefix, opts, out, &mut *stats)?;
            }
            "symlink" => {
                writeln!(out, "{}{}{}", prefix, connector, display)?;
                stats.files += 1;
            }
            _ => {
                writeln!(out, "{}{}{}", prefix, connector, display)?;
                stats.files += 1;
            }
        }
    }
    Ok(())
}

fn format_entry(node: &TreeNode, opts: &Options) -> String {
    let icon = if opts.show_icons {
        file_icon(&node.name, &node.entry_type, false)
    } else {
        ""
    };

    let display_name = if node.entry_type == "symlink" {
        if let Some(ref target) = node.target {
            let bare = node.name.trim_end_matches(&format!(" -> {}", target));
            let coloured = colorize(bare, "symlink", opts.color);
            format!("{} -> {}", coloured, target)
        } else {
            colorize(&node.name, "symlink", opts.color)
        }
    } else {
        colorize(&node.name, &node.entry_type, opts.color)
    };

    let suffix = if opts.human_readable {
        if let Some(sz) = node.size {
            format!(" [{}]", format_size_human(sz))
        } else {
            String::new()
        }
    } else if opts.show_size {
        if let Some(sz) = node.size {
            format!(" [{}]", sz)
        } else {
            String::new()
        }
    } else {
        String::new()
    };

    format!("{}{}{}", icon, display_name, suffix)
}

fn format_size_human(size: u64) -> String {
    const UNITS: &[&str] = &["B", "K", "M", "G", "T"];
    let mut s = size as f64;
    let mut idx = 0;
    while s >= 1024.0 && idx < UNITS.len() - 1 {
        s /= 1024.0;
        idx += 1;
    }
    if idx == 0 {
        format!("{}", size)
    } else if s < 10.0 {
        format!("{:.1}{}", s, UNITS[idx])
    } else {
        format!("{:.0}{}", s, UNITS[idx])
    }
}

mod colour {
    pub const RESET: &str = "\x1b[0m";
    pub const DIR: &str = "\x1b[1;34m";
    pub const EXE: &str = "\x1b[32m";
    pub const SYMLINK: &str = "\x1b[36m";
}

fn colorize(name: &str, entry_type: &str, enabled: bool) -> String {
    if !enabled {
        return name.to_string();
    }
    let code = match entry_type {
        "directory" => colour::DIR,
        "symlink" => colour::SYMLINK,
        _ => colour::EXE,
    };
    format!("{}{}{}", code, name, colour::RESET)
}

fn file_icon(name: &str, entry_type: &str, _is_exe: bool) -> &'static str {
    match entry_type {
        "directory" => "\u{1f4c1} ",
        "symlink" => "\u{1f517} ",
        _ => {
            if name.ends_with(".rs") {
                "\u{1f980} "
            } else if name.ends_with(".md") {
                "\u{1f4dd} "
            } else if name.ends_with(".toml") || name.ends_with(".json") || name.ends_with(".yml") {
                "\u{2699}\u{fe0f}  "
            } else if name.ends_with(".png")
                || name.ends_with(".jpg")
                || name.ends_with(".svg")
                || name.ends_with(".ico")
            {
                "\u{1f5bc}\u{fe0f}  "
            } else {
                "\u{1f4c4} "
            }
        }
    }
}

fn sort_entries(entries: &mut [fs::DirEntry], field: SortField) {
    match field {
        SortField::Name => {
            entries.sort_by_key(|a| a.file_name());
        }
        SortField::Size => {
            entries.sort_by(|a, b| {
                let sa = a.metadata().ok().map(|m| m.len()).unwrap_or(0);
                let sb = b.metadata().ok().map(|m| m.len()).unwrap_or(0);
                sa.cmp(&sb)
            });
        }
        SortField::Time => {
            entries.sort_by(|a, b| {
                let ta = a
                    .metadata()
                    .ok()
                    .and_then(|m| m.modified().ok())
                    .unwrap_or(std::time::UNIX_EPOCH);
                let tb = b
                    .metadata()
                    .ok()
                    .and_then(|m| m.modified().ok())
                    .unwrap_or(std::time::UNIX_EPOCH);
                ta.cmp(&tb)
            });
        }
    }
}

fn build_gitignore(root: &Path) -> Option<Gitignore> {
    let gitignore_path = root.join(".gitignore");
    if !gitignore_path.exists() || !gitignore_path.is_file() {
        return None;
    }
    let mut builder = GitignoreBuilder::new(root);
    builder.add(gitignore_path);
    builder.build().ok()
}

fn is_ignored(path: &Path, is_dir: bool, root: &Path, gi: &Gitignore) -> bool {
    let rel = path.strip_prefix(root).unwrap_or(path);
    gi.matched(rel, is_dir).is_ignore()
}

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

    struct Tmp(PathBuf);
    impl Tmp {
        fn path(&self) -> &Path {
            &self.0
        }
    }
    impl Drop for Tmp {
        fn drop(&mut self) {
            let _ = fs::remove_dir_all(&self.0);
        }
    }

    fn make_tmp() -> Tmp {
        use std::sync::atomic::{AtomicU64, Ordering};
        static COUNTER: AtomicU64 = AtomicU64::new(0);
        let n = COUNTER.fetch_add(1, Ordering::SeqCst);
        let mut p = std::env::temp_dir();
        p.push(format!("ls-tree-test-{}-{}", std::process::id(), n));
        fs::create_dir_all(&p).expect("create temp dir");
        Tmp(p)
    }

    fn render(dir: &Path, opts: &Options) -> (String, TreeStats) {
        let mut buf = Vec::new();
        let stats = generate(dir, &mut buf, opts).expect("generate should succeed");
        (String::from_utf8(buf).unwrap(), stats)
    }

    fn scaffold() -> Tmp {
        let tmp = make_tmp();
        fs::create_dir(tmp.path().join("a")).unwrap();
        fs::write(tmp.path().join("a").join("a1.txt"), b"hello").unwrap();
        fs::write(tmp.path().join("a").join("a2.txt"), b"world").unwrap();
        fs::create_dir(tmp.path().join("a").join("sub")).unwrap();
        fs::write(tmp.path().join("a").join("sub").join("deep.txt"), b"deep").unwrap();
        fs::write(tmp.path().join("b.txt"), b"root").unwrap();
        fs::write(tmp.path().join(".hidden_file"), b"").unwrap();
        fs::create_dir(tmp.path().join(".hidden_dir")).unwrap();
        fs::write(tmp.path().join(".hidden_dir").join("x.txt"), b"").unwrap();
        tmp
    }

    #[test]
    fn renders_recursive_hierarchy() {
        let tmp = scaffold();
        let (out, stats) = render(tmp.path(), &Options::default());
        assert!(out.contains("a/"));
        assert!(out.contains("a1.txt"));
        assert!(out.contains("a2.txt"));
        assert!(out.contains("sub/"));
        assert!(out.contains("deep.txt"));
        assert!(out.contains("b.txt"));
        assert!(!out.contains(".hidden_file"));
        assert!(!out.contains(".hidden_dir"));
        assert_eq!(
            stats,
            TreeStats {
                directories: 3,
                files: 4
            }
        );
    }

    #[test]
    fn shows_hidden_with_flag() {
        let tmp = scaffold();
        let opts = Options {
            show_hidden: true,
            ..Default::default()
        };
        let (out, stats) = render(tmp.path(), &opts);
        assert!(out.contains(".hidden_file"));
        assert!(out.contains(".hidden_dir/"));
        assert_eq!(
            stats,
            TreeStats {
                directories: 4,
                files: 6
            }
        );
    }

    #[test]
    fn symlink_does_not_cause_cycle() {
        let tmp = scaffold();
        #[cfg(unix)]
        std::os::unix::fs::symlink(tmp.path(), tmp.path().join("loop")).unwrap();
        #[cfg(windows)]
        std::os::windows::fs::symlink_dir(tmp.path(), tmp.path().join("loop")).unwrap();

        let (out, _) = render(tmp.path(), &Options::default());
        assert!(out.contains("loop"));
        assert!(out.contains("->"));
    }

    #[test]
    fn handles_permission_denied_gracefully() {
        let tmp = scaffold();
        let locked = tmp.path().join("locked");
        fs::create_dir(&locked).unwrap();
        fs::write(locked.join("secret.txt"), b"").unwrap();
        #[cfg(unix)]
        {
            use std::os::unix::fs::PermissionsExt;
            fs::set_permissions(&locked, fs::Permissions::from_mode(0o000)).unwrap();
        }
        if fs::read_dir(&locked).is_ok() {
            return;
        }

        let (out, _) = render(tmp.path(), &Options::default());
        assert!(out.contains("[Access Denied]"));

        #[cfg(unix)]
        {
            use std::os::unix::fs::PermissionsExt;
            fs::set_permissions(&locked, fs::Permissions::from_mode(0o700)).unwrap();
        }
    }

    #[test]
    fn root_permission_denied_is_minimal() {
        let tmp = make_tmp();
        let locked = tmp.path().join("locked");
        fs::create_dir(&locked).unwrap();
        #[cfg(unix)]
        {
            use std::os::unix::fs::PermissionsExt;
            fs::set_permissions(&locked, fs::Permissions::from_mode(0o000)).unwrap();
        }
        if fs::read_dir(&locked).is_ok() {
            return;
        }

        let mut buf = Vec::new();
        let stats = generate(&locked, &mut buf, &Options::default()).unwrap();
        let out = String::from_utf8(buf).unwrap();
        assert!(out.contains("[Access Denied]"));
        assert_eq!(
            stats,
            TreeStats {
                directories: 1,
                files: 0
            }
        );

        #[cfg(unix)]
        {
            use std::os::unix::fs::PermissionsExt;
            fs::set_permissions(&locked, fs::Permissions::from_mode(0o700)).unwrap();
        }
    }

    #[test]
    fn json_output_is_valid() {
        let tmp = scaffold();
        let opts = Options {
            json: true,
            ..Default::default()
        };
        let (out, stats) = render(tmp.path(), &opts);
        let parsed: serde_json::Value = serde_json::from_str(&out).expect("valid JSON");
        assert!(parsed.get("name").is_some());
        assert_eq!(parsed["type"], "directory");
        assert!(stats.directories >= 1);
        assert!(parsed["children"].is_array());
    }

    #[test]
    fn json_output_serializes_symlinks() {
        let tmp = make_tmp();
        fs::write(tmp.path().join("target.txt"), b"hi").unwrap();
        #[cfg(unix)]
        std::os::unix::fs::symlink("target.txt", tmp.path().join("link")).unwrap();
        #[cfg(windows)]
        std::os::windows::fs::symlink_file("target.txt", tmp.path().join("link")).unwrap();

        let opts = Options {
            json: true,
            ..Default::default()
        };
        let (out, _) = render(tmp.path(), &opts);
        let parsed: serde_json::Value = serde_json::from_str(&out).unwrap();
        let children = parsed["children"].as_array().unwrap();
        if children.len() == 2 {
            let has_symlink = children.iter().any(|c| c["type"] == "symlink");
            assert!(has_symlink, "output should contain a symlink: {}", out);
        }
    }

    #[test]
    fn size_flag_adds_size_info() {
        let tmp = scaffold();
        let opts = Options {
            show_size: true,
            ..Default::default()
        };
        let (out, _) = render(tmp.path(), &opts);
        assert!(out.contains('['));
    }

    #[test]
    fn dirs_only_shows_only_directories() {
        let tmp = scaffold();
        let opts = Options {
            dirs_only: true,
            ..Default::default()
        };
        let (out, stats) = render(tmp.path(), &opts);
        assert!(out.contains("a/"));
        assert!(out.contains("sub/"));
        assert!(!out.contains("a1.txt"));
        assert_eq!(stats.files, 0);
        assert!(stats.directories >= 2);
    }

    #[test]
    fn prune_removes_empty_dirs() {
        let tmp = make_tmp();
        fs::create_dir(tmp.path().join("full")).unwrap();
        fs::write(tmp.path().join("full").join("f.txt"), b"").unwrap();
        fs::create_dir(tmp.path().join("empty")).unwrap();

        let opts = Options {
            prune: true,
            ..Default::default()
        };
        let (out, _) = render(tmp.path(), &opts);
        assert!(out.contains("full/"));
        assert!(!out.contains("empty/"));
    }

    #[test]
    fn max_depth_limits_recursion() {
        let tmp = scaffold();
        let opts = Options {
            max_depth: Some(1),
            ..Default::default()
        };
        let (out, _) = render(tmp.path(), &opts);
        assert!(out.contains("a/"));
        assert!(out.contains("b.txt"));
        assert!(!out.contains("deep.txt"));
    }

    #[test]
    fn icons_are_rendered() {
        let tmp = scaffold();
        let opts = Options {
            show_icons: true,
            ..Default::default()
        };
        let (out, _) = render(tmp.path(), &opts);
        assert!(out.contains('\u{1f4c1}'));
    }
}