Skip to content

Commit

Permalink
Merge pull request #165 from m4rch3n1ng/time-formats
Browse files Browse the repository at this point in the history
add support for specifying time formats
  • Loading branch information
solidiquis authored May 15, 2023
2 parents 4a8d0b4 + 9618d53 commit e4de6ca
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 5 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ Options:
-l, --long Show extended metadata and attributes
--octal Show permissions in numeric octal format instead of symbolic
--time <TIME> Which kind of timestamp to use; modified by default [possible values: created, accessed, modified]
--time-format Which format to use for the timestamp; default by default [possible values: iso, iso-strict, short, default]
-L, --level <NUM> Maximum depth to display
-p, --pattern <PATTERN> Regular expression (or glob if '--glob' or '--iglob' is used) used to match files
--glob Enables glob based searching
Expand Down Expand Up @@ -100,6 +101,7 @@ Of all the above arguments, the following are not yet available on Windows but w
-l, --long Show extended metadata and attributes
--octal Show permissions in numeric octal format instead of symbolic
--time <TIME> Which kind of timestamp to use; modified by default [possible values: created, accessed, modified]
--time-format Which format to use for the timestamp; default by default [possible values: iso, iso-strict, short, default]
```

## Installation
Expand Down Expand Up @@ -377,6 +379,7 @@ Unix file permissions as well as metadata associated with directory entries can
-l, --long Show extended metadata and attributes
--octal Show permissions in numeric octal format instead of symbolic
--time <TIME> Which kind of timestamp to use; modified by default [possible values: created, accessed, modified]
--time-format Which format to use for the timestamp; default by default [possible values: iso, iso-strict, short, default]
```

<p align="center">
Expand Down
11 changes: 11 additions & 0 deletions src/context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@ pub struct Context {
#[arg(long, value_enum, requires = "long")]
pub time: Option<time::Stamp>,

/// Which format to use for the timestamp; default by default
#[cfg(unix)]
#[arg(long = "time-format", value_enum, requires = "long")]
pub time_format: Option<time::Format>,

/// Maximum depth to display
#[arg(short = 'L', long, value_name = "NUM")]
level: Option<usize>,
Expand Down Expand Up @@ -319,6 +324,12 @@ impl Context {
self.time.unwrap_or_default()
}

/// Which format to use for the timestamp; default by default
#[cfg(unix)]
pub fn time_format(&self) -> time::Format {
self.time_format.unwrap_or_default()
}

/// Which `FileType` to filter on; defaults to regular file.
pub fn file_type(&self) -> file::Type {
self.file_type.unwrap_or_default()
Expand Down
17 changes: 17 additions & 0 deletions src/context/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,20 @@ pub enum Stamp {
#[default]
Modified,
}

/// Different formatting options for timestamps
#[derive(Copy, Clone, Debug, ValueEnum, PartialEq, Eq, PartialOrd, Ord, Default)]
pub enum Format {
/// Timestamp formatted following the iso8601, with slight differences and the time-zone omitted
Iso,

/// Timestamp formatted following the exact iso8601 specifications
IsoStrict,

/// Timestamp only shows date without time in YYYY-MM-DD format
Short,

/// Timestamp is shown in DD MMM HH:MM format
#[default]
Default,
}
23 changes: 18 additions & 5 deletions src/render/grid/cell.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use crate::{
context::Context,
context::{time, Context},
disk_usage::file_size::FileSize,
render::theme,
styles::{self, PLACEHOLDER},
tree::node::Node,
};
use chrono::{DateTime, Local};
use std::{
ffi::OsStr,
fmt::{self, Display},
Expand Down Expand Up @@ -182,9 +183,6 @@ impl<'a> Cell<'a> {
#[cfg(unix)]
#[inline]
fn fmt_datetime(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
use crate::context::time;
use chrono::{offset::Local, DateTime};

let node = self.node;
let ctx = self.ctx;

Expand All @@ -196,7 +194,7 @@ impl<'a> Cell<'a> {

let out = datetime.map(DateTime::<Local>::from).map_or_else(
|| format!("{PLACEHOLDER:>12}"),
|dt| format!("{:>12}", dt.format("%d %h %H:%M %g")),
|dt| format!("{:>12}", self.fmt_timestamp(dt)),
);

let formatted_datetime = if let Ok(style) = styles::get_datetime_style() {
Expand All @@ -208,6 +206,21 @@ impl<'a> Cell<'a> {
write!(f, "{formatted_datetime}")
}

/// Rules on how to format timestamp
#[cfg(unix)]
#[inline]
fn fmt_timestamp(&self, dt: DateTime<Local>) -> String {
let time_format = self.ctx.time_format();
let delayed_format = match time_format {
time::Format::Default => dt.format("%d %h %H:%M %g"),
time::Format::Iso => dt.format("%Y-%m-%d %H:%M:%S"),
time::Format::IsoStrict => dt.format("%Y-%m-%dT%H:%M:%S%Z"),
time::Format::Short => dt.format("%Y-%m-%d"),
};

format!("{:>12}", delayed_format)
}

/// Rules on how to format permissions for rendering
#[cfg(unix)]
#[inline]
Expand Down

0 comments on commit e4de6ca

Please sign in to comment.