提交 5b1966d2 编写于 作者: B Benjamin Sago

Move filter and dir_action from options to fs

This commit moves the definitions of Filter and DirAction from the options module to the fs module, but leaves the parts that actually have to do with option parsing alone.

Now, the options module shouldn’t define any types that get used elsewhere in the program: it only adds functionality to types that already exist.
上级 8d96be7f
......@@ -29,9 +29,9 @@ use std::path::{Component, PathBuf};
use ansi_term::{ANSIStrings, Style};
use fs::{Dir, File};
use options::{Options, View, Mode};
use options::Options;
pub use options::Misfire;
use output::{escape, lines, grid, grid_details, details};
use output::{escape, lines, grid, grid_details, details, View, Mode};
mod fs;
mod info;
......
/// What to do when encountering a directory?
#[derive(PartialEq, Debug, Copy, Clone)]
pub enum DirAction {
/// This directory should be listed along with the regular files, instead
/// of having its contents queried.
AsFile,
/// This directory should not be listed, and should instead be opened and
/// *its* files listed separately. This is the default behaviour.
List,
/// This directory should be listed along with the regular files, and then
/// its contents should be listed afterward. The recursive contents of
/// *those* contents are dictated by the options argument.
Recurse(RecurseOptions),
}
impl DirAction {
/// Gets the recurse options, if this dir action has any.
pub fn recurse_options(&self) -> Option<RecurseOptions> {
match *self {
DirAction::Recurse(opts) => Some(opts),
_ => None,
}
}
/// Whether to treat directories as regular files or not.
pub fn treat_dirs_as_files(&self) -> bool {
match *self {
DirAction::AsFile => true,
DirAction::Recurse(RecurseOptions { tree, .. }) => tree,
_ => false,
}
}
}
/// The options that determine how to recurse into a directory.
#[derive(PartialEq, Debug, Copy, Clone)]
pub struct RecurseOptions {
/// Whether recursion should be done as a tree or as multiple individual
/// views of files.
pub tree: bool,
/// The maximum number of times that recursion should descend to, if one
/// is specified.
pub max_depth: Option<usize>,
}
impl RecurseOptions {
/// Returns whether a directory of the given depth would be too deep.
pub fn is_too_deep(&self, depth: usize) -> bool {
match self.max_depth {
None => false,
Some(d) => {
d <= depth
}
}
}
}
\ No newline at end of file
use std::cmp::Ordering;
use std::os::unix::fs::MetadataExt;
use glob;
use natord;
use fs::File;
use fs::DotFilter;
/// The **file filter** processes a vector of files before outputting them,
/// filtering and sorting the files depending on the user’s command-line
/// flags.
#[derive(PartialEq, Debug, Clone)]
pub struct FileFilter {
/// Whether directories should be listed first, and other types of file
/// second. Some users prefer it like this.
pub list_dirs_first: bool,
/// The metadata field to sort by.
pub sort_field: SortField,
/// Whether to reverse the sorting order. This would sort the largest
/// files first, or files starting with Z, or the most-recently-changed
/// ones, depending on the sort field.
pub reverse: bool,
/// Which invisible “dot” files to include when listing a directory.
///
/// Files starting with a single “.” are used to determine “system” or
/// “configuration” files that should not be displayed in a regular
/// directory listing, and the directory entries “.” and “..” are
/// considered extra-special.
///
/// This came about more or less by a complete historical accident,
/// when the original `ls` tried to hide `.` and `..`:
/// https://plus.google.com/+RobPikeTheHuman/posts/R58WgWwN9jp
///
/// When one typed ls, however, these files appeared, so either Ken or
/// Dennis added a simple test to the program. It was in assembler then,
/// but the code in question was equivalent to something like this:
/// if (name[0] == '.') continue;
/// This statement was a little shorter than what it should have been,
/// which is:
/// if (strcmp(name, ".") == 0 || strcmp(name, "..") == 0) continue;
/// but hey, it was easy.
///
/// Two things resulted.
///
/// First, a bad precedent was set. A lot of other lazy programmers
/// introduced bugs by making the same simplification. Actual files
/// beginning with periods are often skipped when they should be counted.
///
/// Second, and much worse, the idea of a "hidden" or "dot" file was
/// created. As a consequence, more lazy programmers started dropping
/// files into everyone's home directory. I don't have all that much
/// stuff installed on the machine I'm using to type this, but my home
/// directory has about a hundred dot files and I don't even know what
/// most of them are or whether they're still needed. Every file name
/// evaluation that goes through my home directory is slowed down by
/// this accumulated sludge.
pub dot_filter: DotFilter,
/// Glob patterns to ignore. Any file name that matches *any* of these
/// patterns won't be displayed in the list.
pub ignore_patterns: IgnorePatterns,
}
impl FileFilter {
/// Remove every file in the given vector that does *not* pass the
/// filter predicate for files found inside a directory.
pub fn filter_child_files(&self, files: &mut Vec<File>) {
files.retain(|f| !self.ignore_patterns.is_ignored(f));
}
/// Remove every file in the given vector that does *not* pass the
/// filter predicate for file names specified on the command-line.
///
/// The rules are different for these types of files than the other
/// type because the ignore rules can be used with globbing. For
/// example, running "exa -I='*.tmp' .vimrc" shouldn't filter out the
/// dotfile, because it's been directly specified. But running
/// "exa -I='*.ogg' music/*" should filter out the ogg files obtained
/// from the glob, even though the globbing is done by the shell!
pub fn filter_argument_files(&self, files: &mut Vec<File>) {
files.retain(|f| !self.ignore_patterns.is_ignored(f));
}
/// Sort the files in the given vector based on the sort field option.
pub fn sort_files<'a, F>(&self, files: &mut Vec<F>)
where F: AsRef<File<'a>> {
files.sort_by(|a, b| self.compare_files(a.as_ref(), b.as_ref()));
if self.reverse {
files.reverse();
}
if self.list_dirs_first {
// This relies on the fact that `sort_by` is stable.
files.sort_by(|a, b| b.as_ref().is_directory().cmp(&a.as_ref().is_directory()));
}
}
/// Compares two files to determine the order they should be listed in,
/// depending on the search field.
pub fn compare_files(&self, a: &File, b: &File) -> Ordering {
use self::SortCase::{Sensitive, Insensitive};
match self.sort_field {
SortField::Unsorted => Ordering::Equal,
SortField::Name(Sensitive) => natord::compare(&a.name, &b.name),
SortField::Name(Insensitive) => natord::compare_ignore_case(&a.name, &b.name),
SortField::Size => a.metadata.len().cmp(&b.metadata.len()),
SortField::FileInode => a.metadata.ino().cmp(&b.metadata.ino()),
SortField::ModifiedDate => a.metadata.mtime().cmp(&b.metadata.mtime()),
SortField::AccessedDate => a.metadata.atime().cmp(&b.metadata.atime()),
SortField::CreatedDate => a.metadata.ctime().cmp(&b.metadata.ctime()),
SortField::FileType => match a.type_char().cmp(&b.type_char()) { // todo: this recomputes
Ordering::Equal => natord::compare(&*a.name, &*b.name),
order => order,
},
SortField::Extension(Sensitive) => match a.ext.cmp(&b.ext) {
Ordering::Equal => natord::compare(&*a.name, &*b.name),
order => order,
},
SortField::Extension(Insensitive) => match a.ext.cmp(&b.ext) {
Ordering::Equal => natord::compare_ignore_case(&*a.name, &*b.name),
order => order,
},
}
}
}
/// User-supplied field to sort by.
#[derive(PartialEq, Debug, Copy, Clone)]
pub enum SortField {
/// Don't apply any sorting. This is usually used as an optimisation in
/// scripts, where the order doesn't matter.
Unsorted,
/// The file name. This is the default sorting.
Name(SortCase),
/// The file's extension, with extensionless files being listed first.
Extension(SortCase),
/// The file's size.
Size,
/// The file's inode. This is sometimes analogous to the order in which
/// the files were created on the hard drive.
FileInode,
/// The time at which this file was modified (the `mtime`).
///
/// As this is stored as a Unix timestamp, rather than a local time
/// instance, the time zone does not matter and will only be used to
/// display the timestamps, not compare them.
ModifiedDate,
/// The time at this file was accessed (the `atime`).
///
/// Oddly enough, this field rarely holds the *actual* accessed time.
/// Recording a read time means writing to the file each time it’s read
/// slows the whole operation down, so many systems will only update the
/// timestamp in certain circumstances. This has become common enough that
/// it’s now expected behaviour for the `atime` field.
/// http://unix.stackexchange.com/a/8842
AccessedDate,
/// The time at which this file was changed or created (the `ctime`).
///
/// Contrary to the name, this field is used to mark the time when a
/// file's metadata changed -- its permissions, owners, or link count.
///
/// In original Unix, this was, however, meant as creation time.
/// https://www.bell-labs.com/usr/dmr/www/cacm.html
CreatedDate,
/// The type of the file: directories, links, pipes, regular, files, etc.
///
/// Files are ordered according to the `PartialOrd` implementation of
/// `fs::fields::Type`, so changing that will change this.
FileType,
}
/// Whether a field should be sorted case-sensitively or case-insensitively.
///
/// This determines which of the `natord` functions to use.
#[derive(PartialEq, Debug, Copy, Clone)]
pub enum SortCase {
/// Sort files case-sensitively with uppercase first, with ‘A’ coming
/// before ‘a’.
Sensitive,
/// Sort files case-insensitively, with ‘A’ being equal to ‘a’.
Insensitive,
}
#[derive(PartialEq, Default, Debug, Clone)]
pub struct IgnorePatterns {
pub patterns: Vec<glob::Pattern>,
}
impl IgnorePatterns {
fn is_ignored(&self, file: &File) -> bool {
self.patterns.iter().any(|p| p.matches(&file.name))
}
}
......@@ -6,3 +6,5 @@ pub use self::file::{File, FileTarget};
pub mod feature;
pub mod fields;
pub mod filter;
pub mod dir_action;
use getopts;
use options::misfire::Misfire;
use fs::dir_action::{DirAction, RecurseOptions};
/// What to do when encountering a directory?
#[derive(PartialEq, Debug, Copy, Clone)]
pub enum DirAction {
/// This directory should be listed along with the regular files, instead
/// of having its contents queried.
AsFile,
/// This directory should not be listed, and should instead be opened and
/// *its* files listed separately. This is the default behaviour.
List,
/// This directory should be listed along with the regular files, and then
/// its contents should be listed afterward. The recursive contents of
/// *those* contents are dictated by the options argument.
Recurse(RecurseOptions),
}
impl DirAction {
......@@ -42,39 +26,9 @@ impl DirAction {
(false, false, _ ) => Ok(DirAction::List),
}
}
/// Gets the recurse options, if this dir action has any.
pub fn recurse_options(&self) -> Option<RecurseOptions> {
match *self {
DirAction::Recurse(opts) => Some(opts),
_ => None,
}
}
/// Whether to treat directories as regular files or not.
pub fn treat_dirs_as_files(&self) -> bool {
match *self {
DirAction::AsFile => true,
DirAction::Recurse(RecurseOptions { tree, .. }) => tree,
_ => false,
}
}
}
/// The options that determine how to recurse into a directory.
#[derive(PartialEq, Debug, Copy, Clone)]
pub struct RecurseOptions {
/// Whether recursion should be done as a tree or as multiple individual
/// views of files.
pub tree: bool,
/// The maximum number of times that recursion should descend to, if one
/// is specified.
pub max_depth: Option<usize>,
}
impl RecurseOptions {
/// Determine which files should be recursed into.
......@@ -91,14 +45,4 @@ impl RecurseOptions {
Ok(RecurseOptions { tree, max_depth })
}
/// Returns whether a directory of the given depth would be too deep.
pub fn is_too_deep(&self, depth: usize) -> bool {
match self.max_depth {
None => false,
Some(d) => {
d <= depth
}
}
}
}
\ No newline at end of file
}
use std::cmp::Ordering;
use std::os::unix::fs::MetadataExt;
use getopts;
use glob;
use natord;
use fs::File;
use fs::DotFilter;
use fs::filter::{FileFilter, SortField, SortCase, IgnorePatterns};
use options::misfire::Misfire;
/// The **file filter** processes a vector of files before outputting them,
/// filtering and sorting the files depending on the user’s command-line
/// flags.
#[derive(Default, PartialEq, Debug, Clone)]
pub struct FileFilter {
/// Whether directories should be listed first, and other types of file
/// second. Some users prefer it like this.
pub list_dirs_first: bool,
/// The metadata field to sort by.
pub sort_field: SortField,
/// Whether to reverse the sorting order. This would sort the largest
/// files first, or files starting with Z, or the most-recently-changed
/// ones, depending on the sort field.
pub reverse: bool,
/// Which invisible “dot” files to include when listing a directory.
///
/// Files starting with a single “.” are used to determine “system” or
/// “configuration” files that should not be displayed in a regular
/// directory listing, and the directory entries “.” and “..” are
/// considered extra-special.
///
/// This came about more or less by a complete historical accident,
/// when the original `ls` tried to hide `.` and `..`:
/// https://plus.google.com/+RobPikeTheHuman/posts/R58WgWwN9jp
///
/// When one typed ls, however, these files appeared, so either Ken or
/// Dennis added a simple test to the program. It was in assembler then,
/// but the code in question was equivalent to something like this:
/// if (name[0] == '.') continue;
/// This statement was a little shorter than what it should have been,
/// which is:
/// if (strcmp(name, ".") == 0 || strcmp(name, "..") == 0) continue;
/// but hey, it was easy.
///
/// Two things resulted.
///
/// First, a bad precedent was set. A lot of other lazy programmers
/// introduced bugs by making the same simplification. Actual files
/// beginning with periods are often skipped when they should be counted.
///
/// Second, and much worse, the idea of a "hidden" or "dot" file was
/// created. As a consequence, more lazy programmers started dropping
/// files into everyone's home directory. I don't have all that much
/// stuff installed on the machine I'm using to type this, but my home
/// directory has about a hundred dot files and I don't even know what
/// most of them are or whether they're still needed. Every file name
/// evaluation that goes through my home directory is slowed down by
/// this accumulated sludge.
pub dot_filter: DotFilter,
/// Glob patterns to ignore. Any file name that matches *any* of these
/// patterns won't be displayed in the list.
ignore_patterns: IgnorePatterns,
}
impl FileFilter {
/// Determines the set of file filter options to use, based on the user’s
......@@ -82,145 +19,9 @@ impl FileFilter {
ignore_patterns: IgnorePatterns::deduce(matches)?,
})
}
/// Remove every file in the given vector that does *not* pass the
/// filter predicate for files found inside a directory.
pub fn filter_child_files(&self, files: &mut Vec<File>) {
files.retain(|f| !self.ignore_patterns.is_ignored(f));
}
/// Remove every file in the given vector that does *not* pass the
/// filter predicate for file names specified on the command-line.
///
/// The rules are different for these types of files than the other
/// type because the ignore rules can be used with globbing. For
/// example, running "exa -I='*.tmp' .vimrc" shouldn't filter out the
/// dotfile, because it's been directly specified. But running
/// "exa -I='*.ogg' music/*" should filter out the ogg files obtained
/// from the glob, even though the globbing is done by the shell!
pub fn filter_argument_files(&self, files: &mut Vec<File>) {
files.retain(|f| !self.ignore_patterns.is_ignored(f));
}
/// Sort the files in the given vector based on the sort field option.
pub fn sort_files<'a, F>(&self, files: &mut Vec<F>)
where F: AsRef<File<'a>> {
files.sort_by(|a, b| self.compare_files(a.as_ref(), b.as_ref()));
if self.reverse {
files.reverse();
}
if self.list_dirs_first {
// This relies on the fact that `sort_by` is stable.
files.sort_by(|a, b| b.as_ref().is_directory().cmp(&a.as_ref().is_directory()));
}
}
/// Compares two files to determine the order they should be listed in,
/// depending on the search field.
pub fn compare_files(&self, a: &File, b: &File) -> Ordering {
use self::SortCase::{Sensitive, Insensitive};
match self.sort_field {
SortField::Unsorted => Ordering::Equal,
SortField::Name(Sensitive) => natord::compare(&a.name, &b.name),
SortField::Name(Insensitive) => natord::compare_ignore_case(&a.name, &b.name),
SortField::Size => a.metadata.len().cmp(&b.metadata.len()),
SortField::FileInode => a.metadata.ino().cmp(&b.metadata.ino()),
SortField::ModifiedDate => a.metadata.mtime().cmp(&b.metadata.mtime()),
SortField::AccessedDate => a.metadata.atime().cmp(&b.metadata.atime()),
SortField::CreatedDate => a.metadata.ctime().cmp(&b.metadata.ctime()),
SortField::FileType => match a.type_char().cmp(&b.type_char()) { // todo: this recomputes
Ordering::Equal => natord::compare(&*a.name, &*b.name),
order => order,
},
SortField::Extension(Sensitive) => match a.ext.cmp(&b.ext) {
Ordering::Equal => natord::compare(&*a.name, &*b.name),
order => order,
},
SortField::Extension(Insensitive) => match a.ext.cmp(&b.ext) {
Ordering::Equal => natord::compare_ignore_case(&*a.name, &*b.name),
order => order,
},
}
}
}
/// User-supplied field to sort by.
#[derive(PartialEq, Debug, Copy, Clone)]
pub enum SortField {
/// Don't apply any sorting. This is usually used as an optimisation in
/// scripts, where the order doesn't matter.
Unsorted,
/// The file name. This is the default sorting.
Name(SortCase),
/// The file's extension, with extensionless files being listed first.
Extension(SortCase),
/// The file's size.
Size,
/// The file's inode. This is sometimes analogous to the order in which
/// the files were created on the hard drive.
FileInode,
/// The time at which this file was modified (the `mtime`).
///
/// As this is stored as a Unix timestamp, rather than a local time
/// instance, the time zone does not matter and will only be used to
/// display the timestamps, not compare them.
ModifiedDate,
/// The time at this file was accessed (the `atime`).
///
/// Oddly enough, this field rarely holds the *actual* accessed time.
/// Recording a read time means writing to the file each time it’s read
/// slows the whole operation down, so many systems will only update the
/// timestamp in certain circumstances. This has become common enough that
/// it’s now expected behaviour for the `atime` field.
/// http://unix.stackexchange.com/a/8842
AccessedDate,
/// The time at which this file was changed or created (the `ctime`).
///
/// Contrary to the name, this field is used to mark the time when a
/// file's metadata changed -- its permissions, owners, or link count.
///
/// In original Unix, this was, however, meant as creation time.
/// https://www.bell-labs.com/usr/dmr/www/cacm.html
CreatedDate,
/// The type of the file: directories, links, pipes, regular, files, etc.
///
/// Files are ordered according to the `PartialOrd` implementation of
/// `fs::fields::Type`, so changing that will change this.
FileType,
}
/// Whether a field should be sorted case-sensitively or case-insensitively.
///
/// This determines which of the `natord` functions to use.
#[derive(PartialEq, Debug, Copy, Clone)]
pub enum SortCase {
/// Sort files case-sensitively with uppercase first, with ‘A’ coming
/// before ‘a’.
Sensitive,
/// Sort files case-insensitively, with ‘A’ being equal to ‘a’.
Insensitive,
}
impl Default for SortField {
fn default() -> SortField {
......@@ -280,12 +81,8 @@ impl DotFilter {
}
#[derive(PartialEq, Default, Debug, Clone)]
struct IgnorePatterns {
patterns: Vec<glob::Pattern>,
}
impl IgnorePatterns {
/// Determines the set of file filter options to use, based on the user’s
/// command-line arguments.
pub fn deduce(matches: &getopts::Matches) -> Result<IgnorePatterns, Misfire> {
......@@ -298,8 +95,4 @@ impl IgnorePatterns {
patterns: patterns?,
})
}
fn is_ignored(&self, file: &File) -> bool {
self.patterns.iter().any(|p| p.matches(&file.name))
}
}
//! Parsing command-line strings into exa options.
//!
//! This module imports exa’s configuration types, such as `View` (the details
//! of displaying multiple files) and `DirAction` (what to do when encountering
//! a directory), and implements `deduce` methods on them so they can be
//! configured using command-line options.
//!
//!
//! ## Useless and overridden options
//!
......@@ -69,13 +74,14 @@ use std::ffi::OsStr;
use getopts;
use fs::feature::xattr;
use fs::dir_action::DirAction;
use fs::filter::FileFilter;
use output::{View, Mode};
use output::details;
mod dir_action;
pub use self::dir_action::{DirAction, RecurseOptions};
mod filter;
pub use self::filter::{FileFilter, SortField, SortCase};
mod view;
mod help;
use self::help::HelpString;
......@@ -83,9 +89,6 @@ use self::help::HelpString;
mod misfire;
pub use self::misfire::Misfire;
mod view;
pub use self::view::{View, Mode};
mod parser;
......@@ -213,8 +216,9 @@ impl Options {
#[cfg(test)]
mod test {
use super::{Options, Misfire, SortField, SortCase};
use super::{Options, Misfire};
use fs::DotFilter;
use fs::filter::{SortField, SortCase};
use fs::feature::xattr;
fn is_helpful<T>(misfire: Result<T, Misfire>) -> bool {
......
......@@ -4,7 +4,7 @@ use getopts;
use info::filetype::FileExtensions;
use output::Colours;
use output::{grid, details};
use output::{View, Mode, grid, details};
use output::table::{TimeTypes, Environment, SizeFormat, Options as TableOptions};
use output::file_name::{Classify, FileStyle};
use output::time::TimeFormat;
......@@ -12,14 +12,6 @@ use options::Misfire;
use fs::feature::xattr;
/// The **view** contains all information about how to format output.
#[derive(Debug)]
pub struct View {
pub mode: Mode,
pub colours: Colours,
pub style: FileStyle,
}
impl View {
/// Determine which view to use and all of that view’s arguments.
......@@ -32,15 +24,6 @@ impl View {
}
/// The **mode** is the “type” of output.
#[derive(Debug)]
pub enum Mode {
Grid(grid::Options),
Details(details::Options),
GridDetails(grid::Options, details::Options),
Lines,
}
impl Mode {
/// Determine the mode from the command-line arguments.
......
......@@ -65,8 +65,9 @@ use std::path::PathBuf;
use std::vec::IntoIter as VecIntoIter;
use fs::{Dir, File};
use fs::dir_action::RecurseOptions;
use fs::filter::FileFilter;
use fs::feature::xattr::{Attribute, FileAttributes};
use options::{FileFilter, RecurseOptions};
use output::colours::Colours;
use output::cell::TextCell;
use output::tree::{TreeTrunk, TreeParams, TreeDepth};
......
......@@ -5,8 +5,8 @@ use term_grid as grid;
use fs::{Dir, File};
use fs::feature::xattr::FileAttributes;
use fs::filter::FileFilter;
use options::FileFilter;
use output::cell::TextCell;
use output::colours::Colours;
use output::details::{Options as DetailsOptions, Row as DetailsRow, Render as DetailsRender};
......
use output::file_name::FileStyle;
pub use self::cell::{TextCell, TextCellContents, DisplayWidth};
pub use self::colours::Colours;
pub use self::escape::escape;
......@@ -15,3 +17,22 @@ mod colours;
mod escape;
mod render;
mod tree;
/// The **view** contains all information about how to format output.
#[derive(Debug)]
pub struct View {
pub mode: Mode,
pub colours: Colours,
pub style: FileStyle,
}
/// The **mode** is the “type” of output.
#[derive(Debug)]
pub enum Mode {
Grid(grid::Options),
Details(details::Options),
GridDetails(grid::Options, details::Options),
Lines,
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册