Skip to main content

lx_cli/
file_entry.rs

1/// Core data structures (FileEntry and FileType) that represent files and its metadata.
2use crate::config::{ColorConfig, IconColorConfig, IconConfig};
3use crate::icon::FileIcon;
4use chrono::format::{Item, StrftimeItems};
5use colored::Color;
6use std::ffi::OsString;
7use std::time::SystemTime;
8
9#[derive(Debug, Clone)]
10pub struct FileEntry {
11    pub path: OsString,
12    pub is_dir: bool,
13    pub is_executable: bool,
14    pub is_hidden: bool,
15    pub mode: u32,
16    pub size: u64,
17    pub modified: SystemTime,
18    pub owner: String,
19    pub group: String,
20    pub nlink: u64,
21}
22
23impl FileEntry {
24    fn get_file_icon(&self) -> FileIcon {
25        if self.is_dir {
26            FileIcon::Directory
27        } else if self.is_executable {
28            FileIcon::Executable
29        } else {
30            FileIcon::RegularFile
31        }
32    }
33
34    pub fn get_icon_custom(&self, config: &IconConfig) -> String {
35        self.get_file_icon().as_str_custom(config)
36    }
37
38    pub fn get_color(&self, config: &ColorConfig) -> Color {
39        if self.is_hidden {
40            return config.get_hidden_color();
41        }
42
43        self.get_file_icon().get_color(config)
44    }
45
46    pub fn get_icon_color(&self, config: &IconColorConfig) -> Color {
47        if self.is_hidden {
48            return config.get_hidden_color();
49        }
50
51        self.get_file_icon().get_icon_color(config)
52    }
53
54    pub fn get_file_type(&self) -> FileType {
55        if self.is_dir {
56            FileType::Directory
57        } else if self.is_executable {
58            FileType::Executable
59        } else {
60            FileType::RegularFile
61        }
62    }
63
64    pub fn format_permissions(&self) -> String {
65        let mode = self.mode;
66
67        // File type
68        let file_type = if self.is_dir { 'd' } else { '-' };
69
70        // Owner permissions
71        let user_r = if mode & 0o400 != 0 { 'r' } else { '-' };
72        let user_w = if mode & 0o200 != 0 { 'w' } else { '-' };
73        let user_x = if mode & 0o100 != 0 { 'x' } else { '-' };
74
75        // Group permissions
76        let group_r = if mode & 0o040 != 0 { 'r' } else { '-' };
77        let group_w = if mode & 0o020 != 0 { 'w' } else { '-' };
78        let group_x = if mode & 0o010 != 0 { 'x' } else { '-' };
79
80        // Other permissions
81        let other_r = if mode & 0o004 != 0 { 'r' } else { '-' };
82        let other_w = if mode & 0o002 != 0 { 'w' } else { '-' };
83        let other_x = if mode & 0o001 != 0 { 'x' } else { '-' };
84
85        format!(
86            "{}{}{}{}{}{}{}{}{}{}",
87            file_type, user_r, user_w, user_x, group_r, group_w, group_x, other_r, other_w, other_x
88        )
89    }
90
91    pub fn format_size(&self) -> String {
92        let size = self.size;
93        if size < 1024 {
94            format!("{}B", size)
95        } else if size < 1024 * 1024 {
96            format!("{:.1}K", size as f64 / 1024.0)
97        } else if size < 1024 * 1024 * 1024 {
98            format!("{:.1}M", size as f64 / (1024.0 * 1024.0))
99        } else {
100            format!("{:.1}G", size as f64 / (1024.0 * 1024.0 * 1024.0))
101        }
102    }
103
104    pub fn format_modified_parts(&self, datetime_format: &str) -> Vec<DateTimePart> {
105        let datetime: chrono::DateTime<chrono::Local> = self.modified.into();
106
107        StrftimeItems::new(datetime_format)
108            .map(|item| {
109                let alignment = match item {
110                    Item::Numeric(_, _) => DateTimePartAlignment::Right,
111                    Item::Fixed(_) => DateTimePartAlignment::Left,
112                    _ => DateTimePartAlignment::None,
113                };
114                let value = datetime
115                    .format_with_items(std::iter::once(item))
116                    .to_string();
117
118                DateTimePart { value, alignment }
119            })
120            .collect()
121    }
122}
123
124#[derive(Clone, Copy, PartialEq, Eq)]
125pub enum DateTimePartAlignment {
126    Left,
127    Right,
128    None,
129}
130
131pub struct DateTimePart {
132    pub value: String,
133    pub alignment: DateTimePartAlignment,
134}
135
136#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
137pub enum FileType {
138    Directory,
139    Executable,
140    RegularFile,
141}