Skip to content
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: Add a time style with better human readability #1202

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.toml
Expand Up @@ -19,6 +19,8 @@ name = "exa"

[dependencies]
ansi_term = "0.12"
chrono = "0.4.26"
chrono-humanize = "0.2.2"
glob = "0.3"
lazy_static = "1.3"
libc = "0.2"
Expand Down
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -89,7 +89,7 @@ Some of the options accept parameters:
- Valid **--color** options are **always**, **automatic**, and **never**.
- Valid sort fields are **accessed**, **changed**, **created**, **extension**, **Extension**, **inode**, **modified**, **name**, **Name**, **size**, **type**, and **none**. Fields starting with a capital letter sort uppercase before lowercase. The modified field has the aliases **date**, **time**, and **newest**, while its reverse has the aliases **age** and **oldest**.
- Valid time fields are **modified**, **changed**, **accessed**, and **created**.
- Valid time styles are **default**, **iso**, **long-iso**, and **full-iso**.
- Valid time styles are **default**, **iso**, **long-iso**, **full-iso** and **relative**.


---
Expand Down
2 changes: 1 addition & 1 deletion completions/bash/exa
Expand Up @@ -29,7 +29,7 @@ _exa()
;;

--time-style)
COMPREPLY=( $( compgen -W 'default iso long-iso full-iso --' -- "$cur" ) )
COMPREPLY=( $( compgen -W 'default iso long-iso full-iso relative --' -- "$cur" ) )
return
;;
esac
Expand Down
1 change: 1 addition & 0 deletions completions/fish/exa.fish
Expand Up @@ -79,6 +79,7 @@ complete -c exa -l 'time-style' -d "How to format timestamps" -x -a "
iso\t'Display brief ISO timestamps'
long-iso\t'Display longer ISO timestaps, up to the minute'
full-iso\t'Display full ISO timestamps, up to the nanosecond'
relative\t'Display more readable date forms for humans'
"
complete -c exa -l 'no-permissions' -d "Suppress the permissions field"
complete -c exa -l 'octal-permissions' -d "List each file's permission in octal format"
Expand Down
2 changes: 1 addition & 1 deletion completions/zsh/_exa
Expand Up @@ -43,7 +43,7 @@ __exa() {
{-n,--numeric}"[List numeric user and group IDs.]" \
{-S,--blocks}"[List each file's number of filesystem blocks]" \
{-t,--time}="[Which time field to show]:(time field):(accessed changed created modified)" \
--time-style="[How to format timestamps]:(time style):(default iso long-iso full-iso)" \
--time-style="[How to format timestamps]:(time style):(default iso long-iso full-iso relative)" \
--no-permissions"[Suppress the permissions field]" \
--octal-permissions"[List each file's permission in octal format]" \
--no-filesize"[Suppress the filesize field]" \
Expand Down
2 changes: 1 addition & 1 deletion man/exa.1.md
Expand Up @@ -157,7 +157,7 @@ These options are available when running with `--long` (`-l`):
`--time-style=STYLE`
: How to format timestamps.

: Valid timestamp styles are ‘`default`’, ‘`iso`’, ‘`long-iso`’, and ‘`full-iso`’.
: Valid timestamp styles are ‘`default`’, ‘`iso`’, ‘`long-iso`’, ‘`full-iso`’, and ‘`relative`’.

`-u`, `--accessed`
: Use the accessed timestamp field.
Expand Down
2 changes: 1 addition & 1 deletion src/options/flags.rs
Expand Up @@ -52,7 +52,7 @@ pub static ACCESSED: Arg = Arg { short: Some(b'u'), long: "accessed", takes_
pub static CREATED: Arg = Arg { short: Some(b'U'), long: "created", takes_value: TakesValue::Forbidden };
pub static TIME_STYLE: Arg = Arg { short: None, long: "time-style", takes_value: TakesValue::Necessary(Some(TIME_STYLES)) };
const TIMES: Values = &["modified", "changed", "accessed", "created"];
const TIME_STYLES: Values = &["default", "long-iso", "full-iso", "iso"];
const TIME_STYLES: Values = &["default", "long-iso", "full-iso", "iso", "relative"];

// suppressing columns
pub static NO_PERMISSIONS: Arg = Arg { short: None, long: "no-permissions", takes_value: TakesValue::Forbidden };
Expand Down
2 changes: 1 addition & 1 deletion src/options/help.rs
Expand Up @@ -54,7 +54,7 @@ LONG VIEW OPTIONS
-u, --accessed use the accessed timestamp field
-U, --created use the created timestamp field
--changed use the changed timestamp field
--time-style how to format timestamps (default, iso, long-iso, full-iso)
--time-style how to format timestamps (default, iso, long-iso, full-iso, relative)
--no-permissions suppress the permissions field
--octal-permissions list each file's permission in octal format
--no-filesize suppress the filesize field
Expand Down
3 changes: 3 additions & 0 deletions src/options/view.rs
Expand Up @@ -266,6 +266,9 @@ impl TimeFormat {
else if &word == "full-iso" {
Ok(Self::FullISO)
}
else if &word == "relative" {
Ok(Self::Human)
}
else {
Err(OptionsError::BadArgument(&flags::TIME_STYLE, word))
}
Expand Down
22 changes: 21 additions & 1 deletion src/output/time.rs
Expand Up @@ -2,6 +2,7 @@

use std::time::{SystemTime, UNIX_EPOCH};

use chrono_humanize::HumanTime;
use datetime::{LocalDateTime, TimeZone, DatePiece, TimePiece};
use datetime::fmt::DateFormat;

Expand All @@ -24,7 +25,7 @@ use unicode_width::UnicodeWidthStr;
/// prints month names as numbers.
///
/// Currently exa does not support *custom* styles, where the user enters a
/// format string in an environment variable or something. Just these four.
/// format string in an environment variable or something. Just these five.
#[derive(PartialEq, Eq, Debug, Copy, Clone)]
pub enum TimeFormat {

Expand All @@ -46,6 +47,10 @@ pub enum TimeFormat {
/// millisecond and includes its offset down to the minute. This too uses
/// only numbers so doesn’t require any special consideration.
FullISO,

/// The **Human format** converts the time form into a more human readable form,
/// such as one month ago, three weeks ago
Human,
}

// There are two different formatting functions because local and zoned
Expand All @@ -58,6 +63,7 @@ impl TimeFormat {
Self::ISOFormat => iso_local(time),
Self::LongISO => long_local(time),
Self::FullISO => full_local(time),
Self::Human => hu_local(time),
}
}

Expand All @@ -67,6 +73,7 @@ impl TimeFormat {
Self::ISOFormat => iso_zoned(time, zone),
Self::LongISO => long_zoned(time, zone),
Self::FullISO => full_zoned(time, zone),
Self::Human => hu_zoned(time, zone),
}
}
}
Expand Down Expand Up @@ -97,6 +104,19 @@ fn get_dateformat(date: &LocalDateTime) -> &'static DateFormat<'static> {
}
}

#[allow(trivial_numeric_casts)]
fn hu_local(time: SystemTime) -> String {
format!("{}", HumanTime::from(time))
}

#[allow(trivial_numeric_casts)]
fn hu_zoned(time: SystemTime, _: &TimeZone) -> String {

let dt: chrono::DateTime<chrono::Utc> = time.clone().into();
let tt: chrono::DateTime<chrono::Local> = dt.with_timezone(&chrono::Local);
format!("{}", HumanTime::from(tt))
}

#[allow(trivial_numeric_casts)]
fn long_local(time: SystemTime) -> String {
let date = LocalDateTime::at(systemtime_epoch(time));
Expand Down
2 changes: 1 addition & 1 deletion xtests/input-options.toml
Expand Up @@ -42,7 +42,7 @@ tags = [ 'options' ]
name = "exa displays an error for option that takes the wrong parameter"
shell = "exa -l --time-style=24"
stdout = { empty = true }
stderr = { string = "Option --time-style has no \"24\" setting (choices: default, long-iso, full-iso, iso)" }
stderr = { string = "Option --time-style has no \"24\" setting (choices: default, long-iso, full-iso, iso, relative)" }
status = 3
tags = [ 'options' ]

Expand Down
2 changes: 1 addition & 1 deletion xtests/outputs/help.ansitxt
Expand Up @@ -46,7 +46,7 @@ LONG VIEW OPTIONS
-u, --accessed use the accessed timestamp field
-U, --created use the created timestamp field
--changed use the changed timestamp field
--time-style how to format timestamps (default, iso, long-iso, full-iso)
--time-style how to format timestamps (default, iso, long-iso, full-iso, relative)
--no-permissions suppress the permissions field
--octal-permissions list each file's permission in octal format
--no-filesize suppress the filesize field
Expand Down