Skip to content

Commit

Permalink
✨ Add -T option to list subcommand for display long time format
Browse files Browse the repository at this point in the history
  • Loading branch information
ChanTsune committed Nov 23, 2024
1 parent f286ed3 commit b26d5bb
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 42 deletions.
81 changes: 40 additions & 41 deletions cli/src/command/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ pub(crate) struct ListCommand {
help = "Display user id and group id instead of user name and group name"
)]
pub(crate) numeric_owner: bool,
#[arg(
short = 'T',
help = "When used with the -l option, display complete time information for the entry, including month, day, hour, minute, second, and year"
)]
pub(crate) long_time: bool,
#[command(flatten)]
pub(crate) password: PasswordArgs,
#[command(flatten)]
Expand All @@ -89,22 +94,15 @@ struct TableRow {
compressed_size: String,
user: String,
group: String,
created: String,
modified: String,
created: Option<Duration>,
modified: Option<Duration>,
name: String,
xattrs: Vec<ExtendedAttribute>,
acl: Vec<chunk::AceWithPlatform>,
privates: Vec<RawChunk>,
}

impl<T>
TryFrom<(
&NormalEntry<T>,
Option<&str>,
SystemTime,
Option<&SolidHeader>,
bool,
)> for TableRow
impl<T> TryFrom<(&NormalEntry<T>, Option<&str>, Option<&SolidHeader>, bool)> for TableRow
where
T: AsRef<[u8]> + Clone,
RawChunk<T>: Chunk,
Expand All @@ -113,10 +111,9 @@ where
type Error = io::Error;
#[inline]
fn try_from(
(entry, password, now, solid, numeric_owner): (
(entry, password, solid, numeric_owner): (
&NormalEntry<T>,
Option<&str>,
SystemTime,
Option<&SolidHeader>,
bool,
),
Expand Down Expand Up @@ -177,8 +174,8 @@ where
}
})
.unwrap_or_else(|| "-".into()),
created: datetime(now, metadata.created()),
modified: datetime(now, metadata.modified()),
created: metadata.created(),
modified: metadata.modified(),
name: if matches!(
header.data_kind(),
DataKind::SymbolicLink | DataKind::HardLink
Expand Down Expand Up @@ -213,6 +210,11 @@ fn list_archive(args: ListCommand) -> io::Result<()> {
show_xattr: args.show_xattr,
show_acl: args.show_acl,
show_private: args.show_private,
time_format: if args.long_time {
TimeFormat::Long
} else {
TimeFormat::Auto(SystemTime::now())
},
numeric_owner: args.numeric_owner,
};
#[cfg(not(feature = "memmap"))]
Expand All @@ -235,13 +237,20 @@ fn list_archive(args: ListCommand) -> io::Result<()> {
}
}

#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub(crate) enum TimeFormat {
Auto(SystemTime),
Long,
}

pub(crate) struct ListOptions {
pub(crate) long: bool,
pub(crate) header: bool,
pub(crate) solid: bool,
pub(crate) show_xattr: bool,
pub(crate) show_acl: bool,
pub(crate) show_private: bool,
pub(crate) time_format: TimeFormat,
pub(crate) numeric_owner: bool,
}

Expand All @@ -254,30 +263,22 @@ pub(crate) fn run_list_archive(
let globs =
GlobPatterns::new(files).map_err(|e| io::Error::new(io::ErrorKind::InvalidInput, e))?;

let now = SystemTime::now();
let mut entries = Vec::new();

run_read_entries(archive_provider, |entry| {
match entry? {
ReadEntry::Solid(solid) if args.solid => {
for entry in solid.entries(password)? {
entries.push(
(
&entry?,
password,
now,
Some(solid.header()),
args.numeric_owner,
)
.try_into()?,
(&entry?, password, Some(solid.header()), args.numeric_owner).try_into()?,
)
}
}
ReadEntry::Solid(_) => {
log::warn!("This archive contain solid mode entry. if you need to show it use --solid option.");
}
ReadEntry::Normal(item) => {
entries.push((&item, password, now, None, args.numeric_owner).try_into()?)
entries.push((&item, password, None, args.numeric_owner).try_into()?)
}
}
Ok(())
Expand All @@ -296,30 +297,22 @@ pub(crate) fn run_list_archive_mem(
let globs =
GlobPatterns::new(files).map_err(|e| io::Error::new(io::ErrorKind::InvalidInput, e))?;

let now = SystemTime::now();
let mut entries = Vec::new();

run_read_entries_mem(archive_provider, |entry| {
match entry? {
ReadEntry::Solid(solid) if args.solid => {
for entry in solid.entries(password)? {
entries.push(
(
&entry?,
password,
now,
Some(solid.header()),
args.numeric_owner,
)
.try_into()?,
(&entry?, password, Some(solid.header()), args.numeric_owner).try_into()?,
);
}
}
ReadEntry::Solid(_) => {
log::warn!("This archive contain solid mode entry. if you need to show it use --solid option.");
}
ReadEntry::Normal(item) => {
entries.push((&item, password, now, None, args.numeric_owner).try_into()?)
entries.push((&item, password, None, args.numeric_owner).try_into()?)
}
}
Ok(())
Expand Down Expand Up @@ -385,8 +378,8 @@ fn detail_list_entries(entries: impl Iterator<Item = TableRow>, options: ListOpt
content.compressed_size,
content.user,
content.group,
content.created,
content.modified,
datetime(options.time_format, content.created),
datetime(options.time_format, content.modified),
content.name,
]);
if options.show_acl {
Expand Down Expand Up @@ -452,17 +445,23 @@ fn within_six_months(now: SystemTime, x: SystemTime) -> bool {
six_months_ago <= x
}

fn datetime(now: SystemTime, d: Option<Duration>) -> String {
fn datetime(format: TimeFormat, d: Option<Duration>) -> String {
match d {
None => "-".into(),
Some(d) => {
let time = UNIX_EPOCH + d;
let datetime = DateTime::<Local>::from(time);
if within_six_months(now, time) {
datetime.format("%b %e %H:%M").to_string()
} else {
datetime.format("%b %e %Y").to_string()
match format {
TimeFormat::Auto(now) => {
if within_six_months(now, time) {
datetime.format("%b %e %H:%M")
} else {
datetime.format("%b %e %Y")
}
}
TimeFormat::Long => datetime.format("%b %e %H:%M:%S %Y"),
}
.to_string()
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion cli/src/command/stdio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::{
},
create::create_archive_file,
extract::{run_extract_archive_reader, OutputOption},
list::ListOptions,
list::{ListOptions, TimeFormat},
Command,
},
utils,
Expand All @@ -18,6 +18,7 @@ use std::{
fs,
io::{self, stdout},
path::PathBuf,
time::SystemTime,
};

#[derive(Args, Clone, Eq, PartialEq, Hash, Debug)]
Expand Down Expand Up @@ -248,6 +249,7 @@ fn run_list_archive(args: StdioCommand) -> io::Result<()> {
show_xattr: false,
show_acl: false,
show_private: false,
time_format: TimeFormat::Auto(SystemTime::now()),
numeric_owner: args.numeric_owner,
};
if let Some(path) = args.file {
Expand Down

0 comments on commit b26d5bb

Please sign in to comment.