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    // Combine all entries in type order
19    let mut all_entries = Vec::new();
20    all_entries.extend(directories);
21    all_entries.extend(executables);
22    all_entries.extend(regular_files);
23
24    // Calculate column widths for alignment
25    let max_nlink_width = all_entries
26        .iter()
27        .map(|e| e.nlink.to_string().len())
28        .max()
29        .unwrap_or(0);
30
31    let max_owner_width = all_entries.iter().map(|e| e.owner.len()).max().unwrap_or(0);
32
33    let max_group_width = all_entries.iter().map(|e| e.group.len()).max().unwrap_or(0);
34
35    let max_size_width = all_entries
36        .iter()
37        .map(|e| e.format_size().len())
38        .max()
39        .unwrap_or(0);
40
41    // Format: <permissions> <nlink> <owner> <group> <size> <date> <icon> <name>
42    // TODO: Allow users to change this order in the config
43    for entry in all_entries {
44        let permissions = entry.format_permissions();
45        let nlink = entry.nlink.to_string();
46        let owner = &entry.owner;
47        let group = &entry.group;
48        let size = entry.format_size();
49        let modified = entry.format_modified();
50        let icon = entry.get_icon();
51        let filename = entry.path.file_name().unwrap().to_string_lossy();
52        let color = entry.get_color(&config.colors);
53
54        println!(
55            "{}  {:>nlink_width$}  {:<owner_width$}  {:<group_width$}  {:>size_width$}  {}  {} {}",
56            permissions,
57            nlink,
58            owner,
59            group,
60            size,
61            modified,
62            icon,
63            filename.color(color).bold(),
64            nlink_width = max_nlink_width,
65            owner_width = max_owner_width,
66            group_width = max_group_width,
67            size_width = max_size_width,
68        );
69    }
70}