From 8b520edf3d667b8f37278be7b3a313b2972db7a8 Mon Sep 17 00:00:00 2001 From: Ben S Date: Mon, 9 Feb 2015 18:14:05 +0000 Subject: [PATCH] Only display the year if it's last year Otherwise, just display the hour and minute. --- src/column.rs | 20 ++++++++++---------- src/file.rs | 31 +++++++++++++++++++------------ src/options.rs | 10 +++++++--- 3 files changed, 36 insertions(+), 25 deletions(-) diff --git a/src/column.rs b/src/column.rs index 8b46d9c..11aae59 100644 --- a/src/column.rs +++ b/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", } } } diff --git a/src/file.rs b/src/file.rs index 07b56a1..92732fb 100644 --- a/src/file.rs +++ b/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(&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()) } diff --git a/src/options.rs b/src/options.rs index cbba1fd..e12b349 100644 --- a/src/options.rs +++ b/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") { -- GitLab