Move time type picking to details module

Technically speaking, picking which timestamp to show for a file is a function of an output module, rather than the file itself. This also means that the `output::column` and `file` modules are now completely separate.
This commit is contained in:
Ben S 2015-11-15 16:12:16 +00:00
parent ca65c981f1
commit 590fb9cd60
3 changed files with 34 additions and 30 deletions

View File

@ -10,14 +10,13 @@ use std::path::{Component, Path, PathBuf};
use unicode_width::UnicodeWidthStr; use unicode_width::UnicodeWidthStr;
use dir::Dir; use dir::Dir;
use output::column::TimeType;
use self::fields as f; use self::fields as f;
// Constant table copied from https://doc.rust-lang.org/src/std/sys/unix/ext/fs.rs.html#11-259
// which is currently unstable and lacks vision for stabilization,
// see https://github.com/rust-lang/rust/issues/27712
/// Constant table copied from https://doc.rust-lang.org/src/std/sys/unix/ext/fs.rs.html#11-259
/// which is currently unstable and lacks vision for stabilization,
/// see https://github.com/rust-lang/rust/issues/27712
#[allow(dead_code)] #[allow(dead_code)]
mod modes { mod modes {
use std::os::unix::raw; use std::os::unix::raw;
@ -281,15 +280,16 @@ impl<'dir> File<'dir> {
} }
} }
/// One of this file's timestamps, as a number in seconds. pub fn modified_time(&self) -> f::Time {
pub fn timestamp(&self, time_type: TimeType) -> f::Time { f::Time(self.metadata.mtime())
let time_in_seconds = match time_type { }
TimeType::FileAccessed => self.metadata.atime(),
TimeType::FileModified => self.metadata.mtime(),
TimeType::FileCreated => self.metadata.ctime(),
};
f::Time(time_in_seconds) pub fn created_time(&self) -> f::Time {
f::Time(self.metadata.ctime())
}
pub fn accessed_time(&self) -> f::Time {
f::Time(self.metadata.mtime())
} }
/// This file's 'type'. /// This file's 'type'.

View File

@ -99,15 +99,15 @@ impl Columns {
} }
if self.time_types.modified { if self.time_types.modified {
columns.push(Column::Timestamp(TimeType::FileModified)); columns.push(Column::Timestamp(TimeType::Modified));
} }
if self.time_types.created { if self.time_types.created {
columns.push(Column::Timestamp(TimeType::FileCreated)); columns.push(Column::Timestamp(TimeType::Created));
} }
if self.time_types.accessed { if self.time_types.accessed {
columns.push(Column::Timestamp(TimeType::FileAccessed)); columns.push(Column::Timestamp(TimeType::Accessed));
} }
if cfg!(feature="git") { if cfg!(feature="git") {
@ -152,13 +152,13 @@ impl Default for SizeFormat {
pub enum TimeType { pub enum TimeType {
/// The files accessed time (`st_atime`). /// The files accessed time (`st_atime`).
FileAccessed, Accessed,
/// The files modified time (`st_mtime`). /// The files modified time (`st_mtime`).
FileModified, Modified,
/// The files creation time (`st_ctime`). /// The files creation time (`st_ctime`).
FileCreated, Created,
} }
impl TimeType { impl TimeType {
@ -166,9 +166,9 @@ impl TimeType {
/// Returns the text to use for a columns heading in the columns output. /// Returns the text to use for a columns heading in the columns output.
pub fn header(&self) -> &'static str { pub fn header(&self) -> &'static str {
match *self { match *self {
TimeType::FileAccessed => "Date Accessed", TimeType::Accessed => "Date Accessed",
TimeType::FileModified => "Date Modified", TimeType::Modified => "Date Modified",
TimeType::FileCreated => "Date Created", TimeType::Created => "Date Created",
} }
} }
} }

View File

@ -486,10 +486,14 @@ impl<U> Table<U> where U: Users {
} }
fn display(&mut self, file: &File, column: &Column, xattrs: bool) -> Cell { fn display(&mut self, file: &File, column: &Column, xattrs: bool) -> Cell {
use output::column::TimeType::*;
match *column { match *column {
Column::Permissions => self.render_permissions(file.permissions(), xattrs), Column::Permissions => self.render_permissions(file.permissions(), xattrs),
Column::FileSize(fmt) => self.render_size(file.size(), fmt), Column::FileSize(fmt) => self.render_size(file.size(), fmt),
Column::Timestamp(t) => self.render_time(file.timestamp(t)), Column::Timestamp(Modified) => self.render_time(file.modified_time()),
Column::Timestamp(Created) => self.render_time(file.created_time()),
Column::Timestamp(Accessed) => self.render_time(file.accessed_time()),
Column::HardLinks => self.render_links(file.links()), Column::HardLinks => self.render_links(file.links()),
Column::Inode => self.render_inode(file.inode()), Column::Inode => self.render_inode(file.inode()),
Column::Blocks => self.render_blocks(file.blocks()), Column::Blocks => self.render_blocks(file.blocks()),