Forráskód Böngészése

Merge pull request #437 from Hulxv/custom-time-style

 feat: supporting custom time-style
Christina Sørensen 2 éve
szülő
commit
2909853b08
7 módosított fájl, 43 hozzáadás és 21 törlés
  1. 6 6
      README.md
  2. 1 1
      man/eza.1.md
  3. 1 1
      src/options/help.rs
  4. 10 1
      src/options/mod.rs
  5. 10 5
      src/options/view.rs
  6. 5 5
      src/output/table.rs
  7. 10 2
      src/output/time.rs

+ 6 - 6
README.md

@@ -140,6 +140,7 @@ nix-env -i eza
 ```
 
 **Declarative Nix Installations**
+
 - Simple NixOS installation: [rfaulhaber/dotfiles](https://github.com/rfaulhaber/dotfiles/blob/a8d084d178efd0592b7ac02d34a450fb58913aca/nix/modules/programs/eza/default.nix#L15)
 - Using the flake via NixOS: [hallettj/home.nix](https://github.com/hallettj/home.nix/blob/a8388483e5d78e110be73c5af0e7f0e3ca8f8aa3/flake.nix#L19)
 - Using home-manager on NixOS: [Misterio77/nix-config](https://github.com/Misterio77/nix-config/blob/6867d66a2fe7899c608b9c8e5a8f9aee279d188b/home/misterio/features/cli/fish.nix#L6)
@@ -217,7 +218,6 @@ sudo port install eza
 
 [![Windows package](https://repology.org/badge/version-for-repo/winget/eza.svg)](https://repology.org/project/eza/versions)
 
-
 Eza is available on Winget.
 
 To install eza, run:
@@ -246,13 +246,13 @@ scoop install eza
 > Change `~/.zshrc` to your preferred zsh config file.
 
 ##### Clone the repository:
-   
+
 ```sh
 git clone https://github.com/eza-community/eza.git
 ```
 
 ##### Add the completion path to your zsh configuration:
-   
+
 Replace `<path_to_eza>` with the actual path where you cloned the `eza` repository.
 
 ```sh
@@ -260,7 +260,7 @@ echo 'export FPATH="<path_to_eza>/completions/zsh:$FPATH"' >> ~/.zshrc
 ```
 
 ##### Reload your zsh configuration:
-   
+
 ```sh
 source ~/.zshrc
 ```
@@ -331,9 +331,9 @@ These options are available when running with `--long` (`-l`):
 - **--changed**: use the changed timestamp field
 - **--git**: list each file’s Git status, if tracked or ignored
 - **--git-repos**: list each directory’s Git status, if tracked
-- **--git-repos-no-status**:  list whether a directory is a Git repository, but not its status (faster)
+- **--git-repos-no-status**: list whether a directory is a Git repository, but not its status (faster)
 - **--no-git**: suppress Git status (always overrides `--git`, `--git-repos`, `--git-repos-no-status`)
-- **--time-style**: how to format timestamps
+- **--time-style**: how to format timestamps. valid timestamp styles are ‘`default`’, ‘`iso`’, ‘`long-iso`’, ‘`full-iso`’, ‘`relative`', or you can use a `custom` style with '`+`' as prefix. (Ex: "`+%Y/%m/%d, %H:%M`" => "`2023/9/30, 12:00`"). [more about format syntax](https://docs.rs/chrono/latest/chrono/format/strftime/index.html).
 - **--no-permissions**: suppress the permissions field
 - **-o**, **--octal-permissions**: list each file's permission in octal format
 - **--no-filesize**: suppress the filesize field

+ 1 - 1
man/eza.1.md

@@ -172,7 +172,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`’, ‘`full-iso`’, and ‘`relative`’.
+: Valid timestamp styles are ‘`default`’, ‘`iso`’, ‘`long-iso`’, ‘`full-iso`’, ‘`relative`', or you can use a `custom` style with '`+`' as prefix. (Ex: "`+%Y/%m/%d, %H:%M`" => "`2023/9/30, 12:00`"). for more details about format syntax, please read: https://docs.rs/chrono/latest/chrono/format/strftime/index.html
 
 `-u`, `--accessed`
 : Use the accessed timestamp field.

+ 1 - 1
src/options/help.rs

@@ -61,7 +61,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, relative)
+  --time-style             how to format timestamps (default, iso, long-iso, full-iso, relative, or a custom style with '+' as prefix. Ex: '+%Y/%m/%d')
   --no-permissions         suppress the permissions field
   -o, --octal-permissions  list each file's permission in octal format
   --no-filesize            suppress the filesize field

+ 10 - 1
src/options/mod.rs

@@ -209,7 +209,16 @@ impl Options {
     }
 }
 
-/// The result of the `Options::getopts` function.
+/// The result of the `Options::parse` function.
+///
+/// NOTE: We disallow the `large_enum_variant` lint here, because we're not
+/// overly concerned about variant fragmentation. We can do this because we are
+/// reasonably sure that the error variant will be rare, and only on faulty
+/// program execution and thus boxing the large variant will be a waste of
+/// resources, but should we come to use it more, we should reconsider.
+///
+/// See <https://github.com/eza-community/eza/pull/437#issuecomment-1738470254>
+#[allow(clippy::large_enum_variant)]
 #[derive(Debug)]
 pub enum OptionsResult<'args> {
     /// The options were parsed successfully.

+ 10 - 5
src/options/view.rs

@@ -319,6 +319,9 @@ impl TimeFormat {
             "iso" => Ok(Self::ISOFormat),
             "long-iso" => Ok(Self::LongISO),
             "full-iso" => Ok(Self::FullISO),
+            fmt if fmt.starts_with('+') => Ok(Self::Custom {
+                fmt: fmt[1..].to_owned(),
+            }),
             _ => Err(OptionsError::BadArgument(&flags::TIME_STYLE, word)),
         }
     }
@@ -533,11 +536,13 @@ mod test {
         test!(empty:     TimeFormat <- [], None;                            Both => like Ok(TimeFormat::DefaultFormat));
 
         // Individual settings
-        test!(default:   TimeFormat <- ["--time-style=default"], None;      Both => like Ok(TimeFormat::DefaultFormat));
-        test!(iso:       TimeFormat <- ["--time-style", "iso"], None;       Both => like Ok(TimeFormat::ISOFormat));
-        test!(relative:  TimeFormat <- ["--time-style", "relative"], None;  Both => like Ok(TimeFormat::Relative));
-        test!(long_iso:  TimeFormat <- ["--time-style=long-iso"], None;     Both => like Ok(TimeFormat::LongISO));
-        test!(full_iso:  TimeFormat <- ["--time-style", "full-iso"], None;  Both => like Ok(TimeFormat::FullISO));
+        test!(default:          TimeFormat <- ["--time-style=default"], None;      Both => like Ok(TimeFormat::DefaultFormat));
+        test!(iso:              TimeFormat <- ["--time-style", "iso"], None;       Both => like Ok(TimeFormat::ISOFormat));
+        test!(relative:         TimeFormat <- ["--time-style", "relative"], None;  Both => like Ok(TimeFormat::Relative));
+        test!(long_iso:         TimeFormat <- ["--time-style=long-iso"], None;     Both => like Ok(TimeFormat::LongISO));
+        test!(full_iso:         TimeFormat <- ["--time-style", "full-iso"], None;  Both => like Ok(TimeFormat::FullISO));
+        test!(custom_style:     TimeFormat <- ["--time-style", "+%Y/%m/%d"], None; Both => like Ok(TimeFormat::Custom { .. }));
+        test!(bad_custom_style: TimeFormat <- ["--time-style", "%Y/%m/%d"], None;  Both => err OptionsError::BadArgument(&flags::TIME_STYLE, OsString::from("%Y/%m/%d")));
 
         // Overriding
         test!(actually:  TimeFormat <- ["--time-style=default", "--time-style", "iso"], None;  Last => like Ok(TimeFormat::ISOFormat));

+ 5 - 5
src/output/table.rs

@@ -375,7 +375,7 @@ impl<'a> Table<'a> {
             columns,
             git,
             env,
-            time_format: options.time_format,
+            time_format: options.time_format.clone(),
             size_format: options.size_format,
             #[cfg(unix)]
             user_format: options.user_format,
@@ -471,22 +471,22 @@ impl<'a> Table<'a> {
             Column::Timestamp(TimeType::Modified) => file.modified_time().render(
                 self.theme.ui.date,
                 self.env.time_offset,
-                self.time_format,
+                self.time_format.clone(),
             ),
             Column::Timestamp(TimeType::Changed) => file.changed_time().render(
                 self.theme.ui.date,
                 self.env.time_offset,
-                self.time_format,
+                self.time_format.clone(),
             ),
             Column::Timestamp(TimeType::Created) => file.created_time().render(
                 self.theme.ui.date,
                 self.env.time_offset,
-                self.time_format,
+                self.time_format.clone(),
             ),
             Column::Timestamp(TimeType::Accessed) => file.accessed_time().render(
                 self.theme.ui.date,
                 self.env.time_offset,
-                self.time_format,
+                self.time_format.clone(),
             ),
         }
     }

+ 10 - 2
src/output/time.rs

@@ -20,9 +20,9 @@ use unicode_width::UnicodeWidthStr;
 /// own enum variants. It’s not worth looking the locale up if the formatter
 /// prints month names as numbers.
 ///
-/// Currently exa does not support *custom* styles, where the user enters a
+/// Also, eza supports *custom* styles, where the user enters a
 /// format string in an environment variable or something. Just these four.
-#[derive(PartialEq, Eq, Debug, Copy, Clone)]
+#[derive(PartialEq, Eq, Debug, Clone)]
 pub enum TimeFormat {
     /// The **default format** uses the user’s locale to print month names,
     /// and specifies the timestamp down to the minute for recent times, and
@@ -45,6 +45,9 @@ pub enum TimeFormat {
 
     /// Use a relative but fixed width representation.
     Relative,
+
+    /// Use a custom format
+    Custom { fmt: String },
 }
 
 impl TimeFormat {
@@ -56,6 +59,7 @@ impl TimeFormat {
             Self::LongISO        => long(time),
             Self::FullISO        => full(time),
             Self::Relative       => relative(time),
+            Self::Custom { fmt } => custom(time, &fmt),
         };
     }
 }
@@ -111,6 +115,10 @@ fn full(time: &DateTime<FixedOffset>) -> String {
     time.format("%Y-%m-%d %H:%M:%S.%f %z").to_string()
 }
 
+fn custom(time: &DateTime<FixedOffset>, fmt: &str) -> String {
+    time.format(fmt).to_string()
+}
+
 lazy_static! {
 
     static ref CURRENT_YEAR: i32 = Local::now().year();