diff --git a/src/core.rs b/src/core.rs index cc526a1ff..2b1b0bfa0 100644 --- a/src/core.rs +++ b/src/core.rs @@ -1,7 +1,7 @@ use crate::color::Colors; use crate::display; use crate::flags::{ - ColorOption, Display, Flags, HyperlinkOption, Layout, Literal, SortOrder, ThemeOption, + ColorOption, Display, Flags, Header, HyperlinkOption, Layout, Literal, SortOrder, ThemeOption, }; use crate::git::GitCache; use crate::icon::Icons; @@ -44,8 +44,6 @@ impl Core { #[cfg(target_os = "windows")] let console_color_ok = crossterm::ansi_support::supports_ansi(); - let mut inner_flags = flags.clone(); - let color_theme = match (tty_available && console_color_ok, flags.color.when) { (_, ColorOption::Never) | (false, ColorOption::Auto) => ThemeOption::NoColor, _ => flags.color.theme.clone(), @@ -71,9 +69,14 @@ impl Core { // // Most of the programs does not handle correctly the ansi colors // or require a raw output (like the `wc` command). - inner_flags.layout = Layout::OneLine; - + flags.header = Header(false); flags.literal = Literal(true); + + // GNU ls will set layout to OneLine when not a tty + // but lsd support `--tree`, and `--tree` should not be overwrite even piped + if flags.layout != Layout::Tree { + flags.layout = Layout::OneLine; + } }; let sorters = sort::assemble_sorters(&flags); diff --git a/tests/lscompatible.rs b/tests/lscompatible.rs new file mode 100644 index 000000000..f5c74db42 --- /dev/null +++ b/tests/lscompatible.rs @@ -0,0 +1,54 @@ +extern crate assert_cmd; +extern crate predicates; + +use assert_cmd::prelude::*; +use assert_fs::prelude::*; +use std::process::{Command, Stdio}; + +fn cmd() -> Command { + Command::cargo_bin(env!("CARGO_PKG_NAME")).unwrap() +} + +fn tempdir() -> assert_fs::TempDir { + assert_fs::TempDir::new().unwrap() +} + +#[test] +fn test_pipe_should_use_line() { + let dir = tempdir(); + dir.child("one").touch().unwrap(); + dir.child("two").touch().unwrap(); + + let lsd = cmd() + .arg("--ignore-config") + .arg(dir.path()) + .stdout(Stdio::piped()) + .spawn() + .expect("Failed to start lsd process"); + let lsd_out = lsd.stdout.expect("Failed to open ls stdout"); + + let cat_lsd = Command::new("cat") + .stdin(Stdio::from(lsd_out)) + .stdout(Stdio::piped()) + .spawn() + .expect("Failed to start cat process"); + let output_lsd = cat_lsd + .wait_with_output() + .expect("Failed to wait on cat lsd"); + + let ls = Command::new("ls") + .arg(dir.path()) + .stdout(Stdio::piped()) + .spawn() + .expect("Failed to start ls process"); + let ls_out = ls.stdout.expect("Failed to open ls stdout"); + + let cat_ls = Command::new("cat") + .stdin(Stdio::from(ls_out)) + .stdout(Stdio::piped()) + .spawn() + .expect("Failed to start cat process"); + let output_ls = cat_ls.wait_with_output().expect("Failed to wait on cat ls"); + + assert_eq!(output_ls.stdout, output_lsd.stdout); +}