diff options
author | Canop <cano.petrole@gmail.com> | 2021-02-22 19:10:05 +0100 |
---|---|---|
committer | Canop <cano.petrole@gmail.com> | 2021-02-22 19:13:30 +0100 |
commit | 93b1ffcc701a7753e8376c80b0c10683f7e3e36d (patch) | |
tree | b3369ab327260c9f31fa21b854621a41a5fcf049 | |
parent | df9c63511c7d80f6e147fdf410af0eeda9be8ba3 (diff) |
replace `--no-style` with `--color` taking `yes`, `no` or `auto`
When in `auto` (which is default) broot detects whether the output
is piped and removes styles and colors in that case.
Fix #347
-rw-r--r-- | CHANGELOG.md | 4 | ||||
-rw-r--r-- | src/clap.rs | 9 | ||||
-rw-r--r-- | src/cli.rs | 17 | ||||
-rw-r--r-- | website/docs/export.md | 8 | ||||
-rw-r--r-- | website/docs/img/20210222-cmd-pt-unstyled.png | bin | 0 -> 31364 bytes |
5 files changed, 33 insertions, 5 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index 3ad2e4a..e64d278 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +### next +- fix style characters being written in `--no-style` mode - Fix #346 +- replace `--no-style` with `--color` taking `yes`, `no` or `auto`, with detection of output being piped in `auto` mode (default). `--no-style` is still usable but it's not documented anymore - Fix #347 + <a name="v1.2.4"></a> ### v1.2.4 - 2021-02-14 - :line_down_no_cycle and :line_up_nocycle. They may be mapped instead of :line_up and :line_down when you don't want to cycle (ie arrive on top when you go down past the end of the tree/list) - Fix #344 diff --git a/src/clap.rs b/src/clap.rs index d161747..765a8bf 100644 --- a/src/clap.rs +++ b/src/clap.rs @@ -159,6 +159,14 @@ pub fn clap_app() -> clap::App<'static, 'static> { .help("Semicolon separated commands to execute"), ) .arg( + clap::Arg::with_name("color") + .long("color") + .takes_value(true) + .possible_values(&["yes", "no", "auto"]) + .default_value("auto") + .help("Whether to have styles and colors (auto is default and usually OK)"), + ) + .arg( clap::Arg::with_name("conf") .long("conf") .takes_value(true) @@ -184,6 +192,7 @@ pub fn clap_app() -> clap::App<'static, 'static> { ) .arg( clap::Arg::with_name("no-style") + .hidden(true) // we're deprecating this in favor of --color .long("no-style") .help("Whether to remove all style and colors from exported tree"), ) @@ -120,6 +120,21 @@ fn get_root_path(cli_args: &ArgMatches<'_>) -> Result<PathBuf, ProgramError> { Ok(canonicalize_root(&root)?) } +fn is_output_piped() -> bool { + unsafe { libc::isatty(libc::STDOUT_FILENO) == 0 } +} + +fn is_no_style(cli_matches: &ArgMatches) -> bool { + if cli_matches.is_present("no-style") { + return true; + } + match cli_matches.value_of("color") { + Some("yes") => false, + Some("no") => true, + _ => is_output_piped(), + } +} + /// run the application, and maybe return a launchable /// which must be run after broot pub fn run() -> Result<Option<Launchable>, ProgramError> { @@ -189,7 +204,7 @@ pub fn run() -> Result<Option<Launchable>, ProgramError> { let file_export_path = cli_matches.value_of("file-export-path").map(str::to_string); let cmd_export_path = cli_matches.value_of("cmd-export-path").map(str::to_string); let commands = cli_matches.value_of("commands").map(str::to_string); - let no_style = cli_matches.is_present("no-style"); + let no_style = is_no_style(&cli_matches); let height = cli_matches.value_of("height").and_then(|s| s.parse().ok()); let root = get_root_path(&cli_matches)?; diff --git a/website/docs/export.md b/website/docs/export.md index 64d5a83..8f396ce 100644 --- a/website/docs/export.md +++ b/website/docs/export.md @@ -10,9 +10,9 @@ Example with a filter: ![exported styled tree](img/20190321-cmd-pt-styled.png) -Example without style or color, thanks to `--no-style`: +Example without style or color, thanks to `--color no`: -![exported unstyled tree](img/20190321-cmd-pt-unstyled.png) +![exported unstyled tree](img/20210222-cmd-pt-unstyled.png) This is also how would look the tree directly exported into a file. @@ -24,13 +24,13 @@ You don't have to enter broot, you may also directly get the tree by using the [ For example - broot --cmd ":pt" --no-style > my_file.txt + br --cmd ":pt" > my_file.txt will export the local tree to the `my_file.txt` file. Or - broot --no-style > tree.txt + br > tree.txt in which case you'll manually do `:pt` when in broot but after having had the opportunity to navigate, filter and change toggles as desired. diff --git a/website/docs/img/20210222-cmd-pt-unstyled.png b/website/docs/img/20210222-cmd-pt-unstyled.png Binary files differnew file mode 100644 index 0000000..761c06a --- /dev/null +++ b/website/docs/img/20210222-cmd-pt-unstyled.png |