Skip to content

Commit

Permalink
Retrieve file timestamp (access time, birth time, and modification
Browse files Browse the repository at this point in the history
time) based on the arguments specified in the ftime command-line option.
  • Loading branch information
uggla committed Nov 29, 2024
1 parent 9cfeb65 commit ef1e627
Show file tree
Hide file tree
Showing 8 changed files with 171 additions and 118 deletions.
17 changes: 8 additions & 9 deletions src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,15 +116,14 @@ impl Core {
.collect();

for path in paths {
let mut meta =
match Meta::from_path(&path, self.flags.dereference.0, self.flags.permission) {
Ok(meta) => meta,
Err(err) => {
print_error!("{}: {}.", path.display(), err);
exit_code.set_if_greater(ExitCode::MajorIssue);
continue;
}
};
let mut meta = match Meta::from_path(&path, self.flags.dereference.0, &self.flags) {
Ok(meta) => meta,
Err(err) => {
print_error!("{}: {}.", path.display(), err);
exit_code.set_if_greater(ExitCode::MajorIssue);
continue;
}
};

let cache = if self.flags.blocks.0.contains(&Block::GitStatus) {
Some(GitCache::new(&path))
Expand Down
30 changes: 18 additions & 12 deletions src/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,7 @@ mod tests {
use crate::app::Cli;
use crate::color;
use crate::color::Colors;
use crate::flags::{HyperlinkOption, IconOption, IconTheme as FlagTheme, PermissionFlag};
use crate::flags::{HyperlinkOption, IconOption, IconTheme as FlagTheme};
use crate::icon::Icons;
use crate::meta::{FileType, Name};
use crate::Config;
Expand Down Expand Up @@ -699,7 +699,7 @@ mod tests {
dir.child("one.d").create_dir_all().unwrap();
dir.child("one.d/two").touch().unwrap();
dir.child("one.d/.hidden").touch().unwrap();
let mut metas = Meta::from_path(Path::new(dir.path()), false, PermissionFlag::Rwx)
let mut metas = Meta::from_path(Path::new(dir.path()), false, &flags)
.unwrap()
.recurse_into(42, &flags, None)
.unwrap()
Expand Down Expand Up @@ -732,7 +732,7 @@ mod tests {
let dir = assert_fs::TempDir::new().unwrap();
dir.child("dir").create_dir_all().unwrap();
dir.child("dir/file").touch().unwrap();
let metas = Meta::from_path(Path::new(dir.path()), false, PermissionFlag::Rwx)
let metas = Meta::from_path(Path::new(dir.path()), false, &flags)
.unwrap()
.recurse_into(42, &flags, None)
.unwrap()
Expand Down Expand Up @@ -773,7 +773,7 @@ mod tests {
let dir = assert_fs::TempDir::new().unwrap();
dir.child("dir").create_dir_all().unwrap();
dir.child("dir/file").touch().unwrap();
let metas = Meta::from_path(Path::new(dir.path()), false, PermissionFlag::Rwx)
let metas = Meta::from_path(Path::new(dir.path()), false, &flags)
.unwrap()
.recurse_into(42, &flags, None)
.unwrap()
Expand Down Expand Up @@ -813,7 +813,7 @@ mod tests {
let dir = assert_fs::TempDir::new().unwrap();
dir.child("one.d").create_dir_all().unwrap();
dir.child("one.d/two").touch().unwrap();
let metas = Meta::from_path(Path::new(dir.path()), false, PermissionFlag::Rwx)
let metas = Meta::from_path(Path::new(dir.path()), false, &flags)
.unwrap()
.recurse_into(42, &flags, None)
.unwrap()
Expand Down Expand Up @@ -844,7 +844,7 @@ mod tests {
let dir = assert_fs::TempDir::new().unwrap();
dir.child("testdir").create_dir_all().unwrap();
dir.child("test").touch().unwrap();
let metas = Meta::from_path(Path::new(dir.path()), false, PermissionFlag::Rwx)
let metas = Meta::from_path(Path::new(dir.path()), false, &flags)
.unwrap()
.recurse_into(1, &flags, None)
.unwrap()
Expand Down Expand Up @@ -878,7 +878,7 @@ mod tests {

let dir = assert_fs::TempDir::new().unwrap();
dir.child("testdir").create_dir_all().unwrap();
let metas = Meta::from_path(Path::new(dir.path()), false, PermissionFlag::Rwx)
let metas = Meta::from_path(Path::new(dir.path()), false, &flags)
.unwrap()
.recurse_into(1, &flags, None)
.unwrap()
Expand Down Expand Up @@ -908,11 +908,14 @@ mod tests {

let file_path = tmp_dir.path().join("file");
std::fs::File::create(&file_path).expect("failed to create the file");
let file = Meta::from_path(&file_path, false, PermissionFlag::Rwx).unwrap();

let flags = Flags::default();

let file = Meta::from_path(&file_path, false, &flags).unwrap();

let dir_path = tmp_dir.path().join("dir");
std::fs::create_dir(&dir_path).expect("failed to create the dir");
let dir = Meta::from_path(&dir_path, false, PermissionFlag::Rwx).unwrap();
let dir = Meta::from_path(&dir_path, false, &flags).unwrap();

assert_eq!(
display_folder_path(&dir),
Expand Down Expand Up @@ -955,15 +958,18 @@ mod tests {

let file_path = tmp_dir.path().join("file");
std::fs::File::create(&file_path).expect("failed to create the file");
let file = Meta::from_path(&file_path, false, PermissionFlag::Rwx).unwrap();

let flags = Flags::default();

let file = Meta::from_path(&file_path, false, &flags).unwrap();

let dir_path = tmp_dir.path().join("dir");
std::fs::create_dir(&dir_path).expect("failed to create the dir");
let dir = Meta::from_path(&dir_path, false, PermissionFlag::Rwx).unwrap();
let dir = Meta::from_path(&dir_path, false, &flags).unwrap();

let link_path = tmp_dir.path().join("link");
std::os::unix::fs::symlink("dir", &link_path).unwrap();
let link = Meta::from_path(&link_path, false, PermissionFlag::Rwx).unwrap();
let link = Meta::from_path(&link_path, false, &flags).unwrap();

const YES: bool = true;
const NO: bool = false;
Expand Down
35 changes: 23 additions & 12 deletions src/icon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ impl Icons {
#[cfg(test)]
mod test {
use super::{IconTheme, Icons};
use crate::flags::{IconOption, IconTheme as FlagTheme, PermissionFlag};
use crate::flags::{Flags, IconOption, IconTheme as FlagTheme};
use crate::meta::Meta;
use std::fs::File;
use tempfile::tempdir;
Expand All @@ -85,7 +85,8 @@ mod test {
let tmp_dir = tempdir().expect("failed to create temp dir");
let file_path = tmp_dir.path().join("file.txt");
File::create(&file_path).expect("failed to create file");
let meta = Meta::from_path(&file_path, false, PermissionFlag::Rwx).unwrap();
let flags = Flags::default();
let meta = Meta::from_path(&file_path, false, &flags).unwrap();

let icons = Icons::new(true, IconOption::Never, FlagTheme::Fancy, " ".to_string());
let icon = icons.get(&meta.name);
Expand All @@ -97,7 +98,8 @@ mod test {
let tmp_dir = tempdir().expect("failed to create temp dir");
let file_path = tmp_dir.path().join("file.txt");
File::create(&file_path).expect("failed to create file");
let meta = Meta::from_path(&file_path, false, PermissionFlag::Rwx).unwrap();
let flags = Flags::default();
let meta = Meta::from_path(&file_path, false, &flags).unwrap();

let icons = Icons::new(false, IconOption::Never, FlagTheme::Fancy, " ".to_string());
let icon = icons.get(&meta.name);
Expand All @@ -110,7 +112,8 @@ mod test {
let tmp_dir = tempdir().expect("failed to create temp dir");
let file_path = tmp_dir.path().join("file.txt");
File::create(&file_path).expect("failed to create file");
let meta = Meta::from_path(&file_path, false, PermissionFlag::Rwx).unwrap();
let flags = Flags::default();
let meta = Meta::from_path(&file_path, false, &flags).unwrap();

let icons = Icons::new(false, IconOption::Auto, FlagTheme::Fancy, " ".to_string());
let icon = icons.get(&meta.name);
Expand All @@ -122,7 +125,8 @@ mod test {
let tmp_dir = tempdir().expect("failed to create temp dir");
let file_path = tmp_dir.path().join("file.txt");
File::create(&file_path).expect("failed to create file");
let meta = Meta::from_path(&file_path, false, PermissionFlag::Rwx).unwrap();
let flags = Flags::default();
let meta = Meta::from_path(&file_path, false, &flags).unwrap();

let icons = Icons::new(true, IconOption::Auto, FlagTheme::Fancy, " ".to_string());
let icon = icons.get(&meta.name);
Expand All @@ -135,7 +139,8 @@ mod test {
let tmp_dir = tempdir().expect("failed to create temp dir");
let file_path = tmp_dir.path().join("file");
File::create(&file_path).expect("failed to create file");
let meta = Meta::from_path(&file_path, false, PermissionFlag::Rwx).unwrap();
let flags = Flags::default();
let meta = Meta::from_path(&file_path, false, &flags).unwrap();

let icon = Icons::new(true, IconOption::Always, FlagTheme::Fancy, " ".to_string());
let icon_str = icon.get(&meta.name);
Expand All @@ -148,7 +153,8 @@ mod test {
let tmp_dir = tempdir().expect("failed to create temp dir");
let file_path = tmp_dir.path().join("file");
File::create(&file_path).expect("failed to create file");
let meta = Meta::from_path(&file_path, false, PermissionFlag::Rwx).unwrap();
let flags = Flags::default();
let meta = Meta::from_path(&file_path, false, &flags).unwrap();

let icon = Icons::new(false, IconOption::Always, FlagTheme::Fancy, " ".to_string());
let icon_str = icon.get(&meta.name);
Expand All @@ -161,7 +167,8 @@ mod test {
let tmp_dir = tempdir().expect("failed to create temp dir");
let file_path = tmp_dir.path().join("file");
File::create(&file_path).expect("failed to create file");
let meta = Meta::from_path(&file_path, false, PermissionFlag::Rwx).unwrap();
let flags = Flags::default();
let meta = Meta::from_path(&file_path, false, &flags).unwrap();

let icon = Icons::new(
false,
Expand All @@ -178,7 +185,8 @@ mod test {
fn get_icon_default_directory() {
let tmp_dir = tempdir().expect("failed to create temp dir");
let file_path = tmp_dir.path();
let meta = Meta::from_path(file_path, false, PermissionFlag::Rwx).unwrap();
let flags = Flags::default();
let meta = Meta::from_path(file_path, false, &flags).unwrap();

let icon = Icons::new(false, IconOption::Always, FlagTheme::Fancy, " ".to_string());
let icon_str = icon.get(&meta.name);
Expand All @@ -190,7 +198,8 @@ mod test {
fn get_icon_default_directory_unicode() {
let tmp_dir = tempdir().expect("failed to create temp dir");
let file_path = tmp_dir.path();
let meta = Meta::from_path(file_path, false, PermissionFlag::Rwx).unwrap();
let flags = Flags::default();
let meta = Meta::from_path(file_path, false, &flags).unwrap();

let icon = Icons::new(
false,
Expand All @@ -210,7 +219,8 @@ mod test {
for (file_name, file_icon) in &IconTheme::get_default_icons_by_name() {
let file_path = tmp_dir.path().join(file_name);
File::create(&file_path).expect("failed to create file");
let meta = Meta::from_path(&file_path, false, PermissionFlag::Rwx).unwrap();
let flags = Flags::default();
let meta = Meta::from_path(&file_path, false, &flags).unwrap();

let icon = Icons::new(false, IconOption::Always, FlagTheme::Fancy, " ".to_string());
let icon_str = icon.get(&meta.name);
Expand All @@ -226,7 +236,8 @@ mod test {
for (ext, file_icon) in &IconTheme::get_default_icons_by_extension() {
let file_path = tmp_dir.path().join(format!("file.{ext}"));
File::create(&file_path).expect("failed to create file");
let meta = Meta::from_path(&file_path, false, PermissionFlag::Rwx).unwrap();
let flags = Flags::default();
let meta = Meta::from_path(&file_path, false, &flags).unwrap();

let icon = Icons::new(false, IconOption::Always, FlagTheme::Fancy, " ".to_string());
let icon_str = icon.get(&meta.name);
Expand Down
42 changes: 25 additions & 17 deletions src/meta/date.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::locale::current_locale;
use crate::color::{ColoredString, Colors, Elem};
use crate::flags::{DateFlag, Flags};
use crate::flags::{DateFlag, FileTimeFlag, Flags};
use chrono::{DateTime, Duration, Local};
use chrono_humanize::HumanTime;
use std::fs::Metadata;
Expand All @@ -23,15 +23,23 @@ impl From<SystemTime> for Date {
}
}

impl From<&Metadata> for Date {
fn from(meta: &Metadata) -> Self {
meta.accessed()
.expect("failed to retrieve modified date")
.into()
}
}

impl Date {
pub fn from_metadata(meta: &Metadata, flags: &Flags) -> Self {
match flags.ftime {
FileTimeFlag::Birth => meta
.created()
.expect("failed to retrieve creation date")
.into(),
FileTimeFlag::Access => meta
.accessed()
.expect("failed to retrieve access date")
.into(),
FileTimeFlag::Modification => meta
.modified()
.expect("failed to retrieve modification date")
.into(),
}
}
pub fn render(&self, colors: &Colors, flags: &Flags) -> ColoredString {
let now = Local::now();
#[allow(deprecated)]
Expand Down Expand Up @@ -130,7 +138,7 @@ mod test {
assert!(success, "failed to exec touch");

let colors = Colors::new(ThemeOption::Default);
let date = Date::from(&file_path.metadata().unwrap());
let date = Date::from_metadata(&file_path.metadata().unwrap(), &Flags::default());
let flags = Flags::default();

assert_eq!(
Expand Down Expand Up @@ -158,7 +166,7 @@ mod test {
assert!(success, "failed to exec touch");

let colors = Colors::new(ThemeOption::Default);
let date = Date::from(&file_path.metadata().unwrap());
let date = Date::from_metadata(&file_path.metadata().unwrap(), &Flags::default());
let flags = Flags::default();

assert_eq!(
Expand Down Expand Up @@ -186,7 +194,7 @@ mod test {
assert!(success, "failed to exec touch");

let colors = Colors::new(ThemeOption::Default);
let date = Date::from(&file_path.metadata().unwrap());
let date = Date::from_metadata(&file_path.metadata().unwrap(), &Flags::default());
let flags = Flags::default();

assert_eq!(
Expand Down Expand Up @@ -214,7 +222,7 @@ mod test {
assert!(success, "failed to exec touch");

let colors = Colors::new(ThemeOption::Default);
let date = Date::from(&file_path.metadata().unwrap());
let date = Date::from_metadata(&file_path.metadata().unwrap(), &Flags::default());

let flags = Flags {
date: DateFlag::Relative,
Expand All @@ -241,7 +249,7 @@ mod test {
assert!(success, "failed to exec touch");

let colors = Colors::new(ThemeOption::Default);
let date = Date::from(&file_path.metadata().unwrap());
let date = Date::from_metadata(&file_path.metadata().unwrap(), &Flags::default());

let flags = Flags {
date: DateFlag::Relative,
Expand All @@ -268,7 +276,7 @@ mod test {
assert!(success, "failed to exec touch");

let colors = Colors::new(ThemeOption::Default);
let date = Date::from(&file_path.metadata().unwrap());
let date = Date::from_metadata(&file_path.metadata().unwrap(), &Flags::default());

let flags = Flags {
date: DateFlag::Iso,
Expand Down Expand Up @@ -299,7 +307,7 @@ mod test {
assert!(success, "failed to exec touch");

let colors = Colors::new(ThemeOption::Default);
let date = Date::from(&file_path.metadata().unwrap());
let date = Date::from_metadata(&file_path.metadata().unwrap(), &Flags::default());

let flags = Flags {
date: DateFlag::Iso,
Expand Down Expand Up @@ -329,7 +337,7 @@ mod test {
assert!(success, "failed to exec touch");

let colors = Colors::new(ThemeOption::Default);
let date = Date::from(&file_path.metadata().unwrap());
let date = Date::from_metadata(&file_path.metadata().unwrap(), &Flags::default());

let flags = Flags {
date: DateFlag::Locale,
Expand Down
6 changes: 3 additions & 3 deletions src/meta/filetype.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,7 @@ impl FileType {
mod test {
use super::FileType;
use crate::color::{Colors, ThemeOption};
#[cfg(unix)]
use crate::flags::PermissionFlag;
use crate::flags::Flags;
#[cfg(unix)]
use crate::meta::permissions_or_attributes::PermissionsOrAttributes;
#[cfg(unix)]
Expand Down Expand Up @@ -148,7 +147,8 @@ mod test {
fn test_dir_type() {
let tmp_dir = tempdir().expect("failed to create temp dir");
#[cfg(not(windows))]
let meta = crate::meta::Meta::from_path(tmp_dir.path(), false, PermissionFlag::Rwx)
let flags = Flags::default();
let meta = crate::meta::Meta::from_path(tmp_dir.path(), false, &flags)
.expect("failed to get tempdir path");
let metadata = tmp_dir.path().metadata().expect("failed to get metas");

Expand Down
Loading

0 comments on commit ef1e627

Please sign in to comment.