-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
259 lines (224 loc) ยท 6.43 KB
/
main.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
package main
import (
"flag"
"fmt"
"os"
"os/user"
"path/filepath"
"strings"
"syscall"
"time"
)
func countFiles(path string) int {
files, err := os.ReadDir(path)
if err != nil {
return 0
}
return len(files)
}
func ReadFolder(showDetails bool, path string, showAll bool, maxDepth int, showEmojis bool, showOnlyFiles bool, showOnlyFolders bool, showCountFiles bool) {
readFolderRecursive(showDetails, path, showAll, 0, maxDepth, "", showEmojis, showOnlyFiles, showOnlyFolders, showCountFiles)
}
func readFolderRecursive(showDetails bool, path string, showAll bool, depth, maxDepth int, prefix string, showEmojis bool, showOnlyFiles bool, showOnlyFolders bool, showCountFiles bool) {
if maxDepth >= 0 && depth > maxDepth {
return
}
files, err := os.ReadDir(path)
if err != nil {
fmt.Printf("Error reading folder: %s : %v\n", path, err)
return
}
for i, f := range files {
if !showAll && strings.HasPrefix(f.Name(), ".") {
continue
}
fileName := f.Name()
filePath := filepath.Join(path, fileName)
var details string
if showDetails {
details = getDetails(filePath)
}
if showOnlyFolders && !f.IsDir() {
continue
}
if showOnlyFiles && f.IsDir() {
continue
}
if f.IsDir() {
numFiles := countFiles(filePath)
if showCountFiles && numFiles > 0 {
fileName = fmt.Sprintf("[%d]", numFiles) + fileName
}
if showEmojis {
fileName = "๐" + fileName
} else {
fileName = fileName + "/"
}
} else if showEmojis {
fileName = "๐" + fileName
}
if depth == 0 {
if i == len(files)-1 {
fmt.Printf("%s%sโโโ%s\n", details, prefix, fileName)
if f.IsDir() {
readFolderRecursive(showDetails, filePath, showAll, depth+1, maxDepth, prefix+" ", showEmojis, showOnlyFiles, showOnlyFolders, showCountFiles)
}
} else {
fmt.Printf("%s%sโโโ%s\n", details, prefix, fileName)
if f.IsDir() {
readFolderRecursive(showDetails, filePath, showAll, depth+1, maxDepth, prefix+"โ ", showEmojis, showOnlyFiles, showOnlyFolders, showCountFiles)
}
}
} else {
if i == len(files)-1 {
fmt.Printf("%s%sโโโ%s\n", details, prefix, fileName)
if f.IsDir() {
readFolderRecursive(showDetails, filePath, showAll, depth+1, maxDepth, prefix+" ", showEmojis, showOnlyFiles, showOnlyFolders, showCountFiles)
}
} else {
fmt.Printf("%s%sโโโ%s\n", details, prefix, fileName)
if f.IsDir() {
readFolderRecursive(showDetails, filePath, showAll, depth+1, maxDepth, prefix+"โ ", showEmojis, showOnlyFiles, showOnlyFolders, showCountFiles)
}
}
}
}
}
func ListFiles(showDetails bool, path string, showAll bool, showEmojis bool, showOnlyFiles bool, showOnlyFolders bool, showCountFiles bool) {
files, err := os.ReadDir(path)
if err != nil {
fmt.Printf("Error reading folder: %s : %v\n", path, err)
return
}
var maxNameLen int
for _, f := range files {
if !showAll && strings.HasPrefix(f.Name(), ".") {
continue
}
fileName := f.Name()
if showOnlyFolders && !f.IsDir() {
continue
}
if showOnlyFiles && f.IsDir() {
continue
}
if f.IsDir() {
numFiles := countFiles(filepath.Join(path, fileName))
if showCountFiles && numFiles > 0 {
fileName = fmt.Sprintf("[%d]", numFiles) + fileName
}
if showEmojis {
fileName = "๐" + fileName
} else {
fileName = fileName + "/"
}
} else if showEmojis {
fileName = "๐" + fileName
}
if len(fileName) > maxNameLen {
maxNameLen = len(fileName)
}
}
colWidth := maxNameLen + 2
numCols := 80 / colWidth
if numCols == 0 {
numCols = 1
}
var details []string
for _, f := range files {
if !showAll && strings.HasPrefix(f.Name(), ".") {
continue
}
fileName := f.Name()
filePath := filepath.Join(path, fileName)
if showOnlyFolders && !f.IsDir() {
continue
}
if showOnlyFiles && f.IsDir() {
continue
}
if f.IsDir() {
numFiles := countFiles(filePath)
if showCountFiles && numFiles > 0 {
fileName = fmt.Sprintf("[%d]", numFiles) + fileName
}
if showEmojis {
fileName = "๐" + fileName
} else {
fileName = fileName + "/"
}
} else if showEmojis {
fileName = "๐" + fileName
}
if showDetails {
details = append(details, fmt.Sprintf("%s %s", getDetails(filePath), fileName))
} else {
details = append(details, fileName)
}
}
for i := 0; i < len(details); i += numCols {
end := i + numCols
if end > len(details) {
end = len(details)
}
fmt.Println(strings.Join(details[i:end], strings.Repeat(" ", colWidth)))
}
}
func getDetails(filePath string) string {
info, err := os.Stat(filePath)
if err != nil {
return "Error getting details"
}
sys := info.Sys().(*syscall.Stat_t)
uid := sys.Uid
u, err := user.LookupId(fmt.Sprint(uid))
if err != nil {
return "Error getting user"
}
perm := info.Mode().Perm()
modTime := info.ModTime().Format(time.RFC3339)
return fmt.Sprintf("%s %s %s", perm, u.Username, modTime)
}
func main() {
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage: %s [OPTIONS] [PATH]\n", os.Args[0])
flag.PrintDefaults()
}
path := "."
showAll := flag.Bool("a", false, "Show all files and folders, including those beginning with '.'.")
maxDepth := flag.Int("depth", 0, "Maximum depth for folder traversal. Use -1 for unlimited depth.")
showDetails := flag.Bool("details", false, "Show file details (permissions, creator, date).")
showTree := flag.Bool("tree", false, "Show files and folders in a tree view.")
showEmojis := flag.Bool("emoji", false, "Show emojis for files and folders.")
showOnlyFiles := flag.Bool("f", false, "Show only files.")
showOnlyFolders := flag.Bool("d", false, "Show only folders.")
showCountFiles := flag.Bool("n", false, "Show count of files in folder names. ( [n]Desktop/ [n]๐Desktop )")
help := flag.Bool("h", false, "Show this help message")
flag.Parse()
if flag.NArg() > 0 {
path = flag.Arg(0)
}
if *help {
flag.Usage()
return
}
if *showTree {
numFiles := countFiles(path)
if *showEmojis {
if *showCountFiles && numFiles > 0 {
fmt.Printf("[%d]๐%s\n", numFiles, path)
} else {
fmt.Printf("๐%s\n", path)
}
} else {
if *showCountFiles && numFiles > 0 {
fmt.Printf("[%d]%s/\n", numFiles, path)
} else {
fmt.Printf("%s/\n", path)
}
}
ReadFolder(*showDetails, path, *showAll, *maxDepth, *showEmojis, *showOnlyFiles, *showOnlyFolders, *showCountFiles)
} else {
ListFiles(*showDetails, path, *showAll, *showEmojis, *showOnlyFiles, *showOnlyFolders, *showCountFiles)
}
}