-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
267 lines (227 loc) · 6.52 KB
/
main.go
File metadata and controls
267 lines (227 loc) · 6.52 KB
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
260
261
262
263
264
265
266
267
package main
import (
"flag"
"fmt"
"os"
"path/filepath"
"strings"
"tview/icons"
"sort"
)
var (
depth int
ignore string
useColor bool
hideIcons bool
hideFileSize bool
showVersion bool
sortBy string
version string = "v0.1.0"
repoLink string = "https://github.com/sameer240704/tview"
)
func init() {
flag.IntVar(&depth, "depth", 0, "Max depth to traverse (-1 for infinite)")
flag.StringVar(&ignore, "ignore", "", "Comma-separated list of ignored directories")
flag.BoolVar(&useColor, "color", true, "Enable colored output")
flag.BoolVar(&hideIcons, "icons", false, "Hide folder and file icons")
flag.BoolVar(&hideFileSize, "size", false, "Display size of the corressponding file")
flag.BoolVar(&showVersion, "version", false, "Show tview version")
flag.StringVar(&sortBy, "sort", "", "Sort files in multiple orders")
}
func main() {
// Added windows support flag for printing help
for _, arg := range os.Args[1:] {
switch arg {
case "-?", "--help":
printHelp()
return
case "-v", "--version":
fmt.Printf("tview %s\n%s\n", version, repoLink)
return
}
}
flag.Parse()
root := "."
if flag.NArg() > 0 {
root = flag.Arg(0)
}
absRoot, err := filepath.Abs(root)
if err != nil {
fmt.Println("Error resolving path: ", err)
return
}
rootName := filepath.Base(absRoot)
rootIcon := " "
if !hideIcons {
rootIcon = ""
}
ignoreList := strings.Split(ignore, ",")
var rootDisplay string
if !hideIcons {
rootDisplay = fmt.Sprintf("%s", rootName)
} else {
rootDisplay = fmt.Sprintf("%s %s", rootIcon, rootName)
}
fmt.Printf("%s\n", colorize(rootDisplay, "cyan", true))
printTree(root, "", 0, ignoreList)
}
func shouldIgnore(path string, ignoreList []string) bool {
for _, i := range ignoreList {
if strings.TrimSpace(i) == filepath.Base(path) {
return true
}
}
return false
}
func colorize(text, color string, bold bool) string {
if !useColor {
return text
}
colors := map[string]string{
"reset": "\033[0m",
"bold": "\033[1m",
"cyan": "\033[36m",
"blue": "\033[34m",
"green": "\033[32m",
"yellow": "\033[33m",
"gray": "\033[90m",
}
colorCode := colors[color]
boldCode := ""
if bold {
boldCode = colors["bold"]
}
return boldCode + colorCode + text + colors["reset"]
}
func printTree(path, prefix string, level int, ignoreList []string) {
if depth != -1 && level > depth {
return
}
entries, err := os.ReadDir(path)
if err != nil {
fmt.Printf("%sError reading %s: %v\n", prefix, path, err)
return
}
// Filter out ignored entries
filteredEntries := make([]os.DirEntry, 0)
for _, entry := range entries {
if !shouldIgnore(entry.Name(), ignoreList) {
filteredEntries = append(filteredEntries, entry)
}
}
filteredEntries = sortEntries(filteredEntries, path, sortBy)
for i, entry := range filteredEntries {
isLast := i == len(filteredEntries)-1
// Curved connectors
var connector, newPrefix string
if isLast {
connector = "╰── "
newPrefix = prefix + " "
} else {
connector = "├── "
newPrefix = prefix + "│ "
}
// Color and styling
displayName := entry.Name()
var icon string
if hideIcons {
icon = icons.GetIcon(entry)
} else {
icon = ""
}
if entry.IsDir() {
displayName = colorize(displayName, "cyan", true)
} else {
displayName = colorize(displayName, "blue", false)
}
if hideFileSize {
if !entry.IsDir() {
info, err := entry.Info()
if err == nil {
size := formatSize(info.Size())
sizeStr := colorize(fmt.Sprintf(" (%s)", size), "gray", false)
displayName += sizeStr
}
}
}
if icon != "" {
fmt.Printf("%s%s %s %s\n",
colorize(prefix, "gray", false),
colorize(connector, "gray", false),
icon,
displayName)
} else {
fmt.Printf("%s%s %s\n",
colorize(prefix, "gray", false),
colorize(connector, "gray", false),
displayName)
}
if entry.IsDir() {
printTree(filepath.Join(path, entry.Name()), newPrefix, level+1, ignoreList)
}
}
}
func formatSize(size int64) string {
if size < 1024 {
return fmt.Sprintf("%dB", size)
} else if size < 1024*1024 {
return fmt.Sprintf("%.1fKB", float64(size)/1024)
} else if size < 1024*1024*1024 {
return fmt.Sprintf("%.1fMB", float64(size)/(1024*1024))
} else {
return fmt.Sprintf("%.1fGB", float64(size)/(1024*1024*1024))
}
}
// Helper function for sorting the files and folders
func sortEntries(entries []os.DirEntry, path string, sortBy string) []os.DirEntry {
if sortBy == "" {
return entries
}
parts := strings.Split(strings.ToLower(sortBy), ":")
field := parts[0]
order := "asc"
if len(parts) > 1 {
order = parts[1]
}
sort.SliceStable(entries, func(i, j int) bool {
a, b := entries[i], entries[j]
switch field {
case "size":
ai, _ := a.Info()
bi, _ := b.Info()
if ai == nil || bi == nil {
return a.Name() < b.Name()
}
if order == "desc" {
return ai.Size() > bi.Size()
}
return ai.Size() < bi.Size()
case "name":
if order == "desc" {
return strings.ToLower(a.Name()) > strings.ToLower(b.Name())
}
return strings.ToLower(a.Name()) < strings.ToLower(b.Name())
default:
return strings.ToLower(a.Name()) < strings.ToLower(b.Name())
}
})
return entries
}
func printHelp() {
fmt.Println(`
tview - A fast, simple, and elegant terminal tool to visualize your folder structure as a tree.
Usage:
tview [path] [options]
META OPTIONS
-?, --help show list of command-line options
-v, --version show tview version
DISPLAY OPTIONS
--depth int set maximum directory depth (-1 for unlimited traversal)
--ignore string comma-separated list of directories or files to exclude (e.g., node_modules,.git)
--color enable colored output in terminal (default: true)
--icons hide file and folder icons in the tree view
--size show file sizes next to each file
--sort string sort files by 'name' or 'size' (optionally add :asc or :desc)
e.g. --sort name:asc, --sort size:desc
`)
}