forked from Angey40/BaiduPCS-Go
/
tree.go
59 lines (50 loc) · 1 KB
/
tree.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package pcscommand
import (
"fmt"
"github.com/iikira/BaiduPCS-Go/baidupcs"
"strings"
)
const (
indentPrefix = "│ "
pathPrefix = "├──"
lastFilePrefix = "└──"
)
func getTree(path string, depth int) {
var (
err error
files baidupcs.FileDirectoryList
)
if depth == 0 {
path, err = getAbsPath(path)
if err != nil {
fmt.Println(err)
return
}
}
files, err = GetBaiduPCS().FilesDirectoriesList(path, baidupcs.DefaultOrderOptions)
if err != nil {
fmt.Println(err)
return
}
var (
prefix = pathPrefix
fN = len(files)
indentPrefixStr = strings.Repeat(indentPrefix, depth)
)
for i, file := range files {
if file.Isdir {
fmt.Printf("%v%v %v/\n", indentPrefixStr, pathPrefix, file.Filename)
getTree(file.Path, depth+1)
continue
}
if i+1 == fN {
prefix = lastFilePrefix
}
fmt.Printf("%v%v %v\n", indentPrefixStr, prefix, file.Filename)
}
return
}
// RunTree 列出树形图
func RunTree(path string) {
getTree(path, 0)
}