-
Notifications
You must be signed in to change notification settings - Fork 209
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Added the ability to filter the corresponding files based on the access_time, modify time, and change time of the file for statistics. #399
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. typo! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry, due to my carelessness, I did not correct this spelling error in time. I will resubmit a PR for processing. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not a problem! we all do it. |
||
|
||
#[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)); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure if we need all these - I like the idea of a including a filter that works with modified time. But 'access time' - would someone actually use that? I have to launch 'stat' to even see it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah we can leave it in if you'd like.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In my opinion, access time is an effective filtering tool when users want to determine whether a file has been accessed recently based on the access time to determine whether other people are using the file.