/
wc.go
187 lines (173 loc) · 3.5 KB
/
wc.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
package u
import (
"fmt"
"os"
"path/filepath"
"sort"
"strings"
)
// LineCount describes line count for a file
type LineCount struct {
Name string
Ext string
LineCount int
}
// LineStats gathers line count info for files
type LineStats struct {
FileToCount map[string]*LineCount
}
// NewLineStats returns new LineStats
func NewLineStats() *LineStats {
return &LineStats{
FileToCount: map[string]*LineCount{},
}
}
func LineStatsPerExt(fileToCount map[string]*LineCount) []*LineCount {
extToCount := map[string]*LineCount{}
for _, wc := range fileToCount {
ext := wc.Ext
extWc := extToCount[ext]
if extWc == nil {
extWc = &LineCount{
Ext: ext,
}
extToCount[ext] = extWc
}
extWc.LineCount += wc.LineCount
}
var res []*LineCount
for _, wc := range extToCount {
res = append(res, wc)
}
sort.Slice(res, func(i, j int) bool {
wc1 := res[i]
wc2 := res[j]
return wc1.LineCount < wc2.LineCount
})
return res
}
type FilterFunc func(string) bool
func MakeAllowedFileFilterForExts(exts ...string) FilterFunc {
for i, ext := range exts {
exts[i] = strings.ToLower(ext)
}
return func(path string) bool {
fext := strings.ToLower(filepath.Ext(path))
for _, ext := range exts {
if ext == fext {
return true
}
}
return false
}
}
func MakeExcludeDirsFilter(dirs ...string) FilterFunc {
return func(path string) bool {
// path starts as a file path
// we only compare directory names
path = filepath.Dir(path)
for len(path) > 0 {
if path == "." {
return true
}
name := filepath.Base(path)
for _, dir := range dirs {
if name == dir {
return false
}
}
path = filepath.Dir(path)
}
return true
}
}
func MakeFilterOr(filters ...FilterFunc) FilterFunc {
return func(name string) bool {
for _, f := range filters {
if f(name) {
return true
}
}
return false
}
}
func MakeFilterAnd(filters ...FilterFunc) FilterFunc {
return func(name string) bool {
for _, f := range filters {
if !f(name) {
return false
}
}
return true
}
}
// FileLineCount returns number of lines in a file
func FileLineCount(path string) (int, error) {
d, err := os.ReadFile(path)
if err != nil {
return 0, err
}
if len(d) == 0 {
return 0, nil
}
d = NormalizeNewlines(d)
nLines := 1
n := len(d)
for i := 0; i < n; i++ {
if d[i] == 10 {
nLines++
}
}
return nLines, nil
}
func (s *LineStats) CalcInDir(dir string, allowedFileFilter func(name string) bool, recur bool) error {
files, err := os.ReadDir(dir)
if err != nil {
return err
}
for _, fi := range files {
name := fi.Name()
path := filepath.Join(dir, name)
if fi.IsDir() {
if recur {
s.CalcInDir(path, allowedFileFilter, recur)
}
continue
}
if !fi.Type().IsRegular() {
continue
}
if !allowedFileFilter(path) {
continue
}
lineCount, err := FileLineCount(path)
if err != nil {
return err
}
s.FileToCount[path] = &LineCount{
Name: fi.Name(),
Ext: strings.ToLower(filepath.Ext(name)),
LineCount: lineCount,
}
}
return nil
}
func PrintLineStats(stats *LineStats) {
var files []string
for k := range stats.FileToCount {
files = append(files, k)
}
sort.Strings(files)
total := 0
for _, f := range files {
wc := stats.FileToCount[f]
fmt.Printf("% 6d %s\n", wc.LineCount, f)
total += wc.LineCount
}
fmt.Printf("\nPer extension:\n")
wcPerExt := LineStatsPerExt(stats.FileToCount)
for _, wc := range wcPerExt {
fmt.Printf("%d %s\n", wc.LineCount, wc.Ext)
}
fmt.Printf("\ntotal: %d\n", total)
}