1use crate::config::Config;
2use crate::file_entry::{FileEntry, FileType};
3use colored::Colorize;
4
5pub fn format_long(entries: Vec<FileEntry>, config: &Config) {
6 let mut directories: Vec<FileEntry> = Vec::new();
7 let mut executables: Vec<FileEntry> = Vec::new();
8 let mut regular_files: Vec<FileEntry> = Vec::new();
9
10 for entry in entries {
11 match entry.get_file_type() {
12 FileType::Directory => directories.push(entry),
13 FileType::Executable => executables.push(entry),
14 FileType::RegularFile => regular_files.push(entry),
15 }
16 }
17
18 directories.sort_by(|a, b| {
20 let a_name = a.path.to_string_lossy();
21 let b_name = b.path.to_string_lossy();
22 a_name.cmp(&b_name)
23 });
24 executables.sort_by(|a, b| {
25 let a_name = a.path.to_string_lossy();
26 let b_name = b.path.to_string_lossy();
27 a_name.cmp(&b_name)
28 });
29 regular_files.sort_by(|a, b| {
30 let a_name = a.path.to_string_lossy();
31 let b_name = b.path.to_string_lossy();
32 a_name.cmp(&b_name)
33 });
34
35 let mut all_entries = Vec::new();
37 all_entries.extend(directories);
38 all_entries.extend(executables);
39 all_entries.extend(regular_files);
40
41 let max_nlink_width = all_entries
43 .iter()
44 .map(|e| e.nlink.to_string().len())
45 .max()
46 .unwrap_or(0);
47
48 let max_owner_width = all_entries.iter().map(|e| e.owner.len()).max().unwrap_or(0);
49
50 let max_group_width = all_entries.iter().map(|e| e.group.len()).max().unwrap_or(0);
51
52 let max_size_width = all_entries
53 .iter()
54 .map(|e| e.format_size().len())
55 .max()
56 .unwrap_or(0);
57
58 for entry in all_entries {
61 let permissions = entry.format_permissions();
62 let nlink = entry.nlink.to_string();
63 let owner = &entry.owner;
64 let group = &entry.group;
65 let size = entry.format_size();
66 let modified = entry.format_modified();
67 let icon = entry.get_icon_custom(&config.icons);
68 let filename = entry.path.to_string_lossy();
69 let color = entry.get_color(&config.colors);
70 let icon_color = entry.get_icon_color(&config.icons.colors);
71
72 println!(
73 "{} {:>nlink_width$} {:<owner_width$} {:<group_width$} {:>size_width$} {} {} {}",
74 permissions,
75 nlink,
76 owner,
77 group,
78 size,
79 modified,
80 icon.color(icon_color),
81 filename.color(color).bold(),
82 nlink_width = max_nlink_width,
83 owner_width = max_owner_width,
84 group_width = max_group_width,
85 size_width = max_size_width,
86 );
87 }
88}