diff --git a/src/app.rs b/src/app.rs index f38a034..00dee63 100644 --- a/src/app.rs +++ b/src/app.rs @@ -10,6 +10,14 @@ pub fn build_app() -> App<'static, 'static> { .short("a") .long("all") .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::with_name("long") .short("l") diff --git a/src/core.rs b/src/core.rs index 4e6f965..d3c0e99 100644 --- a/src/core.rs +++ b/src/core.rs @@ -1,7 +1,7 @@ use batch::Batch; use color::{Colors, Theme}; use display::Display; -use flags::Flags; +use flags::{Flags, WhenFlag}; use meta::{FileType, Meta}; use std::path::{Path, PathBuf}; use terminal_size::terminal_size; @@ -19,16 +19,18 @@ impl Core { let mut inner_flags = flags; - let theme; - if tty_available { - // There is only the "Default" theme for now. - theme = Theme::Default; - } else { + let theme = match (tty_available, flags.color) { + (true, WhenFlag::Never) => Theme::NoColor, + (false, WhenFlag::Auto) => Theme::NoColor, + (false, WhenFlag::Always) => Theme::Default, + _ => Theme::Default, + }; + + if !tty_available { // 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 // or require a raw output (like the `wc` command). - theme = Theme::NoColor; inner_flags.display_online = true; }; diff --git a/src/flags.rs b/src/flags.rs index e97b6ce..f933250 100644 --- a/src/flags.rs +++ b/src/flags.rs @@ -7,6 +7,7 @@ pub struct Flags { pub display_online: bool, pub display_tree: bool, pub recursive: bool, + pub color: WhenFlag, } impl<'a> From> for Flags { @@ -17,6 +18,25 @@ impl<'a> From> for Flags { display_online: matches.is_present("oneline"), display_tree: matches.is_present("tree"), 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), } } }