mirror of
https://github.com/Llewellynvdm/exa.git
synced 2025-01-13 17:02:58 +00:00
Add --colo[u]r options
The user can now control the output parameters by specifying the console width and when to show colours. Fixes #75.
This commit is contained in:
parent
c543e61ced
commit
f92459d957
112
src/options.rs
112
src/options.rs
@ -1,4 +1,5 @@
|
|||||||
use std::cmp;
|
use std::cmp;
|
||||||
|
use std::env::var_os;
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
use std::num::ParseIntError;
|
use std::num::ParseIntError;
|
||||||
use std::os::unix::fs::MetadataExt;
|
use std::os::unix::fs::MetadataExt;
|
||||||
@ -47,6 +48,8 @@ impl Options {
|
|||||||
opts.optflag("R", "recurse", "recurse into directories");
|
opts.optflag("R", "recurse", "recurse into directories");
|
||||||
opts.optflag("T", "tree", "recurse into subdirectories in a tree view");
|
opts.optflag("T", "tree", "recurse into subdirectories in a tree view");
|
||||||
opts.optflag("x", "across", "sort multi-column view entries across");
|
opts.optflag("x", "across", "sort multi-column view entries across");
|
||||||
|
opts.optopt ("", "color", "when to show anything in colours", "WHEN");
|
||||||
|
opts.optopt ("", "colour", "when to show anything in colours (alternate spelling)", "WHEN");
|
||||||
|
|
||||||
// Filtering and sorting options
|
// Filtering and sorting options
|
||||||
opts.optflag("", "group-directories-first", "list directories before other files");
|
opts.optflag("", "group-directories-first", "list directories before other files");
|
||||||
@ -158,13 +161,27 @@ impl View {
|
|||||||
Err(Useless("oneline", true, "long"))
|
Err(Useless("oneline", true, "long"))
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
let term_colours = try!(TerminalColours::deduce(matches));
|
||||||
|
let colours = match term_colours {
|
||||||
|
TerminalColours::Always => Colours::colourful(),
|
||||||
|
TerminalColours::Never => Colours::plain(),
|
||||||
|
TerminalColours::Automatic => {
|
||||||
|
if dimensions().is_some() {
|
||||||
|
Colours::colourful()
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
Colours::plain()
|
||||||
|
}
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
let details = Details {
|
let details = Details {
|
||||||
columns: Some(try!(Columns::deduce(matches))),
|
columns: Some(try!(Columns::deduce(matches))),
|
||||||
header: matches.opt_present("header"),
|
header: matches.opt_present("header"),
|
||||||
recurse: dir_action.recurse_options(),
|
recurse: dir_action.recurse_options(),
|
||||||
filter: filter,
|
filter: filter,
|
||||||
xattr: xattr::ENABLED && matches.opt_present("extended"),
|
xattr: xattr::ENABLED && matches.opt_present("extended"),
|
||||||
colours: if dimensions().is_some() { Colours::colourful() } else { Colours::plain() },
|
colours: colours,
|
||||||
};
|
};
|
||||||
|
|
||||||
Ok(details)
|
Ok(details)
|
||||||
@ -193,14 +210,23 @@ impl View {
|
|||||||
};
|
};
|
||||||
|
|
||||||
let other_options_scan = || {
|
let other_options_scan = || {
|
||||||
if let Some((width, _)) = dimensions() {
|
let term_colours = try!(TerminalColours::deduce(matches));
|
||||||
|
let term_width = try!(TerminalWidth::deduce(matches));
|
||||||
|
|
||||||
|
if let TerminalWidth::Set(width) = term_width {
|
||||||
|
let colours = match term_colours {
|
||||||
|
TerminalColours::Always => Colours::colourful(),
|
||||||
|
TerminalColours::Never => Colours::plain(),
|
||||||
|
TerminalColours::Automatic => Colours::colourful(),
|
||||||
|
};
|
||||||
|
|
||||||
if matches.opt_present("oneline") {
|
if matches.opt_present("oneline") {
|
||||||
if matches.opt_present("across") {
|
if matches.opt_present("across") {
|
||||||
Err(Useless("across", true, "oneline"))
|
Err(Useless("across", true, "oneline"))
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
let lines = Lines {
|
let lines = Lines {
|
||||||
colours: Colours::colourful(),
|
colours: colours,
|
||||||
};
|
};
|
||||||
|
|
||||||
Ok(View::Lines(lines))
|
Ok(View::Lines(lines))
|
||||||
@ -213,7 +239,7 @@ impl View {
|
|||||||
recurse: dir_action.recurse_options(),
|
recurse: dir_action.recurse_options(),
|
||||||
filter: filter,
|
filter: filter,
|
||||||
xattr: false,
|
xattr: false,
|
||||||
colours: if dimensions().is_some() { Colours::colourful() } else { Colours::plain() },
|
colours: colours,
|
||||||
};
|
};
|
||||||
|
|
||||||
Ok(View::Details(details))
|
Ok(View::Details(details))
|
||||||
@ -222,7 +248,7 @@ impl View {
|
|||||||
let grid = Grid {
|
let grid = Grid {
|
||||||
across: matches.opt_present("across"),
|
across: matches.opt_present("across"),
|
||||||
console_width: width,
|
console_width: width,
|
||||||
colours: Colours::colourful(),
|
colours: colours,
|
||||||
};
|
};
|
||||||
|
|
||||||
Ok(View::Grid(grid))
|
Ok(View::Grid(grid))
|
||||||
@ -232,8 +258,15 @@ impl View {
|
|||||||
// If the terminal width couldn’t be matched for some reason, such
|
// If the terminal width couldn’t be matched for some reason, such
|
||||||
// as the program’s stdout being connected to a file, then
|
// as the program’s stdout being connected to a file, then
|
||||||
// fallback to the lines view.
|
// fallback to the lines view.
|
||||||
|
|
||||||
|
let colours = match term_colours {
|
||||||
|
TerminalColours::Always => Colours::colourful(),
|
||||||
|
TerminalColours::Never => Colours::plain(),
|
||||||
|
TerminalColours::Automatic => Colours::plain(),
|
||||||
|
};
|
||||||
|
|
||||||
let lines = Lines {
|
let lines = Lines {
|
||||||
colours: Colours::plain(),
|
colours: colours,
|
||||||
};
|
};
|
||||||
|
|
||||||
Ok(View::Lines(lines))
|
Ok(View::Lines(lines))
|
||||||
@ -444,6 +477,60 @@ impl OptionSet for SortField {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#[derive(PartialEq, Debug)]
|
||||||
|
enum TerminalWidth {
|
||||||
|
Set(usize),
|
||||||
|
Unset,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl OptionSet for TerminalWidth {
|
||||||
|
fn deduce(_: &getopts::Matches) -> Result<TerminalWidth, Misfire> {
|
||||||
|
if let Some(columns) = var_os("COLUMNS").and_then(|s| s.into_string().ok()) {
|
||||||
|
match columns.parse() {
|
||||||
|
Ok(width) => Ok(TerminalWidth::Set(width)),
|
||||||
|
Err(e) => Err(Misfire::FailedParse(e)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if let Some((width, _)) = dimensions() {
|
||||||
|
Ok(TerminalWidth::Set(width))
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
Ok(TerminalWidth::Unset)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#[derive(PartialEq, Debug)]
|
||||||
|
enum TerminalColours {
|
||||||
|
Always,
|
||||||
|
Automatic,
|
||||||
|
Never,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for TerminalColours {
|
||||||
|
fn default() -> TerminalColours {
|
||||||
|
TerminalColours::Automatic
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl OptionSet for TerminalColours {
|
||||||
|
fn deduce(matches: &getopts::Matches) -> Result<TerminalColours, Misfire> {
|
||||||
|
if let Some(word) = matches.opt_str("color").or(matches.opt_str("colour")) {
|
||||||
|
match &*word {
|
||||||
|
"always" => Ok(TerminalColours::Always),
|
||||||
|
"auto" | "automatic" => Ok(TerminalColours::Automatic),
|
||||||
|
"never" => Ok(TerminalColours::Never),
|
||||||
|
otherwise => Err(Misfire::bad_argument("color", otherwise))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
Ok(TerminalColours::default())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
impl OptionSet for Columns {
|
impl OptionSet for Columns {
|
||||||
fn deduce(matches: &getopts::Matches) -> Result<Columns, Misfire> {
|
fn deduce(matches: &getopts::Matches) -> Result<Columns, Misfire> {
|
||||||
Ok(Columns {
|
Ok(Columns {
|
||||||
@ -594,12 +681,13 @@ impl fmt::Display for Misfire {
|
|||||||
|
|
||||||
static OPTIONS: &'static str = r##"
|
static OPTIONS: &'static str = r##"
|
||||||
DISPLAY OPTIONS
|
DISPLAY OPTIONS
|
||||||
-1, --oneline display one entry per line
|
-1, --oneline display one entry per line
|
||||||
-G, --grid display entries in a grid view (default)
|
-G, --grid display entries in a grid view (default)
|
||||||
-l, --long display extended details and attributes
|
-l, --long display extended details and attributes
|
||||||
-R, --recurse recurse into directories
|
-R, --recurse recurse into directories
|
||||||
-T, --tree recurse into subdirectories in a tree view
|
-T, --tree recurse into subdirectories in a tree view
|
||||||
-x, --across sort multi-column view entries across
|
-x, --across sort multi-column view entries across
|
||||||
|
--color, --colour when to colourise the output
|
||||||
|
|
||||||
FILTERING AND SORTING OPTIONS
|
FILTERING AND SORTING OPTIONS
|
||||||
-a, --all show dot-files
|
-a, --all show dot-files
|
||||||
|
Loading…
Reference in New Issue
Block a user