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