mirror of
https://github.com/lsd-rs/lsd
synced 2024-12-14 14:12:31 +00:00
Add the --color flag support
This commit is contained in:
parent
7e158ae6b9
commit
0cee42aa69
3 changed files with 37 additions and 7 deletions
|
@ -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")
|
||||
|
|
16
src/core.rs
16
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;
|
||||
};
|
||||
|
||||
|
|
20
src/flags.rs
20
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<ArgMatches<'a>> for Flags {
|
||||
|
@ -17,6 +18,25 @@ impl<'a> From<ArgMatches<'a>> 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),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue