mirror of
https://github.com/bootandy/dust.git
synced 2026-06-08 11:29:05 +03:00
feat: Added the ability to filter the corresponding files based on the access time, modify time, and change time of the file for statistics
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
use chrono::{Local, TimeZone};
|
||||
use clap::ArgMatches;
|
||||
use config_file::FromConfigFile;
|
||||
use regex::Regex;
|
||||
@@ -6,8 +7,11 @@ use std::io::IsTerminal;
|
||||
use std::path::Path;
|
||||
use std::path::PathBuf;
|
||||
|
||||
use crate::dir_walker::Operater;
|
||||
use crate::display::get_number_format;
|
||||
|
||||
pub static DAY_SECEONDS: i64 = 24 * 60 * 60;
|
||||
|
||||
#[derive(Deserialize, Default)]
|
||||
#[serde(rename_all = "kebab-case")]
|
||||
#[serde(deny_unknown_fields)]
|
||||
@@ -132,6 +136,61 @@ impl Config {
|
||||
pub fn get_output_json(&self, options: &ArgMatches) -> bool {
|
||||
Some(true) == self.output_json || options.get_flag("output_json")
|
||||
}
|
||||
|
||||
pub fn get_modified_time_operator(&self, options: &ArgMatches) -> (Operater, i64) {
|
||||
get_filter_time_operator(
|
||||
options.get_one::<String>("mtime"),
|
||||
get_current_date_epoch_seconds(),
|
||||
)
|
||||
}
|
||||
|
||||
pub fn get_accessed_time_operator(&self, options: &ArgMatches) -> (Operater, i64) {
|
||||
get_filter_time_operator(
|
||||
options.get_one::<String>("atime"),
|
||||
get_current_date_epoch_seconds(),
|
||||
)
|
||||
}
|
||||
|
||||
pub fn get_created_time_operator(&self, options: &ArgMatches) -> (Operater, i64) {
|
||||
get_filter_time_operator(
|
||||
options.get_one::<String>("ctime"),
|
||||
get_current_date_epoch_seconds(),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
fn get_current_date_epoch_seconds() -> i64 {
|
||||
// calcurate current date epoch seconds
|
||||
let now = Local::now();
|
||||
let current_date = now.date_naive();
|
||||
|
||||
let current_date_time = current_date.and_hms_opt(0, 0, 0).unwrap();
|
||||
Local
|
||||
.from_local_datetime(¤t_date_time)
|
||||
.unwrap()
|
||||
.timestamp()
|
||||
}
|
||||
|
||||
fn get_filter_time_operator(
|
||||
option_value: Option<&String>,
|
||||
current_date_epoch_seconds: i64,
|
||||
) -> (Operater, i64) {
|
||||
match option_value {
|
||||
Some(val) => {
|
||||
let time = current_date_epoch_seconds
|
||||
- val
|
||||
.parse::<i64>()
|
||||
.unwrap_or_else(|_| panic!("invalid data format"))
|
||||
.abs()
|
||||
* DAY_SECEONDS;
|
||||
match val.chars().next().expect("Value should not be empty") {
|
||||
'+' => (Operater::LessThan, time - DAY_SECEONDS),
|
||||
'-' => (Operater::GreaterThan, time),
|
||||
_ => (Operater::Equal, time - DAY_SECEONDS),
|
||||
}
|
||||
}
|
||||
None => (Operater::GreaterThan, 0),
|
||||
}
|
||||
}
|
||||
|
||||
fn convert_min_size(input: &str) -> Option<usize> {
|
||||
@@ -191,8 +250,22 @@ pub fn get_config() -> Config {
|
||||
mod tests {
|
||||
#[allow(unused_imports)]
|
||||
use super::*;
|
||||
use chrono::{Datelike, Timelike};
|
||||
use clap::{value_parser, Arg, ArgMatches, Command};
|
||||
|
||||
#[test]
|
||||
fn test_get_current_date_epoch_seconds() {
|
||||
let epoch_seconds = get_current_date_epoch_seconds();
|
||||
let dt = Local.timestamp_opt(epoch_seconds, 0).unwrap();
|
||||
|
||||
assert_eq!(dt.hour(), 0);
|
||||
assert_eq!(dt.minute(), 0);
|
||||
assert_eq!(dt.second(), 0);
|
||||
assert_eq!(dt.date_naive().day(), Local::now().date_naive().day());
|
||||
assert_eq!(dt.date_naive().month(), Local::now().date_naive().month());
|
||||
assert_eq!(dt.date_naive().year(), Local::now().date_naive().year());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_conversion() {
|
||||
assert_eq!(convert_min_size("55"), Some(55));
|
||||
|
||||
Reference in New Issue
Block a user