Add the --color flag support

This commit is contained in:
Peltoche 2018-12-08 14:21:32 +01:00 committed by Pierre Peltier
parent 7e158ae6b9
commit 0cee42aa69
3 changed files with 37 additions and 7 deletions

View file

@ -10,6 +10,14 @@ pub fn build_app() -> App<'static, 'static> {
.short("a") .short("a")
.long("all") .long("all")
.help("Do not ignore entries starting with ."), .help("Do not ignore entries starting with ."),
).arg(
Arg::with_name("color")
.long("color")
.possible_value("always")
.possible_value("auto")
.possible_value("never")
.default_value("auto")
.help("When to use terminal colours"),
).arg( ).arg(
Arg::with_name("long") Arg::with_name("long")
.short("l") .short("l")

View file

@ -1,7 +1,7 @@
use batch::Batch; use batch::Batch;
use color::{Colors, Theme}; use color::{Colors, Theme};
use display::Display; use display::Display;
use flags::Flags; use flags::{Flags, WhenFlag};
use meta::{FileType, Meta}; use meta::{FileType, Meta};
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use terminal_size::terminal_size; use terminal_size::terminal_size;
@ -19,16 +19,18 @@ impl Core {
let mut inner_flags = flags; let mut inner_flags = flags;
let theme; let theme = match (tty_available, flags.color) {
if tty_available { (true, WhenFlag::Never) => Theme::NoColor,
// There is only the "Default" theme for now. (false, WhenFlag::Auto) => Theme::NoColor,
theme = Theme::Default; (false, WhenFlag::Always) => Theme::Default,
} else { _ => Theme::Default,
};
if !tty_available {
// The output is not a tty, this means the command is piped. (ex: lsd -l | less) // The output is not a tty, this means the command is piped. (ex: lsd -l | less)
// //
// Most of the programs does not handle correctly the ansi colors // Most of the programs does not handle correctly the ansi colors
// or require a raw output (like the `wc` command). // or require a raw output (like the `wc` command).
theme = Theme::NoColor;
inner_flags.display_online = true; inner_flags.display_online = true;
}; };

View file

@ -7,6 +7,7 @@ pub struct Flags {
pub display_online: bool, pub display_online: bool,
pub display_tree: bool, pub display_tree: bool,
pub recursive: bool, pub recursive: bool,
pub color: WhenFlag,
} }
impl<'a> From<ArgMatches<'a>> for Flags { impl<'a> From<ArgMatches<'a>> for Flags {
@ -17,6 +18,25 @@ impl<'a> From<ArgMatches<'a>> for Flags {
display_online: matches.is_present("oneline"), display_online: matches.is_present("oneline"),
display_tree: matches.is_present("tree"), display_tree: matches.is_present("tree"),
recursive: matches.is_present("recursive"), recursive: matches.is_present("recursive"),
color: WhenFlag::from(matches.value_of("color").unwrap()),
}
}
}
#[derive(Clone, Debug, Copy, PartialEq, Eq)]
pub enum WhenFlag {
Always,
Auto,
Never,
}
impl<'a> From<&'a str> for WhenFlag {
fn from(when: &'a str) -> Self {
match when {
"always" => WhenFlag::Always,
"auto" => WhenFlag::Auto,
"never" => WhenFlag::Never,
_ => panic!("invalid \"when\" flag: {}", when),
} }
} }
} }