-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper.go
92 lines (83 loc) · 1.74 KB
/
helper.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package internal
import (
"fmt"
"github.com/ibrahimaydinnet/go-tree/constant"
"io"
"math"
"os"
"strings"
)
func search(files []os.FileInfo, subStr string) []os.FileInfo {
result := []os.FileInfo{}
for _, f := range files {
if f.IsDir() || strings.Contains(f.Name(), subStr) {
result = append(result, f)
}
}
return result
}
func justDirs(files []os.FileInfo) []os.FileInfo {
dirs := []os.FileInfo{}
for _, f := range files {
if f.IsDir() {
dirs = append(dirs, f)
}
}
return dirs
}
func appendSeperator(path string) string {
if !strings.HasSuffix(path, constant.Seperator) {
return fmt.Sprintf("%s%s", path, constant.Seperator)
}
return path
}
func formatSize(s int64) string {
GB := int64(1024 * 1024 * 1024)
MB := int64(1024 * 1024)
KB := int64(1024)
unit := "B"
amount := 0.
if s > GB {
amount = math.Round(float64(s/GB)*100) / 100
unit = "G"
}
if s > MB {
amount = math.Round(float64(s/MB)*100) / 100
unit = "M"
}
if s > KB {
amount = math.Round(float64(s/KB)*100) / 100
unit = "K"
}
return fmt.Sprintf("%v%v", amount, unit)
}
func exceptHiddens(all, win bool, path string, files []os.FileInfo) []os.FileInfo {
result := []os.FileInfo{}
for _, file := range files {
if !all && strings.HasPrefix(file.Name(), ".") {
continue
}
if !win && isHidden(path, file.Name()) {
continue
}
result = append(result, file)
}
return result
}
func writeToFile(output, outputFile string) (err error) {
file, err := os.Create(outputFile)
if err != nil {
return
}
defer file.Close()
if _, err = io.WriteString(file, output); err != nil {
return
}
if err = file.Sync(); err != nil {
return
}
return
}
func colorize(color, str string) string {
return fmt.Sprintf("%s%s%s", color, str, resetColor)
}