Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#1052 fix lsd pipe output #1059

Merged
merged 5 commits into from
Aug 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -66,12 +64,17 @@ impl Core {

let icon_separator = flags.icons.separator.0.clone();

// The output is not a tty, this means the command is piped. e.g.
//
// lsd -l | less
//
// Most of the programs does not handle correctly the ansi colors
// or require a raw output (like the `wc` command).
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).
inner_flags.layout = Layout::OneLine;
// we should not overwrite the tree layout
if flags.layout != Layout::Tree {
flags.layout = Layout::OneLine;
}

flags.literal = Literal(true);
};
Expand Down
55 changes: 13 additions & 42 deletions src/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,8 @@ fn inner_display_grid(
// Maybe skip showing the directory meta now; show its contents later.
if skip_dirs
&& (matches!(meta.file_type, FileType::Directory { .. })
|| (matches!(meta.file_type, FileType::SymLink { is_dir: true })
&& flags.layout != Layout::OneLine))
|| (matches!(meta.file_type, FileType::SymLink { is_dir: true }))
&& flags.blocks.0.len() == 1)
{
continue;
}
Expand Down Expand Up @@ -167,7 +167,7 @@ fn inner_display_grid(
output += &grid.fit_into_columns(flags.blocks.0.len()).to_string();
}

let should_display_folder_path = should_display_folder_path(depth, metas, flags);
let should_display_folder_path = should_display_folder_path(depth, metas);

// print the folder content
for meta in metas {
Expand Down Expand Up @@ -301,16 +301,15 @@ fn inner_display_tree(
cells
}

fn should_display_folder_path(depth: usize, metas: &[Meta], flags: &Flags) -> bool {
fn should_display_folder_path(depth: usize, metas: &[Meta]) -> bool {
if depth > 0 {
true
} else {
let folder_number = metas
.iter()
.filter(|x| {
matches!(x.file_type, FileType::Directory { .. })
|| (matches!(x.file_type, FileType::SymLink { is_dir: true })
&& flags.layout != Layout::OneLine)
|| (matches!(x.file_type, FileType::SymLink { is_dir: true }))
})
.count();

Expand Down Expand Up @@ -925,23 +924,20 @@ mod tests {
const NO: bool = false;

assert_eq!(
should_display_folder_path(0, &[file.clone()], &Flags::default()),
should_display_folder_path(0, &[file.clone()]),
YES // doesn't matter since there is no folder
);
assert_eq!(should_display_folder_path(0, &[dir.clone()]), NO);
assert_eq!(
should_display_folder_path(0, &[dir.clone()], &Flags::default()),
NO
);
assert_eq!(
should_display_folder_path(0, &[file.clone(), dir.clone()], &Flags::default()),
should_display_folder_path(0, &[file.clone(), dir.clone()]),
YES
);
assert_eq!(
should_display_folder_path(0, &[dir.clone(), dir.clone()], &Flags::default()),
should_display_folder_path(0, &[dir.clone(), dir.clone()]),
YES
);
assert_eq!(
should_display_folder_path(0, &[file.clone(), file.clone()], &Flags::default()),
should_display_folder_path(0, &[file.clone(), file.clone()]),
YES // doesn't matter since there is no folder
);

Expand All @@ -966,43 +962,18 @@ mod tests {
std::os::unix::fs::symlink("dir", &link_path).unwrap();
let link = Meta::from_path(&link_path, false, PermissionFlag::Rwx).unwrap();

let grid_flags = Flags {
layout: Layout::Grid,
..Flags::default()
};

let oneline_flags = Flags {
layout: Layout::OneLine,
..Flags::default()
};

const YES: bool = true;
const NO: bool = false;

assert_eq!(
should_display_folder_path(0, &[link.clone()], &grid_flags),
NO
);
assert_eq!(
should_display_folder_path(0, &[link.clone()], &oneline_flags),
YES // doesn't matter since this link will be expanded as a directory
);
assert_eq!(should_display_folder_path(0, &[link.clone()]), NO);

assert_eq!(
should_display_folder_path(0, &[file.clone(), link.clone()], &grid_flags),
should_display_folder_path(0, &[file.clone(), link.clone()]),
YES
);
assert_eq!(
should_display_folder_path(0, &[file.clone(), link.clone()], &oneline_flags),
YES // doesn't matter since this link will be expanded as a directory
);

assert_eq!(
should_display_folder_path(0, &[dir.clone(), link.clone()], &grid_flags),
YES
);
assert_eq!(
should_display_folder_path(0, &[dir.clone(), link.clone()], &oneline_flags),
should_display_folder_path(0, &[dir.clone(), link.clone()]),
YES
);

Expand Down
6 changes: 3 additions & 3 deletions src/meta/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ impl Meta {
match self.file_type {
FileType::Directory { .. } => (),
FileType::SymLink { is_dir: true } => {
if flags.layout == Layout::OneLine {
if flags.blocks.0.len() > 1 {
return Ok((None, ExitCode::OK));
}
}
Expand All @@ -98,14 +98,14 @@ impl Meta {
&& flags.layout != Layout::Tree
{
let mut current_meta = self.clone();
current_meta.name.name = ".".to_owned();
".".clone_into(&mut current_meta.name.name);

let mut parent_meta = Self::from_path(
&self.path.join(Component::ParentDir),
flags.dereference.0,
flags.permission,
)?;
parent_meta.name.name = "..".to_owned();
"..".clone_into(&mut parent_meta.name.name);

current_meta.git_status = cache.and_then(|cache| cache.get(&current_meta.path, true));
parent_meta.git_status = cache.and_then(|cache| cache.get(&parent_meta.path, true));
Expand Down
37 changes: 33 additions & 4 deletions tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,27 +213,42 @@ fn test_list_broken_link_ok() {
.assert()
.stderr(predicate::str::contains(matched).not());
}

// ls link
// should show dir content
#[cfg(unix)]
#[test]
fn test_nosymlink_on_non_long() {
let dir = tempdir();
dir.child("target").touch().unwrap();
dir.child("target").child("inside").touch().unwrap();
let link = dir.path().join("link");
let link_icon = "⇒";
fs::symlink("target", &link).unwrap();

cmd()
.arg("-l")
.arg("--ignore-config")
.arg(&link)
.assert()
.stdout(predicate::str::contains(link_icon));
.stdout(predicate::str::contains(link_icon).not());
}

// ls -l link
// should show the link itself
#[cfg(unix)]
#[test]
fn test_symlink_on_long() {
let dir = tempdir();
dir.child("target").child("inside").touch().unwrap();
let link = dir.path().join("link");
let link_icon = "⇒";
fs::symlink("target", &link).unwrap();

cmd()
.arg("-l")
.arg("--ignore-config")
.arg(&link)
.assert()
.stdout(predicate::str::contains(link_icon).not());
.stdout(predicate::str::contains(link_icon));
}

#[cfg(unix)]
Expand Down Expand Up @@ -322,6 +337,7 @@ fn test_dereference_link_broken_link_output() {
.stdout(predicate::str::starts_with("l????????? ? ? ? ?"));
}

/// should work both tty available and not
#[cfg(unix)]
#[test]
fn test_show_folder_content_of_symlink() {
Expand All @@ -338,6 +354,8 @@ fn test_show_folder_content_of_symlink() {
.stdout(predicate::str::starts_with("inside"));
}

/// ls -l link
/// should show the link itself
#[cfg(unix)]
#[test]
fn test_no_show_folder_content_of_symlink_for_long() {
Expand All @@ -353,6 +371,17 @@ fn test_no_show_folder_content_of_symlink_for_long() {
.assert()
.stdout(predicate::str::starts_with("lrw"))
.stdout(predicate::str::contains("⇒"));
}

/// ls -l link/
/// should show the dir content
#[cfg(unix)]
#[test]
fn test_show_folder_content_of_symlink_for_long_tail_slash() {
let dir = tempdir();
dir.child("target").child("inside").touch().unwrap();
let link = dir.path().join("link");
fs::symlink("target", link).unwrap();

cmd()
.arg("-l")
Expand Down
Loading