forked from Angey40/BaiduPCS-Go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ls.go
69 lines (62 loc) · 1.33 KB
/
ls.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
60
61
62
63
64
65
66
67
68
69
package baidupcscmd
import (
"fmt"
"github.com/iikira/BaiduPCS-Go/util"
"os"
"text/template"
)
// RunLs 执行列目录
func RunLs(path string) {
path, err := getAbsPath(path)
if err != nil {
fmt.Println(err)
return
}
files, err := info.FileList(path)
if err != nil {
fmt.Println(err)
return
}
if len(files) == 0 {
RunGetMeta(path)
return
}
for k := range files {
if files[k].Isdir {
files[k].Path += "/"
}
}
tmpl, err := template.New("ls").Funcs(
template.FuncMap{
"convertFileSize": func(size int64) string {
res := pcsutil.ConvertFileSize(size)
if res == "0" {
return "- "
}
return res
},
"timeFmt": pcsutil.FormatTime,
"totalSize": func() string {
return pcsutil.ConvertFileSize(files.TotalSize())
},
"fdCount": func() string {
fN, dN := files.Count()
return fmt.Sprintf("文件总数: %d,\t目录总数: %d", fN, dN)
},
},
).Parse(
`
文件大小 创建日期 文件(目录)
------------------------------------------------------------------------------{{range .}}
{{convertFileSize .Size}} {{timeFmt .Ctime}} {{.Path}} {{end}}
------------------------------------------------------------------------------
总大小: {{totalSize}} {{fdCount}}
`)
if err != nil {
panic(err)
}
err = tmpl.Execute(os.Stdout, files)
if err != nil {
panic(err)
}
}