From ba98808eb17a3ee06f48480a6e9bce1121419810 Mon Sep 17 00:00:00 2001 From: fdai7375 Date: Thu, 2 Feb 2023 18:30:56 +0100 Subject: [PATCH] feat: add ls command accessed timestamp --- src/builtins/ls.rs | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/src/builtins/ls.rs b/src/builtins/ls.rs index d621b81..1b6a891 100644 --- a/src/builtins/ls.rs +++ b/src/builtins/ls.rs @@ -19,7 +19,7 @@ impl Builtin for Ls { //for entry in entries.by_ref().into_iter() {} println!( - "{} | dir | size | modified |", + "{} | dir | size | modified | accessed |", right_padding(" filename", 20) ); @@ -42,6 +42,11 @@ impl Builtin for Ls { Err(_) => Local::now(), }; + let accessed: DateTime = match metadata.accessed() { + Ok(t) => DateTime::from(t), + Err(_) => Local::now(), + }; + let mut file_type = "unknown"; if metadata.file_type().is_dir() { file_type = "dir" @@ -52,7 +57,14 @@ impl Builtin for Ls { } println!( "{}", - format_line(20, &file_name, file_type, metadata.len(), modified) + format_line( + 20, + &file_name, + file_type, + metadata.len(), + modified, + accessed + ) ); } Ok(()) @@ -86,13 +98,15 @@ fn format_line( file_type: &str, file_size: u64, modified: DateTime, + accessed: DateTime, ) -> String { format!( - "{} | {:4} | {:6} | {} |", + "{} | {:4} | {:6} | {} | {} |", right_padding(file_name, max_name_len), file_type, format_filesize(file_size), - right_padding(&format_date(modified), 10) + right_padding(&format_date(modified), 10), + right_padding(&format_date(accessed), 10) ) }