-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
236 lines (206 loc) · 6.23 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
package main
import (
"bufio"
"flag"
"os"
"path/filepath"
"sort"
"strings"
"sync"
"time"
color "github.com/fatih/color"
cmap "github.com/orcaman/concurrent-map/v2"
)
var options struct {
dir string
version bool
help bool
hidden bool
top uint
noColor bool
}
var workers sync.WaitGroup
var lines = cmap.New[int]()
var notAllowedExtensions = map[string]bool{
".exe": true, ".dll": true, ".so": true, ".dylib": true,
".zip": true, ".tar": true, ".gz": true, ".bz2": true, ".xz": true,
".jpg": true, ".jpeg": true, ".png": true, ".gif": true, ".bmp": true, ".webp": true, ".svg": true, ".ico": true,
".mp3": true, ".wav": true, ".flac": true, ".ogg": true, ".aac": true,
".mp4": true, ".mkv": true, ".avi": true, ".mov": true, ".wmv": true,
".pdf": true, ".doc": true, ".docx": true, ".xls": true, ".xlsx": true,
".icns": true, ".ttf": true, ".otf": true, ".woff": true, ".woff2": true,
".eot": true, ".svgz": true, ".uasset": true, ".plist": true,
".url": true, ".pbxproj": true, ".sln": true,
".vcxproj": true, ".csproj": true, ".vcproj": true, ".tlog": true,
".tmp": true, ".filters": true, ".idb": true, ".lock": true, ".rc": true,
".sqlite": true, ".gdb": true, ".node": true, ".rmeta": true,
".rlib": true, ".mcmeta": true, ".iml": true, ".map": true, ".natvis": true,
".d": true, ".dat_old": true, ".storyboard": true, ".ilk": true, ".ppt": true,
".pptx": true, ".odt": true, ".ods": true, ".odp": true, ".odg": true, ".mca": true,
".psd": true, ".bin": true, ".jar": true, ".pdb": true, ".dox": true, ".db": true,
".schem": true, ".lnk": true, ".mod": true, ".lib": true, ".o": true, ".obj": true,
".a": true, ".class": true, ".pyc": true, ".pyo": true, ".whl": true, ".log": true,
".in": true, "idb": true, ".dat": true, ".TAG": true, ".repositories": true, ".MF": true,
}
var notAllowedDirs = map[string]bool{
"node_modules": true, "vendor": true, ".git": true, "target": true,
}
func countNonBlankLines(path string) int {
file, err := os.Open(path)
if err != nil {
c := color.New(color.FgRed)
c.Println(err)
return 0
}
defer file.Close()
scanner := bufio.NewScanner(file)
buffer := make([]byte, 0, 64*1024)
scanner.Buffer(buffer, 1024*1024)
line_counter := 0
for scanner.Scan() {
line := scanner.Text()
trimmed := strings.TrimSpace(line)
// Skip empty lines
if len(strings.TrimSpace(line)) > 0 {
line_counter++
}
// Skip comments
if strings.HasPrefix(trimmed, "//") || strings.HasPrefix(trimmed, "/*") || strings.HasPrefix(trimmed, "*") || strings.HasSuffix(trimmed, "*/") || strings.HasPrefix(trimmed, "#") {
// Skip comments
_ = trimmed
continue
}
}
if err := scanner.Err(); err != nil {
c := color.New(color.FgRed)
c.Printf("Failed to count lines %s: %s\n", path, err)
}
return line_counter
}
func fastLineCounter(path string) {
extension := filepath.Ext(path)
workers.Add(1)
go func() {
defer workers.Done()
countedLines := countNonBlankLines(path)
if val, ok := lines.Get(extension); ok {
lines.Set(extension, val+countedLines)
} else {
lines.Set(extension, countedLines)
}
}()
}
func needToAnalyze(path string) bool {
// Skip hidden files
if !options.hidden && filepath.Base(path)[0] == '.' {
return false
}
extension := filepath.Ext(path)
// Skip files without extension
if len(extension) == 0 {
return false
}
// Skip not allowed extensions
if _, ok := notAllowedExtensions[extension]; ok {
return false
}
return true
}
func walkDir(dir string) {
defer workers.Done()
visit := func(path string, f os.FileInfo, err error) error {
if f.IsDir() && path != dir {
dirname := filepath.Base(path)
// Skip hidden directory
if !options.hidden && dirname[0] == '.' {
return filepath.SkipDir
}
// Skip node_modules, vendor, .git, target directories
if _, ok := notAllowedDirs[dirname]; ok {
return filepath.SkipDir
}
// Walk the directory
workers.Add(1)
go walkDir(path)
return filepath.SkipDir
}
if f.Mode().IsRegular() {
if needToAnalyze(path) {
fastLineCounter(path)
}
}
return nil
}
filepath.Walk(dir, visit)
}
func main() {
// Parse the command line flags
flag.StringVar(&options.dir, "dir", ".", "The directory to analyze")
flag.BoolVar(&options.version, "version", false, "Print the version and exit")
flag.BoolVar(&options.help, "help", false, "Print the help message and exit")
flag.BoolVar(&options.hidden, "hidden", false, "Allows to analize hidden files")
flag.UintVar(&options.top, "top", 0, "Print the top N files")
flag.BoolVar(&options.noColor, "no-color", false, "Disable color output")
flag.Parse()
// If the no-color flag is set, disable color output
if options.noColor {
color.NoColor = true
}
// If the version flag is set, print the version and exit
if options.version {
c := color.New(color.FgGreen)
c.Println("Lines version 1.0.1 created by @Moderrek")
return
}
// If the help flag is set, print the help message and exit
if options.help {
c := color.New(color.FgYellow)
c.Println("Usage: lines [options]")
flag.PrintDefaults()
return
}
if _, err := os.Stat(options.dir); os.IsNotExist(err) {
c := color.New(color.FgRed)
c.Printf("Directory %s does not exist\n", options.dir)
return
}
// Get current time. Will be used to calculate the time taken to analyze files
startTime := time.Now()
c := color.New(color.FgYellow)
c.Printf("Analyzing.. %s\n\n", options.dir)
// Start the analysis
workers.Add(1)
walkDir(options.dir)
workers.Wait()
// Convert lines to a regular map
lineMap := make(map[string]int)
for _, key := range lines.Keys() {
lineMap[key], _ = lines.Get(key)
}
// Sort the map by value
sortedKeys := make([]string, 0, len(lineMap))
for key := range lineMap {
sortedKeys = append(sortedKeys, key)
}
sort.Slice(sortedKeys, func(i, j int) bool {
return lineMap[sortedKeys[i]] > lineMap[sortedKeys[j]]
})
// Print the top N extensions
counter := uint(0)
for _, key := range sortedKeys {
if options.top > 0 {
if counter >= options.top {
break
}
}
counter++
var lines int = lineMap[key]
if lines == 0 {
continue
}
c := color.New(color.Bold)
c.Printf("%d. %s | Lines of code: %d\n", counter, key, lines)
}
success := color.New(color.FgGreen)
success.Printf("\nTime taken: %v to analyze files\n", time.Since(startTime))
}