util: Improve tree printing function

This makes it behave more like the core GNU tree util.
This commit is contained in:
James Shubin
2024-02-21 13:30:14 -05:00
parent 733d7fb55f
commit d117cb8ed5
4 changed files with 12 additions and 5 deletions

View File

@@ -29,8 +29,15 @@ import (
// FsTree returns a string representation of the file system tree similar to the // FsTree returns a string representation of the file system tree similar to the
// well-known `tree` command. // well-known `tree` command.
func FsTree(fs afero.Fs, name string) (string, error) { func FsTree(fs afero.Fs, name string) (string, error) {
str := ".\n" // top level dir clean := path.Clean(name)
s, err := stringify(fs, path.Clean(name), []bool{}) str := clean + "/\n" // named dir
if name == "" {
str = ".\n" // relative dir
}
if name == "/" {
str = "/\n" // root dir
}
s, err := stringify(fs, clean, []bool{})
if err != nil { if err != nil {
return "", err return "", err
} }

View File

@@ -1,4 +1,4 @@
. /
├── 1 ├── 1
├── 2 ├── 2
├── 3 ├── 3

View File

@@ -1,4 +1,4 @@
. /
└── simple1/ └── simple1/
├── 1 ├── 1
├── 2 ├── 2

View File

@@ -1,4 +1,4 @@
. /
└── dest/ └── dest/
└── simple1/ └── simple1/
├── 1 ├── 1