lx_cli/formatter/
recursive.rs1use crate::config::Config;
2use crate::directory_size::DirectorySizeCache;
3use crate::file_entry::FileEntry;
4use crate::filter::EntryFilter;
5use crate::formatter::long::print_long_entries;
6use crate::formatter::print_line;
7use crate::formatter::tree::{TreeEntry, TreeRenderer};
8use crate::formatter::{format_long, format_one_per_line, format_short_entry};
9use crate::git::GitRepository;
10use crate::reader::{MetadataMode, read_entry, read_target};
11use crate::sort::{DefaultSort, SortOptions};
12use std::io::{self, Write};
13use std::path::Path;
14
15pub struct TreeOptions<'a> {
16 pub show_hidden: bool,
17 pub use_long_format: bool,
18 pub total_size: bool,
19 pub depth: Option<usize>,
20 pub ignored: Option<&'a GitRepository>,
21}
22
23pub fn format_recursive(
24 out: &mut dyn Write,
25 path: &Path,
26 config: &Config,
27 options: &TreeOptions<'_>,
28 filter: &EntryFilter,
29 sort: SortOptions,
30 git: Option<&GitRepository>,
31) -> io::Result<()> {
32 let metadata_mode = if options.use_long_format || sort.requires_full_metadata() {
33 MetadataMode::Full
34 } else {
35 MetadataMode::Basic
36 };
37 if !path.is_dir() || path.is_symlink() {
38 let mut entries = filter.apply(read_target(path, options.show_hidden, metadata_mode)?);
39 if let Some(repository) = git {
40 repository.annotate(path, &mut entries)?;
41 }
42
43 if options.use_long_format {
44 format_long(out, entries, config, sort)?;
45 } else {
46 format_one_per_line(out, entries, config, sort)?;
47 }
48 return Ok(());
49 }
50
51 let mut root = read_entry(path, metadata_mode)?;
52 if let Some(repository) = git {
53 root.git_status = Some(repository.status(&path.canonicalize()?));
54 }
55 let mut directory_sizes = (options.total_size
58 && config.display.uses_sizes(options.use_long_format, sort))
59 .then(DirectorySizeCache::default);
60 if git.is_some() || options.ignored.is_some() {
61 root.full_path = path.canonicalize()?;
62 }
63 if let Some(sizes) = &mut directory_sizes {
64 sizes.apply(std::slice::from_mut(&mut root));
65 }
66
67 let uses_icons = !options.use_long_format && config.icons.has_icons();
68 let default_sort = if options.use_long_format {
69 DefaultSort::TypeThenName
70 } else {
71 DefaultSort::Name
72 };
73 let renderer = TreeRenderer::new(
74 &config.display.tree.style,
75 uses_icons,
76 filter,
77 sort,
78 default_sort,
79 git,
80 options,
81 );
82 let tree_entries =
83 renderer.collect(&root.full_path, metadata_mode, directory_sizes.as_mut())?;
84
85 if options.use_long_format {
86 print_nested_long_tree(out, &root, &tree_entries, config)?;
87 } else {
88 print_tree_root(out, &root, config)?;
89 print_short_tree(out, &tree_entries, config)?;
90 }
91
92 Ok(())
93}
94
95fn print_nested_long_tree(
96 out: &mut dyn Write,
97 root: &FileEntry,
98 tree_entries: &[TreeEntry],
99 config: &Config,
100) -> io::Result<()> {
101 let mut display_entries = Vec::with_capacity(tree_entries.len() + 1);
102 let mut filename_prefixes = vec![String::new()];
103 display_entries.push(display_entry(root));
104 display_entries.extend(
105 tree_entries
106 .iter()
107 .map(|tree_entry| display_entry(&tree_entry.entry)),
108 );
109 filename_prefixes.extend(
110 tree_entries
111 .iter()
112 .map(|tree_entry| format!("{} ", tree_entry.branch)),
113 );
114
115 print_long_entries(out, &display_entries, Some(&filename_prefixes), config)
116}
117
118fn print_tree_root(out: &mut dyn Write, root: &FileEntry, config: &Config) -> io::Result<()> {
119 print_line(
120 out,
121 format_args!("{}", format_short_entry(&display_entry(root), config)),
122 )
123}
124
125fn print_short_tree(
126 out: &mut dyn Write,
127 tree_entries: &[TreeEntry],
128 config: &Config,
129) -> io::Result<()> {
130 for tree_entry in tree_entries {
131 print_line(
132 out,
133 format_args!(
134 "{} {}",
135 config.theme.style("tree").paint(&tree_entry.branch),
136 format_short_entry(&display_entry(&tree_entry.entry), config)
137 ),
138 )?;
139 }
140 Ok(())
141}
142
143fn display_entry(entry: &FileEntry) -> FileEntry {
144 let mut display_entry = entry.clone();
145 if display_entry.is_dir {
146 display_entry.path.push("/");
147 }
148 display_entry
149}