2017-06-23 22:50:29 +01:00
|
|
|
|
use std::fmt;
|
2016-04-17 20:38:37 +01:00
|
|
|
|
|
2020-10-10 19:49:46 +01:00
|
|
|
|
use crate::fs::feature::xattr;
|
2018-12-08 00:43:31 +01:00
|
|
|
|
use crate::options::flags;
|
|
|
|
|
use crate::options::parser::MatchedFlags;
|
2017-08-05 14:33:32 +01:00
|
|
|
|
|
2017-06-23 21:22:39 +01:00
|
|
|
|
|
2020-12-03 02:06:29 +01:00
|
|
|
|
static USAGE_PART1: &str = "Usage:
|
2020-10-16 23:53:32 +01:00
|
|
|
|
exa [options] [files...]
|
|
|
|
|
|
|
|
|
|
META OPTIONS
|
2017-05-06 23:00:45 +01:00
|
|
|
|
-?, --help show list of command-line options
|
|
|
|
|
-v, --version show version of exa
|
|
|
|
|
|
2016-04-17 20:38:37 +01:00
|
|
|
|
DISPLAY OPTIONS
|
|
|
|
|
-1, --oneline display one entry per line
|
2017-05-06 23:00:45 +01:00
|
|
|
|
-l, --long display extended file metadata as a table
|
|
|
|
|
-G, --grid display entries as a grid (default)
|
|
|
|
|
-x, --across sort the grid across, rather than downwards
|
2016-04-17 20:38:37 +01:00
|
|
|
|
-R, --recurse recurse into directories
|
2017-05-06 23:00:45 +01:00
|
|
|
|
-T, --tree recurse into directories as a tree
|
|
|
|
|
-F, --classify display type indicator by file names
|
|
|
|
|
--colo[u]r=WHEN when to use terminal colours (always, auto, never)
|
|
|
|
|
--colo[u]r-scale highlight levels of file sizes distinctly
|
2019-07-19 09:58:50 +03:00
|
|
|
|
--icons display icons
|
2016-04-17 20:38:37 +01:00
|
|
|
|
|
|
|
|
|
FILTERING AND SORTING OPTIONS
|
2017-06-29 13:24:55 +01:00
|
|
|
|
-a, --all show hidden and 'dot' files
|
2017-05-06 23:00:45 +01:00
|
|
|
|
-d, --list-dirs list directories like regular files
|
2018-10-26 17:04:22 -06:00
|
|
|
|
-L, --level DEPTH limit the depth of recursion
|
2017-05-06 23:00:45 +01:00
|
|
|
|
-r, --reverse reverse the sort order
|
2017-08-12 11:39:12 +01:00
|
|
|
|
-s, --sort SORT_FIELD which field to sort by
|
2016-04-17 20:38:37 +01:00
|
|
|
|
--group-directories-first list directories before other files
|
2018-10-26 17:21:31 -06:00
|
|
|
|
-D, --only-dirs list only directories
|
2020-12-03 02:06:29 +01:00
|
|
|
|
-I, --ignore-glob GLOBS glob patterns (pipe-separated) of files to ignore";
|
|
|
|
|
|
|
|
|
|
static USAGE_PART2: &str = " \
|
2017-06-29 14:57:43 +01:00
|
|
|
|
Valid sort fields: name, Name, extension, Extension, size, type,
|
2017-09-13 23:26:06 +01:00
|
|
|
|
modified, accessed, created, inode, and none.
|
|
|
|
|
date, time, old, and new all refer to modified.
|
2016-04-17 20:38:37 +01:00
|
|
|
|
|
|
|
|
|
LONG VIEW OPTIONS
|
2020-10-16 23:53:32 +01:00
|
|
|
|
-b, --binary list file sizes with binary prefixes
|
|
|
|
|
-B, --bytes list file sizes in bytes, without any prefixes
|
|
|
|
|
-g, --group list each file's group
|
|
|
|
|
-h, --header add a header row to each column
|
|
|
|
|
-H, --links list each file's number of hard links
|
|
|
|
|
-i, --inode list each file's inode number
|
|
|
|
|
-m, --modified use the modified timestamp field
|
|
|
|
|
-S, --blocks show number of file system blocks
|
|
|
|
|
-t, --time FIELD which timestamp field to list (modified, accessed, created)
|
|
|
|
|
-u, --accessed use the accessed timestamp field
|
|
|
|
|
-U, --created use the created timestamp field
|
|
|
|
|
--changed use the changed timestamp field
|
|
|
|
|
--time-style how to format timestamps (default, iso, long-iso, full-iso)
|
|
|
|
|
--no-permissions suppress the permissions field
|
|
|
|
|
--octal-permissions list each file's permission in octal format
|
|
|
|
|
--no-filesize suppress the filesize field
|
|
|
|
|
--no-user suppress the user field
|
2020-12-03 02:06:29 +01:00
|
|
|
|
--no-time suppress the time field";
|
2020-10-16 23:53:32 +01:00
|
|
|
|
|
2020-12-03 02:06:29 +01:00
|
|
|
|
static GIT_FILTER_HELP: &str = " --git-ignore ignore files mentioned in '.gitignore'";
|
|
|
|
|
static GIT_VIEW_HELP: &str = " --git list each file's Git status, if tracked or ignored";
|
|
|
|
|
static EXTENDED_HELP: &str = " -@, --extended list each file's extended attributes and sizes";
|
2017-06-23 21:22:39 +01:00
|
|
|
|
|
2017-08-05 14:33:32 +01:00
|
|
|
|
|
|
|
|
|
/// All the information needed to display the help text, which depends
|
|
|
|
|
/// on which features are enabled and whether the user only wants to
|
|
|
|
|
/// see one section’s help.
|
2020-10-10 15:30:19 +01:00
|
|
|
|
#[derive(PartialEq, Debug, Copy, Clone)]
|
2020-10-16 23:53:32 +01:00
|
|
|
|
pub struct HelpString;
|
2017-06-23 21:22:39 +01:00
|
|
|
|
|
2017-08-05 14:33:32 +01:00
|
|
|
|
impl HelpString {
|
|
|
|
|
|
|
|
|
|
/// Determines how to show help, if at all, based on the user’s
|
|
|
|
|
/// command-line arguments. This one works backwards from the other
|
|
|
|
|
/// ‘deduce’ functions, returning Err if help needs to be shown.
|
Be stricter in strict mode
Now the code actually starts to use the Strictness flag that was added in the earlier commit! Well, the *code* doesn’t, but the tests do: the macros that create the test cases now have a parameter for which tests they should run. It’s usually ‘Both’ for both strict mode and default mode, but can be specified to only run in one, for when the results differ (usually when options override one another)
The downside to strict mode is that, now, *any* call to `matches.has` or `matches.get` could fail, because an option could have been specified twice, and this is the place where those are checked for. This makes the code a little less ergonomic in places, but that’s what the ? operator is for. The only place this has really had an effect is in `Classify::deduce`, which used to just return a boolean but can now fail.
In order to more thoroughly test the mode, some of the older parts of the code can now act more strict. For example, `TerminalColours::deduce` will now use the last-given option rather than searching for “colours” before “colors”.
Help and Version continue doing their own thing.
2017-08-09 09:21:29 +01:00
|
|
|
|
///
|
|
|
|
|
/// We don’t do any strict-mode error checking here: it’s OK to give
|
|
|
|
|
/// the --help or --long flags more than once. Actually checking for
|
|
|
|
|
/// errors when the user wants help is kind of petty!
|
2020-10-13 01:36:41 +01:00
|
|
|
|
pub fn deduce(matches: &MatchedFlags<'_>) -> Option<Self> {
|
Be stricter in strict mode
Now the code actually starts to use the Strictness flag that was added in the earlier commit! Well, the *code* doesn’t, but the tests do: the macros that create the test cases now have a parameter for which tests they should run. It’s usually ‘Both’ for both strict mode and default mode, but can be specified to only run in one, for when the results differ (usually when options override one another)
The downside to strict mode is that, now, *any* call to `matches.has` or `matches.get` could fail, because an option could have been specified twice, and this is the place where those are checked for. This makes the code a little less ergonomic in places, but that’s what the ? operator is for. The only place this has really had an effect is in `Classify::deduce`, which used to just return a boolean but can now fail.
In order to more thoroughly test the mode, some of the older parts of the code can now act more strict. For example, `TerminalColours::deduce` will now use the last-given option rather than searching for “colours” before “colors”.
Help and Version continue doing their own thing.
2017-08-09 09:21:29 +01:00
|
|
|
|
if matches.count(&flags::HELP) > 0 {
|
2020-10-16 23:53:32 +01:00
|
|
|
|
Some(Self)
|
2017-08-05 14:33:32 +01:00
|
|
|
|
}
|
|
|
|
|
else {
|
Replace Misfire with a testable OptionsResult
This was meant to be a small change, but it spiralled into a big one.
The original intention was to separate OptionsResult and OptionsError. With these types separated, the Help and Version variants can only be returned from the Options::parse function, and the later option-parsing functions can only return success or errors.
Also, Misfire was a silly name.
As a side-effect of Options::parse returning OptionsResult instead of Result<Options, Misfire>, we could no longer use unwrap() or unwrap_err() to get the contents out. This commit makes OptionsResult into a value type, and Options::parse a pure function. It feels like it should be one, having its return value entirely dependent on its arguments, but it also loaded locales and time zones. These parts have been moved into lazy_static references, and the code still passes tests without much change.
OptionsResult isn't PartialEq yet, because the file colouring uses a Box internally.
2020-10-12 23:47:36 +01:00
|
|
|
|
None
|
2017-08-05 14:33:32 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-23 22:50:29 +01:00
|
|
|
|
impl fmt::Display for HelpString {
|
2017-08-05 14:33:32 +01:00
|
|
|
|
|
|
|
|
|
/// Format this help options into an actual string of help
|
|
|
|
|
/// text to be displayed to the user.
|
2020-10-13 01:36:41 +01:00
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
|
2020-12-03 02:06:29 +01:00
|
|
|
|
write!(f, "{}", USAGE_PART1)?;
|
|
|
|
|
|
|
|
|
|
if cfg!(feature = "git") {
|
|
|
|
|
write!(f, "\n{}", GIT_FILTER_HELP)?;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
write!(f, "\n{}", USAGE_PART2)?;
|
2017-06-23 21:22:39 +01:00
|
|
|
|
|
2020-12-03 02:06:29 +01:00
|
|
|
|
if cfg!(feature = "git") {
|
|
|
|
|
write!(f, "\n{}", GIT_VIEW_HELP)?;
|
2017-06-23 22:50:29 +01:00
|
|
|
|
}
|
2017-06-23 21:22:39 +01:00
|
|
|
|
|
2020-10-16 23:53:32 +01:00
|
|
|
|
if xattr::ENABLED {
|
2020-12-03 02:06:29 +01:00
|
|
|
|
write!(f, "\n{}", EXTENDED_HELP)?;
|
2017-06-23 22:50:29 +01:00
|
|
|
|
}
|
2017-06-23 21:22:39 +01:00
|
|
|
|
|
2017-06-23 22:50:29 +01:00
|
|
|
|
Ok(())
|
|
|
|
|
}
|
2017-06-23 21:22:39 +01:00
|
|
|
|
}
|
2017-08-05 14:33:32 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
mod test {
|
Replace Misfire with a testable OptionsResult
This was meant to be a small change, but it spiralled into a big one.
The original intention was to separate OptionsResult and OptionsError. With these types separated, the Help and Version variants can only be returned from the Options::parse function, and the later option-parsing functions can only return success or errors.
Also, Misfire was a silly name.
As a side-effect of Options::parse returning OptionsResult instead of Result<Options, Misfire>, we could no longer use unwrap() or unwrap_err() to get the contents out. This commit makes OptionsResult into a value type, and Options::parse a pure function. It feels like it should be one, having its return value entirely dependent on its arguments, but it also loaded locales and time zones. These parts have been moved into lazy_static references, and the code still passes tests without much change.
OptionsResult isn't PartialEq yet, because the file colouring uses a Box internally.
2020-10-12 23:47:36 +01:00
|
|
|
|
use crate::options::{Options, OptionsResult};
|
2020-10-13 00:29:49 +01:00
|
|
|
|
use std::ffi::OsStr;
|
2017-08-05 14:33:32 +01:00
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn help() {
|
2020-10-13 00:29:49 +01:00
|
|
|
|
let args = vec![ OsStr::new("--help") ];
|
|
|
|
|
let opts = Options::parse(args, &None);
|
Replace Misfire with a testable OptionsResult
This was meant to be a small change, but it spiralled into a big one.
The original intention was to separate OptionsResult and OptionsError. With these types separated, the Help and Version variants can only be returned from the Options::parse function, and the later option-parsing functions can only return success or errors.
Also, Misfire was a silly name.
As a side-effect of Options::parse returning OptionsResult instead of Result<Options, Misfire>, we could no longer use unwrap() or unwrap_err() to get the contents out. This commit makes OptionsResult into a value type, and Options::parse a pure function. It feels like it should be one, having its return value entirely dependent on its arguments, but it also loaded locales and time zones. These parts have been moved into lazy_static references, and the code still passes tests without much change.
OptionsResult isn't PartialEq yet, because the file colouring uses a Box internally.
2020-10-12 23:47:36 +01:00
|
|
|
|
assert!(matches!(opts, OptionsResult::Help(_)));
|
2017-08-05 14:33:32 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn help_with_file() {
|
2020-10-13 00:29:49 +01:00
|
|
|
|
let args = vec![ OsStr::new("--help"), OsStr::new("me") ];
|
|
|
|
|
let opts = Options::parse(args, &None);
|
Replace Misfire with a testable OptionsResult
This was meant to be a small change, but it spiralled into a big one.
The original intention was to separate OptionsResult and OptionsError. With these types separated, the Help and Version variants can only be returned from the Options::parse function, and the later option-parsing functions can only return success or errors.
Also, Misfire was a silly name.
As a side-effect of Options::parse returning OptionsResult instead of Result<Options, Misfire>, we could no longer use unwrap() or unwrap_err() to get the contents out. This commit makes OptionsResult into a value type, and Options::parse a pure function. It feels like it should be one, having its return value entirely dependent on its arguments, but it also loaded locales and time zones. These parts have been moved into lazy_static references, and the code still passes tests without much change.
OptionsResult isn't PartialEq yet, because the file colouring uses a Box internally.
2020-10-12 23:47:36 +01:00
|
|
|
|
assert!(matches!(opts, OptionsResult::Help(_)));
|
2017-08-05 14:33:32 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn unhelpful() {
|
2020-10-13 00:29:49 +01:00
|
|
|
|
let args = vec![];
|
|
|
|
|
let opts = Options::parse(args, &None);
|
Replace Misfire with a testable OptionsResult
This was meant to be a small change, but it spiralled into a big one.
The original intention was to separate OptionsResult and OptionsError. With these types separated, the Help and Version variants can only be returned from the Options::parse function, and the later option-parsing functions can only return success or errors.
Also, Misfire was a silly name.
As a side-effect of Options::parse returning OptionsResult instead of Result<Options, Misfire>, we could no longer use unwrap() or unwrap_err() to get the contents out. This commit makes OptionsResult into a value type, and Options::parse a pure function. It feels like it should be one, having its return value entirely dependent on its arguments, but it also loaded locales and time zones. These parts have been moved into lazy_static references, and the code still passes tests without much change.
OptionsResult isn't PartialEq yet, because the file colouring uses a Box internally.
2020-10-12 23:47:36 +01:00
|
|
|
|
assert!(! matches!(opts, OptionsResult::Help(_))) // no help when --help isn’t passed
|
2017-08-05 14:33:32 +01:00
|
|
|
|
}
|
|
|
|
|
}
|