Skip to content

Commit 4a0d89d

Browse files
init commit
1 parent b1af694 commit 4a0d89d

5 files changed

Lines changed: 234 additions & 0 deletions

File tree

README.md

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,76 @@
11
# TreeSize
2+
23
Print folders in tree format and display file's size.
4+
5+
# Examples
6+
7+
```bash
8+
╰─± go run main.go -f /Users/dapeng/Desktop/code/python3/learn
9+
Total size: 43.5KiB
10+
Output in tree format:
11+
.
12+
└── learn
13+
├── zero.py (704 B)
14+
├── brackets.py (551 B)
15+
├── celery_learn
16+
├── main.py (1.9KiB)
17+
├── spawn_fork.py (457 B)
18+
├── generic_type.py (993 B)
19+
├── demo.py (512 B)
20+
├── demo1.py (1.5KiB)
21+
├── iterator_folder
22+
│ ├── test.py (1.8KiB)
23+
│ ├── __init__.py (71 B)
24+
│ ├── __pycache__
25+
│ │ ├── _base.cpython-37.pyc (640 B)
26+
│ │ └── __init__.cpython-37.pyc (221 B)
27+
│ ├── product_i.py (1010 B)
28+
│ ├── permutation_i.py (788 B)
29+
│ ├── _base.py (380 B)
30+
│ ├── qtlogfh.log (3.4KiB)
31+
│ ├── accumulate_i.py (557 B)
32+
│ └── compress_i.py (558 B)
33+
├── hw
34+
│ ├── sort_str.py (437 B)
35+
│ ├── div2.py (248 B)
36+
│ ├── hex.py (596 B)
37+
│ ├── hj22.py (692 B)
38+
│ ├── hj31.py (265 B)
39+
│ ├── hj35.py (819 B)
40+
│ ├── hj54.py (3.7KiB)
41+
│ ├── merge_value.py (310 B)
42+
│ └── number.py (346 B)
43+
├── zaro1.py (153 B)
44+
├── IndexPy
45+
│ ├── demo.py (338 B)
46+
│ ├── .idea
47+
│ │ ├── workspace.xml (8.8KiB)
48+
│ │ ├── .gitignore (176 B)
49+
│ │ ├── IndexPy.iml (327 B)
50+
│ │ ├── misc.xml (195 B)
51+
│ │ ├── modules.xml (266 B)
52+
│ │ ├── inspectionProfiles
53+
│ │ │ ├── profiles_settings.xml (174 B)
54+
│ │ │ └── Project_Default.xml (1.2KiB)
55+
│ │ └── codeStyles
56+
│ │ └── codeStyleConfig.xml (149 B)
57+
│ ├── test.json (421 B)
58+
│ ├── demo1.py (453 B)
59+
│ └── __pycache__
60+
│ └── demo1.cpython-39.pyc (530 B)
61+
├── socket
62+
│ ├── client.py (612 B)
63+
│ ├── server.py (715 B)
64+
│ └── socket.io.folder
65+
├── descriptor
66+
│ ├── d1.py (1.1KiB)
67+
│ └── d3.py (872 B)
68+
├── goCode
69+
│ ├── sortStr.go (732 B)
70+
│ ├── go.mod (23 B)
71+
│ ├── hj34.go (345 B)
72+
│ ├── main.go (468 B)
73+
│ └── reverseStr.go (573 B)
74+
└── __pycache__
75+
└── zero.cpython-310.pyc (696 B)
76+
```

go.mod

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
module github.com/GoToUse/TreeSize
2+
3+
go 1.19
4+
5+
require (
6+
github.com/GoToUse/treeprint v0.0.0-20230224140308-24dbc04deaaf
7+
github.com/stretchr/testify v1.8.1
8+
)
9+
10+
require (
11+
github.com/davecgh/go-spew v1.1.1 // indirect
12+
github.com/pmezard/go-difflib v1.0.0 // indirect
13+
gopkg.in/yaml.v3 v3.0.1 // indirect
14+
)

go.sum

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
github.com/GoToUse/treeprint v0.0.0-20230224140308-24dbc04deaaf h1:1lOzGFFDSzWGCSmqb/q8wWEN3RkddQYLikXuzWwjnIQ=
2+
github.com/GoToUse/treeprint v0.0.0-20230224140308-24dbc04deaaf/go.mod h1:h4NTOWMR3IF9LTbUCiNE5kvwrvBECGQAWnNownTKhYU=
3+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
4+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
5+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
6+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
7+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
8+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
9+
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
10+
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
11+
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
12+
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
13+
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
14+
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
15+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
16+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
17+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
18+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
19+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

main.go

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
package main
2+
3+
import (
4+
"flag"
5+
"fmt"
6+
"io/fs"
7+
"os"
8+
"path"
9+
"sync"
10+
"sync/atomic"
11+
12+
"github.com/GoToUse/treeprint"
13+
)
14+
15+
// 存储大小之间的单位
16+
const unit = 1024
17+
18+
// command line arguments
19+
var flag_folder_path string
20+
21+
// global variable
22+
var te = treeprint.New()
23+
24+
func calc(entry fs.DirEntry, wg *sync.WaitGroup, folder string, total *int64, tree treeprint.Tree) {
25+
defer wg.Done()
26+
27+
if entry.IsDir() {
28+
size, err := Parallel(path.Join(folder, entry.Name()), tree)
29+
if err != nil {
30+
panic(err)
31+
}
32+
atomic.AddInt64(total, size)
33+
return
34+
}
35+
36+
info, err := entry.Info()
37+
if err != nil {
38+
panic(err)
39+
}
40+
41+
size := info.Size()
42+
atomic.AddInt64(total, size)
43+
tree.AddNode(fmt.Sprintf("%s (%s)", path.Base(entry.Name()), ByteCountIEC(size)))
44+
}
45+
46+
// Parallel execution, fast enough
47+
func Parallel(folder string, tree treeprint.Tree) (total int64, e error) {
48+
var wg sync.WaitGroup
49+
entrys, err := os.ReadDir(folder)
50+
// 不记录子目录的大小
51+
branch := tree.AddBranch(path.Base(folder))
52+
53+
// catch panic
54+
defer func() {
55+
if err := recover(); err != nil {
56+
e = fmt.Errorf("%v", err)
57+
}
58+
}()
59+
60+
if err != nil {
61+
return 0, err
62+
}
63+
64+
entrysLen := len(entrys)
65+
66+
if entrysLen == 0 {
67+
return 0, nil
68+
}
69+
70+
wg.Add(entrysLen)
71+
72+
for i := 0; i < entrysLen; i++ {
73+
go calc(entrys[i], &wg, folder, &total, branch)
74+
}
75+
76+
wg.Wait()
77+
return total, nil
78+
}
79+
80+
// ByteCountIEC, 以1024作为基数, 将字节转为对应的KB等单位
81+
func ByteCountIEC(b int64) string {
82+
if b < unit {
83+
return fmt.Sprintf("%d B", b)
84+
}
85+
div, exp := int64(unit), 0
86+
for n := b / unit; n >= unit; n /= unit {
87+
div *= unit
88+
exp++
89+
}
90+
return fmt.Sprintf("%.1f%ciB",
91+
float64(b)/float64(div), "KMGTPE"[exp])
92+
}
93+
94+
func init() {
95+
flag.StringVar(&flag_folder_path, "f", ".", "Folder path.")
96+
flag.Parse()
97+
}
98+
99+
func main() {
100+
size, err := Parallel(flag_folder_path, te)
101+
if err != nil {
102+
panic(err)
103+
}
104+
105+
for _, d := range []string{fmt.Sprintf("Total size: %s", ByteCountIEC(size)), "Output in tree format:", te.String()} {
106+
fmt.Println(d)
107+
}
108+
}

main_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package main
2+
3+
import (
4+
"testing"
5+
6+
"github.com/GoToUse/treeprint"
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
func TestParallel(t *testing.T) {
11+
is := assert.New(t)
12+
tree := treeprint.New()
13+
14+
size, err := Parallel("/Users/dapeng/Desktop/code/Git", tree)
15+
16+
is.Nil(err)
17+
18+
is.Equal(size, int64(2636490533))
19+
}

0 commit comments

Comments
 (0)