Implement lsd --tree -d

This commit implements the `-d` option in combination with `--tree` to mimic `tree -d` behaviour. There are also changes to the behaviour of `--tree` and `-R` such that they follow the same behaviour as `tree -d` and `ls -R` i.e. not follwoing symlinked directories unless the `-L` flag is specified.
This commit is contained in:
Jay 2020-10-02 14:39:08 +01:00 committed by Abin Simon
parent b6bd1d3b55
commit c67c639327
5 changed files with 30 additions and 2 deletions

View file

@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Add support for `--extensionsort` `-X` from [aldhsu](https://github.com/aldhsu)
- Add support for `--versionsort` `-v` from [zwpaper](https://github.com/zwpaper)
- Add support for config symlink arrow from [zwpaper](https://github.com/zwpaper) [#409](https://github.com/Peltoche/lsd/issues/409)
- Implement `--tree -d`, analogous to `tree -d` from [0jdxt](https://github.com/0jdxt)
### Changed
- Use last sort flag for sort field from [meain](https://github.com/meain)
### Fixed

View file

@ -116,7 +116,6 @@ pub fn build() -> App<'static, 'static> {
.conflicts_with("almost-all")
.conflicts_with("depth")
.conflicts_with("recursive")
.conflicts_with("tree")
.help("Display directories themselves, and not their contents"),
)
.arg(

View file

@ -15,6 +15,7 @@ pub enum Display {
AlmostAll,
DirectoryItself,
DisplayOnlyVisible,
TreeD,
}
impl Display {
@ -45,7 +46,11 @@ impl Configurable<Self> for Display {
} else if matches.is_present("almost-all") {
Some(Self::AlmostAll)
} else if matches.is_present("directory-only") {
Some(Self::DirectoryItself)
if matches.is_present("tree") {
Some(Self::TreeD)
} else {
Some(Self::DirectoryItself)
}
} else {
None
}

View file

@ -116,6 +116,11 @@ impl Meta {
}
};
// skip files for --tree -d
if flags.display == Display::TreeD && !entry_meta.file_type.is_dirlike() {
continue;
}
match entry_meta.recurse_into(depth - 1, &flags) {
Ok(content) => entry_meta.content = content,
Err(err) => {

View file

@ -397,6 +397,24 @@ fn test_bad_utf_8_name() {
.stdout(predicate::str::is_match("bad-name\u{fffd}\u{fffd}.ext\n$").unwrap());
}
#[test]
fn test_tree_d() {
let tmp = tempdir();
tmp.child("one").touch().unwrap();
tmp.child("two").touch().unwrap();
tmp.child("one.d").create_dir_all().unwrap();
tmp.child("one.d/one").touch().unwrap();
tmp.child("one.d/one.d").create_dir_all().unwrap();
tmp.child("two.d").create_dir_all().unwrap();
cmd()
.arg(tmp.path())
.arg("--tree")
.arg("-d")
.assert()
.stdout(predicate::str::is_match("├── one.d\n│ └── one.d\n└── two.d\n$").unwrap());
}
fn cmd() -> Command {
Command::cargo_bin(env!("CARGO_PKG_NAME")).unwrap()
}