ex-cli 1.20.1

Command line tool to find, filter, sort and list files.
Documentation
use crate::cli::file::FileKind;
use crate::config::Config;
use crate::fs::file::File;
use chrono::{DateTime, Utc};
use std::cmp::max;

const EXT_WIDTH: usize = 4; // (minimum width of ".txt")

pub struct Total {
    pub start_time: Option<DateTime<Utc>>,
    pub max_size: u64,
    pub total_size: u64,
    #[cfg(unix)]
    pub user_width: usize,
    #[cfg(unix)]
    pub group_width: usize,
    #[cfg(windows)]
    pub ver_width: usize,
    pub ext_width: usize,
    pub num_files: usize,
    pub num_dirs: usize,
}

impl Total {
    pub fn new(start_time: Option<DateTime<Utc>>) -> Self {
        Self {
            start_time,
            max_size: 0,
            total_size: 0,
            #[cfg(unix)]
            user_width: 0,
            #[cfg(unix)]
            group_width: 0,
            #[cfg(windows)]
            ver_width: 0,
            ext_width: 0,
            num_files: 0,
            num_dirs: 0,
        }
    }

    #[allow(unused_variables)]
    pub fn from_files(
        start_time: Option<DateTime<Utc>>,
        config: &Config,
        files: &Vec<File>,
    ) -> Self {
        let mut total = Self::new(start_time);
        #[cfg(unix)]
        if config.show_owner() {
            total.user_width = 1;
            total.group_width = 1;
        }
        for file in files.iter() {
            total.apply_file(file);
        }
        if total.ext_width > 0 {
            total.ext_width = max(total.ext_width, EXT_WIDTH);
        }
        total
    }

    fn apply_file(&mut self, file: &File) {
        self.max_size = max(self.max_size, file.file_size);
        self.total_size += file.file_size;
        #[cfg(unix)]
        if let Some(user) = &file.owner_user {
            self.user_width = max(self.user_width, user.len());
        }
        #[cfg(unix)]
        if let Some(group) = &file.owner_group {
            self.group_width = max(self.group_width, group.len());
        }
        #[cfg(windows)]
        if let Some(ver) = &file.file_ver {
            self.ver_width = max(self.ver_width, ver.len());
        }
        self.ext_width = max(self.ext_width, file.file_ext.len());
        if file.file_type == FileKind::Dir {
            self.num_dirs += 1;
        } else {
            self.num_files += 1;
        }
    }
}