ex_cli/fs/
total.rs

1use crate::config::{Config, FileKind};
2use crate::fs::file::File;
3use chrono::{DateTime, Utc};
4use std::cmp::max;
5
6const EXT_WIDTH: usize = 4; // (minimum width of ".txt")
7
8pub struct Total {
9    pub start_time: Option<DateTime<Utc>>,
10    pub max_size: u64,
11    pub total_size: u64,
12    #[cfg(unix)]
13    pub user_width: usize,
14    #[cfg(unix)]
15    pub group_width: usize,
16    #[cfg(windows)]
17    pub ver_width: usize,
18    pub ext_width: usize,
19    pub num_files: usize,
20    pub num_dirs: usize,
21}
22
23impl Total {
24    pub fn new(start_time: Option<DateTime<Utc>>) -> Self {
25        Self {
26            start_time,
27            max_size: 0,
28            total_size: 0,
29            #[cfg(unix)]
30            user_width: 0,
31            #[cfg(unix)]
32            group_width: 0,
33            #[cfg(windows)]
34            ver_width: 0,
35            ext_width: 0,
36            num_files: 0,
37            num_dirs: 0,
38        }
39    }
40
41    #[allow(unused_variables)]
42    pub fn from_files(
43        start_time: Option<DateTime<Utc>>,
44        config: &Config,
45        files: &Vec<File>,
46    ) -> Self {
47        let mut total = Self::new(start_time);
48        #[cfg(unix)]
49        if config.show_owner {
50            total.user_width = 1;
51            total.group_width = 1;
52        }
53        for file in files.iter() {
54            total.apply_file(file);
55        }
56        if total.ext_width > 0 {
57            total.ext_width = max(total.ext_width, EXT_WIDTH);
58        }
59        total
60    }
61
62    fn apply_file(&mut self, file: &File) {
63        self.max_size = max(self.max_size, file.file_size);
64        self.total_size += file.file_size;
65        #[cfg(unix)]
66        if let Some(user) = &file.owner_user {
67            self.user_width = max(self.user_width, user.len());
68        }
69        #[cfg(unix)]
70        if let Some(group) = &file.owner_group {
71            self.group_width = max(self.group_width, group.len());
72        }
73        #[cfg(windows)]
74        if let Some(ver) = &file.file_ver {
75            self.ver_width = max(self.ver_width, ver.len());
76        }
77        self.ext_width = max(self.ext_width, file.file_ext.len());
78        if file.file_type == FileKind::Dir {
79            self.num_dirs += 1;
80        } else {
81            self.num_files += 1;
82        }
83    }
84}