-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
566 lines (535 loc) · 14.9 KB
/
main.go
File metadata and controls
566 lines (535 loc) · 14.9 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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
package main
import (
"bufio"
_ "embed"
"encoding/json"
"flag"
"fmt"
"io/fs"
"log"
"math"
"os"
"path/filepath"
"sort"
"strings"
)
//go:embed languages.json
var embeddedConfig []byte
// Types and Helper Functions
type Config struct {
Extensions []string `json:"extensions"`
Exclusions []string `json:"exclusions"`
}
type FileData struct {
Path string
LineCount int
}
type ClusterSummary struct {
Cluster int
Count int
Sum float64
Min float64
Max float64
Avg float64
}
func fileExists(path string) bool {
_, err := os.Stat(path)
return err == nil
}
func loadConfigFromBytes(data []byte) (*Config, error) {
var config Config
err := json.Unmarshal(data, &config)
return &config, err
}
func loadConfig(path string) (*Config, error) {
// If a custom config path is provided and it exists, use it
if path != "languages.json" && fileExists(path) {
f, err := os.Open(path)
if err != nil {
return nil, fmt.Errorf("failed to open custom config file %s: %w", path, err)
}
defer f.Close()
var config Config
err = json.NewDecoder(f).Decode(&config)
if err != nil {
return nil, fmt.Errorf("failed to parse custom config file %s: %w", path, err)
}
return &config, nil
}
// Try loading from the default languages.json file
if fileExists("languages.json") {
f, err := os.Open("languages.json")
if err != nil {
return nil, fmt.Errorf("failed to open default config file: %w", err)
}
defer f.Close()
var config Config
err = json.NewDecoder(f).Decode(&config)
if err != nil {
return nil, fmt.Errorf("failed to parse default config file: %w", err)
}
return &config, nil
}
// Fall back to embedded configuration
config, err := loadConfigFromBytes(embeddedConfig)
if err != nil {
return nil, fmt.Errorf("failed to load embedded config: %w", err)
}
return config, nil
}
func countLines(filePath string) (int, error) {
f, err := os.Open(filePath)
if err != nil {
return 0, err
}
defer f.Close()
scanner := bufio.NewScanner(f)
count := 0
for scanner.Scan() {
count++
}
return count, scanner.Err()
}
func computeStats(sizes []int) (avg float64, median float64, stdHigh float64, stdLow float64) {
if len(sizes) == 0 {
return 0, 0, 0, 0
}
// Compute average and median
var sum int
for _, s := range sizes {
sum += s
}
avg = float64(sum) / float64(len(sizes))
// Sorting for median
sorted := make([]int, len(sizes))
copy(sorted, sizes)
sort.Ints(sorted)
n := len(sorted)
if n%2 == 0 {
median = float64(sorted[n/2-1]+sorted[n/2]) / 2.0
} else {
median = float64(sorted[n/2])
}
// Compute standard deviations separately for files above and below average
var varianceHighSum float64
var countHigh int
var varianceLowSum float64
var countLow int
for _, s := range sizes {
diff := float64(s) - avg
if diff >= 0 {
varianceHighSum += diff * diff
countHigh++
} else {
varianceLowSum += diff * diff
countLow++
}
}
if countHigh > 0 {
stdHigh = math.Sqrt(varianceHighSum / float64(countHigh))
}
if countLow > 0 {
stdLow = math.Sqrt(varianceLowSum / float64(countLow))
}
return
}
// runKMeans performs k-means clustering on float64 data with fixed iterations.
func runKMeans(data []float64, k int) (assignments []int, centroids []float64) {
n := len(data)
// Initialize centroids using min, median and max of sorted data.
sortedData := make([]float64, n)
copy(sortedData, data)
sort.Float64s(sortedData)
centroids = []float64{sortedData[0], sortedData[n/2], sortedData[n-1]}
assignments = make([]int, n)
for iter := 0; iter < 10; iter++ {
changed := false
for i, x := range data {
best := 0
bestDist := math.Abs(x - centroids[0])
for j := 1; j < k; j++ {
d := math.Abs(x - centroids[j])
if d < bestDist {
bestDist = d
best = j
}
}
if assignments[i] != best {
assignments[i] = best
changed = true
}
}
newCentroids := make([]float64, k)
counts := make([]int, k)
for i, cluster := range assignments {
newCentroids[cluster] += data[i]
counts[cluster]++
}
for j := 0; j < k; j++ {
if counts[j] > 0 {
newCentroids[j] /= float64(counts[j])
} else {
newCentroids[j] = centroids[j]
}
}
centroids = newCentroids
if !changed {
break
}
}
return
}
// computeClusterSummaries computes summaries from the data and cluster assignments.
func computeClusterSummaries(data []float64, assignments []int, k int) []ClusterSummary {
summaries := make([]ClusterSummary, k)
for j := 0; j < k; j++ {
summaries[j].Min = 1e9
summaries[j].Max = -1
summaries[j].Cluster = j
}
for i, cluster := range assignments {
x := data[i]
summaries[cluster].Count++
summaries[cluster].Sum += x
if x < summaries[cluster].Min {
summaries[cluster].Min = x
}
if x > summaries[cluster].Max {
summaries[cluster].Max = x
}
}
for j := 0; j < k; j++ {
if summaries[j].Count > 0 {
summaries[j].Avg = summaries[j].Sum / float64(summaries[j].Count)
}
}
return summaries
}
// labelClusters returns a map from cluster index to label based on average value.
func labelClusters(summaries []ClusterSummary) map[int]string {
k := len(summaries)
type idxAvg struct {
Index int
Avg float64
}
idxs := make([]idxAvg, k)
for j := 0; j < k; j++ {
idxs[j] = idxAvg{Index: j, Avg: summaries[j].Avg}
}
sort.Slice(idxs, func(i, j int) bool { return idxs[i].Avg < idxs[j].Avg })
labels := map[int]string{
idxs[0].Index: "Small",
idxs[1].Index: "Medium",
idxs[2].Index: "Large",
}
return labels
}
// main Function
func main() {
// Define flags
detailed := flag.Bool("l", false, "detailed output")
sorted := flag.Bool("s", false, "detailed sorted output (smallest to largest)")
histogram := flag.Bool("h", false, "detailed histogram output (graphical)")
allFiles := flag.Bool("a", false, "all files, not just source code")
helpFlag := flag.Bool("?", false, "print help")
configPath := flag.String("config", "languages.json", "config file with file extensions")
jsonOutput := flag.Bool("j", false, "save output as JSON to file")
includeLang := flag.String("i", "", "include only this language type (extension)")
excludeLang := flag.String("e", "", "exclude this language type (extension)")
kFlag := flag.Int("k", 0, "exclude largest n files") // NEW - added new flag
flag.Parse()
// If help flag is provided, print usage and exit
if *helpFlag {
flag.Usage()
return
}
// Get the folder argument
if flag.NArg() < 1 {
log.Fatal("Please provide a folder name")
}
root := flag.Arg(0)
// Remove allowed map logic and load config only when needed
var config *Config
if *includeLang == "" {
var err error
config, err = loadConfig(*configPath)
if err != nil {
log.Fatalf("Unable to load config: %v", err)
}
}
var files []FileData
if err := filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if d.IsDir() {
return nil
}
if !*allFiles {
lowerName := strings.ToLower(d.Name())
// Check exclude flag if provided.
if *excludeLang != "" {
exc := strings.ToLower(*excludeLang)
if !strings.HasPrefix(exc, ".") {
exc = "." + exc
}
if strings.HasSuffix(lowerName, exc) {
return nil
}
}
// Check include flag if provided.
if *includeLang != "" {
inc := strings.ToLower(*includeLang)
if !strings.HasPrefix(inc, ".") {
inc = "." + inc
}
if !strings.HasSuffix(lowerName, inc) {
return nil
}
} else {
// First, exclude files with any exclusion extension
for _, exc := range config.Exclusions {
if strings.HasSuffix(lowerName, strings.ToLower(exc)) {
return nil
}
}
// Then, check if file name matches any allowed extension
allowFlag := false
for _, ext := range config.Extensions {
if strings.HasSuffix(lowerName, strings.ToLower(ext)) {
allowFlag = true
break
}
}
if !allowFlag {
return nil
}
}
}
lines, err := countLines(path)
if err != nil {
log.Printf("Error reading %s: %v", path, err)
return nil
}
files = append(files, FileData{Path: path, LineCount: lines})
return nil
}); err != nil {
log.Fatalf("Error walking the path %q: %v", root, err)
}
// Exclude the largest n files if -k is provided
var excludedFiles []FileData
if *kFlag > 0 {
// Sort files smallest to largest so that largest files are at the end
sort.Slice(files, func(i, j int) bool { return files[i].LineCount < files[j].LineCount })
if *kFlag >= len(files) {
fmt.Printf("Excluding %d file(s) (largest files).\n", len(files))
excludedFiles = files
files = []FileData{}
} else {
n := *kFlag
excludedFiles = files[len(files)-n:]
files = files[:len(files)-n]
fmt.Printf("Excluding %d largest file(s):\n", len(excludedFiles))
for _, fd := range excludedFiles {
// Print each excluded file with its line count.
fmt.Printf(" - %s: %d lines\n", fd.Path, fd.LineCount)
}
fmt.Println()
}
}
if len(files) == 0 {
fmt.Println("No files found")
return
}
// Compute overall stats
sizes := make([]int, len(files))
smallest := files[0]
largest := files[0]
for i, fd := range files {
sizes[i] = fd.LineCount
if fd.LineCount < smallest.LineCount {
smallest = fd
}
if fd.LineCount > largest.LineCount {
largest = fd
}
}
avg, median, stdHigh, stdLow := computeStats(sizes)
// Compute sum total lines (only for non-all-files mode)
sumTotal := 0
for _, count := range sizes {
sumTotal += count
}
// Compute clusters once using k-means if possible
var clusterResults []struct {
Label string `json:"label"`
Count int `json:"count"`
Percentage float64 `json:"percentage"`
Avg float64 `json:"avg"`
Range [2]float64 `json:"range"`
}
if len(files) >= 3 {
k := 3
n := len(files)
data := make([]float64, n)
for i, fd := range files {
data[i] = float64(fd.LineCount)
}
assignments, _ := runKMeans(data, k)
summaries := computeClusterSummaries(data, assignments, k)
labels := labelClusters(summaries)
clusterResults = make([]struct {
Label string `json:"label"`
Count int `json:"count"`
Percentage float64 `json:"percentage"`
Avg float64 `json:"avg"`
Range [2]float64 `json:"range"`
}, k)
for j := 0; j < k; j++ {
avgVal := 0.0
if summaries[j].Count > 0 {
avgVal = summaries[j].Sum / float64(summaries[j].Count)
}
perc := 100.0 * float64(summaries[j].Count) / float64(n)
clusterResults[j] = struct {
Label string `json:"label"`
Count int `json:"count"`
Percentage float64 `json:"percentage"`
Avg float64 `json:"avg"`
Range [2]float64 `json:"range"`
}{
Label: labels[j],
Count: summaries[j].Count,
Percentage: perc,
Avg: math.Round(avgVal),
Range: [2]float64{math.Round(summaries[j].Min), math.Round(summaries[j].Max)},
}
}
}
// JSON Output section
if *jsonOutput {
output := struct {
TotalFiles int `json:"total_files"`
Average float64 `json:"average"`
Median float64 `json:"median"`
StdDevHigh float64 `json:"std_dev_high"`
StdDevLow float64 `json:"std_dev_low"`
TotalSum *int `json:"total_sum,omitempty"`
SmallestFile FileData `json:"smallest_file"`
LargestFile FileData `json:"largest_file"`
Files []FileData `json:"files,omitempty"`
Clusters interface{} `json:"clusters,omitempty"`
}{
TotalFiles: len(files),
Average: avg,
Median: median,
StdDevHigh: stdHigh,
StdDevLow: stdLow,
SmallestFile: smallest,
LargestFile: largest,
TotalSum: func() *int {
if !*allFiles {
return &sumTotal
}
return nil
}(),
Files: func() []FileData {
if *detailed || *sorted {
jsonFiles := make([]FileData, len(files))
copy(jsonFiles, files)
if *sorted {
sort.Slice(jsonFiles, func(i, j int) bool {
return jsonFiles[i].LineCount < jsonFiles[j].LineCount
})
}
return jsonFiles
}
return nil
}(),
Clusters: clusterResults,
}
folderName := filepath.Base(root)
jsonFileName := folderName + ".codesiz.json"
f, err := os.Create(jsonFileName)
if err != nil {
log.Fatalf("Unable to create JSON file: %v", err)
}
defer f.Close()
encoder := json.NewEncoder(f)
encoder.SetIndent("", " ")
if err := encoder.Encode(output); err != nil {
log.Fatalf("Error encoding JSON: %v", err)
}
fmt.Printf("JSON output saved to %s\n", jsonFileName)
return
}
// Display results (updated to round all line counts)
fmt.Printf("Total files analyzed: %d\n", len(files))
fmt.Printf("Average: %.0f lines\n", math.Round(avg))
fmt.Printf("Median: %.0f lines\n", math.Round(median))
fmt.Printf("Standard deviation (high): %.0f lines\n", math.Round(stdHigh))
fmt.Printf("Standard deviation (low): %.0f lines\n", math.Round(stdLow))
if !*allFiles {
fmt.Printf("Total sum: %d lines\n", sumTotal)
}
fmt.Printf("Smallest file: %s (%d lines)\n", smallest.Path, smallest.LineCount)
fmt.Printf("Largest file: %s (%d lines)\n", largest.Path, largest.LineCount)
// Compute file clusters using k-means clustering (k=3) on file line counts
if len(files) >= 3 {
fmt.Println("\nFile clusters (k-means clustering, k=3):")
for j := 0; j < 3; j++ {
perc := 100.0 * float64(clusterResults[j].Count) / float64(len(files))
fmt.Printf(" %s: %d files (%.2f%%), Avg = %.0f lines, Range = [%.0f, %.0f] lines\n",
clusterResults[j].Label, clusterResults[j].Count, perc,
clusterResults[j].Avg, clusterResults[j].Range[0], clusterResults[j].Range[1])
}
} else {
fmt.Println("\nNot enough files for clustering.")
}
// Detailed file listing (histogram/sorted/detailed)
if *histogram {
const barWidth = 50
maxLine := largest.LineCount
var outputFiles []FileData
if *sorted {
outputFiles = make([]FileData, len(files))
copy(outputFiles, files)
sort.Slice(outputFiles, func(i, j int) bool {
return outputFiles[i].LineCount < outputFiles[j].LineCount
})
} else {
outputFiles = files
}
// Compute max width for file paths for alignment
maxPathLen := 0
for _, fd := range outputFiles {
if len(fd.Path) > maxPathLen {
maxPathLen = len(fd.Path)
}
}
fmt.Println("\nDetailed file histogram:")
for _, fd := range outputFiles {
barLen := 0
if maxLine > 0 {
barLen = int((float64(fd.LineCount) / float64(maxLine)) * barWidth)
}
bar := strings.Repeat("█", barLen)
fmt.Printf("%-*s: %s\n", maxPathLen, fd.Path, bar)
}
} else if *sorted {
sortedFiles := make([]FileData, len(files))
copy(sortedFiles, files)
sort.Slice(sortedFiles, func(i, j int) bool {
return sortedFiles[i].LineCount < sortedFiles[j].LineCount
})
fmt.Println("\nDetailed file list (sorted smallest to largest):")
for _, fd := range sortedFiles {
fmt.Printf("%s: %d lines\n", fd.Path, fd.LineCount)
}
} else if *detailed {
fmt.Println("\nDetailed file list:")
for _, fd := range files {
fmt.Printf("%s: %d lines\n", fd.Path, fd.LineCount)
}
}
}