Explorar el Código

feat: supporting custom time-style

Hulxv hace 2 años
padre
commit
e4a8040f1c
Se han modificado 4 ficheros con 25 adiciones y 12 borrados
  1. 1 1
      src/options/help.rs
  2. 5 0
      src/options/view.rs
  3. 5 5
      src/output/table.rs
  4. 14 6
      src/output/time.rs

+ 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

+ 5 - 0
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)),
         }
     }
@@ -538,6 +541,8 @@ mod test {
         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(),
             ),
         }
     }

+ 14 - 6
src/output/time.rs

@@ -22,7 +22,7 @@ use unicode_width::UnicodeWidthStr;
 ///
 /// Currently exa does not support *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,17 +45,21 @@ pub enum TimeFormat {
 
     /// Use a relative but fixed width representation.
     Relative,
+
+    /// Use a custom format
+    Custom { fmt: String },
 }
 
 impl TimeFormat {
     pub fn format(self, time: &DateTime<FixedOffset>) -> String {
         #[rustfmt::skip]
         return match self {
-            Self::DefaultFormat  => default(time),
-            Self::ISOFormat      => iso(time),
-            Self::LongISO        => long(time),
-            Self::FullISO        => full(time),
-            Self::Relative       => relative(time),
+            Self::DefaultFormat           => default(time),
+            Self::ISOFormat               => iso(time),
+            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();