lx_cli/formatter/
long.rs

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    // Sort each file type alphabetically by filename
19    directories.sort_by(|a, b| {
20        let a_name = a.path.file_name().unwrap().to_string_lossy();
21        let b_name = b.path.file_name().unwrap().to_string_lossy();
22        a_name.cmp(&b_name)
23    });
24    executables.sort_by(|a, b| {
25        let a_name = a.path.file_name().unwrap().to_string_lossy();
26        let b_name = b.path.file_name().unwrap().to_string_lossy();
27        a_name.cmp(&b_name)
28    });
29    regular_files.sort_by(|a, b| {
30        let a_name = a.path.file_name().unwrap().to_string_lossy();
31        let b_name = b.path.file_name().unwrap().to_string_lossy();
32        a_name.cmp(&b_name)
33    });
34
35    // Combine all entries in type order
36    let mut all_entries = Vec::new();
37    all_entries.extend(directories);
38    all_entries.extend(executables);
39    all_entries.extend(regular_files);
40
41    // Calculate column widths for alignment
42    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    // Format: <permissions> <nlink> <owner> <group> <size> <date> <icon> <name>
59    // TODO: Allow users to change this order in the config
60    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();
68        let filename = entry.path.file_name().unwrap().to_string_lossy();
69        let color = entry.get_color(&config.colors);
70
71        println!(
72            "{}  {:>nlink_width$}  {:<owner_width$}  {:<group_width$}  {:>size_width$}  {}  {} {}",
73            permissions,
74            nlink,
75            owner,
76            group,
77            size,
78            modified,
79            icon,
80            filename.color(color).bold(),
81            nlink_width = max_nlink_width,
82            owner_width = max_owner_width,
83            group_width = max_group_width,
84            size_width = max_size_width,
85        );
86    }
87}