Просмотр исходного кода

Only display the year if it's last year

Otherwise, just display the hour and minute.
Ben S 11 лет назад
Родитель
Сommit
8b520edf3d
3 измененных файлов с 36 добавлено и 25 удалено
  1. 10 10
      src/column.rs
  2. 19 12
      src/file.rs
  3. 7 3
      src/options.rs

+ 10 - 10
src/column.rs

@@ -8,7 +8,7 @@ use options::{SizeFormat, TimeType};
 pub enum Column {
     Permissions,
     FileSize(SizeFormat),
-    Timestamp(TimeType),
+    Timestamp(TimeType, i64),
     Blocks,
     User,
     Group,
@@ -43,15 +43,15 @@ impl Column {
     /// to have a header row printed.
     pub fn header(&self) -> &'static str {
         match *self {
-            Column::Permissions  => "Permissions",
-            Column::FileSize(_)  => "Size",
-            Column::Timestamp(t) => t.header(),
-            Column::Blocks       => "Blocks",
-            Column::User         => "User",
-            Column::Group        => "Group",
-            Column::HardLinks    => "Links",
-            Column::Inode        => "inode",
-            Column::GitStatus    => "Git",
+            Column::Permissions     => "Permissions",
+            Column::FileSize(_)     => "Size",
+            Column::Timestamp(t, _) => t.header(),
+            Column::Blocks          => "Blocks",
+            Column::User            => "User",
+            Column::Group           => "Group",
+            Column::HardLinks       => "Links",
+            Column::Inode           => "inode",
+            Column::GitStatus       => "Git",
         }
     }
 }

+ 19 - 12
src/file.rs

@@ -13,7 +13,7 @@ use pad::Alignment;
 use number_prefix::{binary_prefix, decimal_prefix, Prefixed, Standalone, PrefixNames};
 
 use datetime;
-use datetime::local::LocalDateTime;
+use datetime::local::{LocalDateTime, DatePiece};
 
 use column::{Column, Cell};
 use column::Column::*;
@@ -99,15 +99,15 @@ impl<'a> File<'a> {
     /// Get the data for a column, formatted as a coloured string.
     pub fn display<U: Users>(&self, column: &Column, users_cache: &mut U) -> Cell {
         match *column {
-            Permissions  => self.permissions_string(),
-            FileSize(f)  => self.file_size(f),
-            Timestamp(t) => self.timestamp(t),
-            HardLinks    => self.hard_links(),
-            Inode        => self.inode(),
-            Blocks       => self.blocks(),
-            User         => self.user(users_cache),
-            Group        => self.group(users_cache),
-            GitStatus    => self.git_status(),
+            Permissions     => self.permissions_string(),
+            FileSize(f)     => self.file_size(f),
+            Timestamp(t, y) => self.timestamp(t, y),
+            HardLinks       => self.hard_links(),
+            Inode           => self.inode(),
+            Blocks          => self.blocks(),
+            User            => self.user(users_cache),
+            Group           => self.group(users_cache),
+            GitStatus       => self.git_status(),
         }
     }
 
@@ -303,8 +303,7 @@ impl<'a> File<'a> {
         }
     }
 
-    fn timestamp(&self, time_type: TimeType) -> Cell {
-        let format = date_format!("{:Y} {:M} {2>:D} {2>:h}:{02>:m}");
+    fn timestamp(&self, time_type: TimeType, current_year: i64) -> Cell {
 
         // Need to convert these values from milliseconds into seconds.
         let time_in_seconds = match time_type {
@@ -314,6 +313,14 @@ impl<'a> File<'a> {
         } as i64 / 1000;
 
         let date = LocalDateTime::at(time_in_seconds);
+
+        let format = if date.year() == current_year {
+                date_format!("{2>:D} {:M} {2>:h}:{02>:m}")
+            }
+            else {
+                date_format!("{2>:D} {:M} {4>:Y}")
+            };
+
         Cell::paint(Blue.normal(), format.format(date).as_slice())
     }
 

+ 7 - 3
src/options.rs

@@ -12,6 +12,8 @@ use std::fmt;
 use getopts;
 use natord;
 
+use datetime::local::{LocalDateTime, DatePiece};
+
 use self::Misfire::*;
 
 /// The *Options* struct represents a parsed version of the user's
@@ -422,16 +424,18 @@ impl Columns {
             columns.push(Group);
         }
 
+        let current_year = LocalDateTime::now().year();
+
         if self.time_types.modified {
-            columns.push(Timestamp(TimeType::FileModified));
+            columns.push(Timestamp(TimeType::FileModified, current_year));
         }
 
         if self.time_types.created {
-            columns.push(Timestamp(TimeType::FileCreated));
+            columns.push(Timestamp(TimeType::FileCreated, current_year));
         }
 
         if self.time_types.accessed {
-            columns.push(Timestamp(TimeType::FileAccessed));
+            columns.push(Timestamp(TimeType::FileAccessed, current_year));
         }
 
         if cfg!(feature="git") {