diff options
author | Ben S <ogham@bsago.me> | 2015-02-09 17:20:42 +0000 |
---|---|---|
committer | Ben S <ogham@bsago.me> | 2015-02-09 17:20:42 +0000 |
commit | 1e48539380a49ed4a91d95debc593004bbe03e94 (patch) | |
tree | 15df6513b58de28ca24ab60fb1d9e70d8ed10386 /src/options.rs | |
parent | 4a6555b9a98ef65a345d9a5173b80e7d5f1ddc81 (diff) |
Add shorthand options for displaying timestamps
Add options -u, -U, and -m as shorthand options for displaying different
types of timestamp, not just one. It's possible to have more than one by
specifying more than one of these shorthands, but *not* when used with
--time, as this is only meant to display one at a time.
Diffstat (limited to 'src/options.rs')
-rw-r--r-- | src/options.rs | 75 |
1 files changed, 58 insertions, 17 deletions
diff --git a/src/options.rs b/src/options.rs index c78e644..cbba1fd 100644 --- a/src/options.rs +++ b/src/options.rs @@ -52,12 +52,15 @@ impl Options { opts.optflag("H", "links", "show number of hard links"); opts.optflag("i", "inode", "show each file's inode number"); opts.optflag("l", "long", "display extended details and attributes"); + opts.optflag("m", "modified", "display timestamp of most recent modification"); opts.optflag("r", "reverse", "reverse order of files"); opts.optflag("R", "recurse", "recurse into directories"); opts.optopt ("s", "sort", "field to sort by", "WORD"); opts.optflag("S", "blocks", "show number of file system blocks"); opts.optopt ("t", "time", "which timestamp to show for a file", "WORD"); opts.optflag("T", "tree", "recurse into subdirectories in a tree view"); + opts.optflag("u", "accessed", "display timestamp of last access for a file"); + opts.optflag("U", "created", "display timestamp of creation for a file"); opts.optflag("x", "across", "sort multi-column view entries across"); opts.optflag("?", "help", "show list of command-line options"); @@ -291,21 +294,56 @@ pub enum TimeType { } impl TimeType { + pub fn header(&self) -> &'static str { + match *self { + TimeType::FileAccessed => "Date Accessed", + TimeType::FileModified => "Date Modified", + TimeType::FileCreated => "Date Created", + } + } +} + +#[derive(PartialEq, Debug, Copy)] +pub struct TimeTypes { + accessed: bool, + modified: bool, + created: bool, +} + +impl TimeTypes { /// Find which field to use based on a user-supplied word. - fn deduce(matches: &getopts::Matches) -> Result<TimeType, Misfire> { + fn deduce(matches: &getopts::Matches) -> Result<TimeTypes, Misfire> { let possible_word = matches.opt_str("time"); + let modified = matches.opt_present("modified"); + let created = matches.opt_present("created"); + let accessed = matches.opt_present("accessed"); if let Some(word) = possible_word { + if modified { + return Err(Misfire::Useless("modified", true, "time")); + } + else if created { + return Err(Misfire::Useless("created", true, "time")); + } + else if accessed { + return Err(Misfire::Useless("accessed", true, "time")); + } + match word.as_slice() { - "mod" | "modified" => Ok(TimeType::FileModified), - "acc" | "accessed" => Ok(TimeType::FileAccessed), - "cr" | "created" => Ok(TimeType::FileCreated), - field => Err(TimeType::none(field)), + "mod" | "modified" => Ok(TimeTypes { accessed: false, modified: true, created: false }), + "acc" | "accessed" => Ok(TimeTypes { accessed: true, modified: false, created: false }), + "cr" | "created" => Ok(TimeTypes { accessed: false, modified: false, created: true }), + field => Err(TimeTypes::none(field)), } } else { - Ok(TimeType::FileModified) + if modified || created || accessed { + Ok(TimeTypes { accessed: accessed, modified: modified, created: created }) + } + else { + Ok(TimeTypes { accessed: false, modified: true, created: false }) + } } } @@ -313,15 +351,8 @@ impl TimeType { fn none(field: &str) -> Misfire { Misfire::InvalidOptions(getopts::Fail::UnrecognizedOption(format!("--time {}", field))) } - - pub fn header(&self) -> &'static str { - match *self { - TimeType::FileAccessed => "Date Accessed", - TimeType::FileModified => "Date Modified", - TimeType::FileCreated => "Date Created", - } - } } + /// What to do when encountering a directory? #[derive(PartialEq, Debug, Copy)] pub enum DirAction { @@ -347,7 +378,7 @@ impl DirAction { #[derive(PartialEq, Copy, Debug)] pub struct Columns { size_format: SizeFormat, - time_type: TimeType, + time_types: TimeTypes, inode: bool, links: bool, blocks: bool, @@ -358,7 +389,7 @@ impl Columns { pub fn deduce(matches: &getopts::Matches) -> Result<Columns, Misfire> { Ok(Columns { size_format: try!(SizeFormat::deduce(matches)), - time_type: try!(TimeType::deduce(matches)), + time_types: try!(TimeTypes::deduce(matches)), inode: matches.opt_present("inode"), links: matches.opt_present("links"), blocks: matches.opt_present("blocks"), @@ -391,7 +422,17 @@ impl Columns { columns.push(Group); } - columns.push(Timestamp(self.time_type)); + if self.time_types.modified { + columns.push(Timestamp(TimeType::FileModified)); + } + + if self.time_types.created { + columns.push(Timestamp(TimeType::FileCreated)); + } + + if self.time_types.accessed { + columns.push(Timestamp(TimeType::FileAccessed)); + } if cfg!(feature="git") { if let Some(d) = dir { |