mirror of
https://github.com/Llewellynvdm/exa.git
synced 2024-11-05 04:17:51 +00:00
Avoid an allocation when printing help text
This commit is contained in:
parent
4e32b7fca9
commit
4018165e26
@ -1,4 +1,4 @@
|
|||||||
use getopts::Matches;
|
use std::fmt;
|
||||||
|
|
||||||
|
|
||||||
static OPTIONS: &str = r##"
|
static OPTIONS: &str = r##"
|
||||||
@ -45,25 +45,31 @@ LONG VIEW OPTIONS
|
|||||||
static GIT_HELP: &str = r##" --git list each file's Git status, if tracked"##;
|
static GIT_HELP: &str = r##" --git list each file's Git status, if tracked"##;
|
||||||
static EXTENDED_HELP: &str = r##" -@, --extended list each file's extended attributes and sizes"##;
|
static EXTENDED_HELP: &str = r##" -@, --extended list each file's extended attributes and sizes"##;
|
||||||
|
|
||||||
|
#[derive(PartialEq, Debug)]
|
||||||
pub fn help_string(matches: &Matches, git: bool, xattr: bool) -> String {
|
pub struct HelpString {
|
||||||
let mut help = String::from("Usage:\n exa [options] [files...]\n");
|
pub only_long: bool,
|
||||||
|
pub git: bool,
|
||||||
if !matches.opt_present("long") {
|
pub xattrs: bool,
|
||||||
help.push_str(OPTIONS);
|
}
|
||||||
}
|
|
||||||
|
impl fmt::Display for HelpString {
|
||||||
help.push_str(LONG_OPTIONS);
|
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
|
||||||
|
try!(write!(f, "Usage:\n exa [options] [files...]\n"));
|
||||||
if git {
|
|
||||||
help.push('\n');
|
if !self.only_long {
|
||||||
help.push_str(GIT_HELP);
|
try!(write!(f, "{}", OPTIONS));
|
||||||
}
|
}
|
||||||
|
|
||||||
if xattr {
|
try!(write!(f, "{}", LONG_OPTIONS));
|
||||||
help.push('\n');
|
|
||||||
help.push_str(EXTENDED_HELP);
|
if self.git {
|
||||||
}
|
try!(write!(f, "\n{}", GIT_HELP));
|
||||||
|
}
|
||||||
help
|
|
||||||
|
if self.xattrs {
|
||||||
|
try!(write!(f, "\n{}", EXTENDED_HELP));
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -4,6 +4,8 @@ use std::num::ParseIntError;
|
|||||||
use getopts;
|
use getopts;
|
||||||
use glob;
|
use glob;
|
||||||
|
|
||||||
|
use options::help::HelpString;
|
||||||
|
|
||||||
|
|
||||||
/// A list of legal choices for an argument-taking option
|
/// A list of legal choices for an argument-taking option
|
||||||
#[derive(PartialEq, Debug)]
|
#[derive(PartialEq, Debug)]
|
||||||
@ -28,7 +30,7 @@ pub enum Misfire {
|
|||||||
|
|
||||||
/// The user asked for help. This isn’t strictly an error, which is why
|
/// The user asked for help. This isn’t strictly an error, which is why
|
||||||
/// this enum isn’t named Error!
|
/// this enum isn’t named Error!
|
||||||
Help(String),
|
Help(HelpString),
|
||||||
|
|
||||||
/// The user wanted the version number.
|
/// The user wanted the version number.
|
||||||
Version,
|
Version,
|
||||||
|
@ -12,7 +12,7 @@ mod filter;
|
|||||||
pub use self::filter::{FileFilter, SortField, SortCase};
|
pub use self::filter::{FileFilter, SortField, SortCase};
|
||||||
|
|
||||||
mod help;
|
mod help;
|
||||||
use self::help::help_string;
|
use self::help::HelpString;
|
||||||
|
|
||||||
mod misfire;
|
mod misfire;
|
||||||
pub use self::misfire::Misfire;
|
pub use self::misfire::Misfire;
|
||||||
@ -103,7 +103,13 @@ impl Options {
|
|||||||
};
|
};
|
||||||
|
|
||||||
if matches.opt_present("help") {
|
if matches.opt_present("help") {
|
||||||
return Err(Misfire::Help(help_string(&matches, cfg!(feature="git"), xattr::ENABLED)));
|
let help = HelpString {
|
||||||
|
only_long: matches.opt_present("long"),
|
||||||
|
git: cfg!(feature="git"),
|
||||||
|
xattrs: xattr::ENABLED,
|
||||||
|
};
|
||||||
|
|
||||||
|
return Err(Misfire::Help(help));
|
||||||
}
|
}
|
||||||
else if matches.opt_present("version") {
|
else if matches.opt_present("version") {
|
||||||
return Err(Misfire::Version);
|
return Err(Misfire::Version);
|
||||||
|
Loading…
Reference in New Issue
Block a user