-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdir.go
More file actions
271 lines (233 loc) · 6.29 KB
/
dir.go
File metadata and controls
271 lines (233 loc) · 6.29 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
package wordcounter
import (
"os"
"path/filepath"
"runtime"
"strings"
"sync"
)
type DirCounter struct {
dirname string
ignoreList []string
fileCounters []*FileCounter
withTotal bool
pathDisplayMode string
}
func NewDirCounter(dirname string, ignores ...string) *DirCounter {
return NewDirCounterWithPathMode(dirname, PathDisplayAbsolute, ignores...)
}
func NewDirCounterWithPathMode(dirname string, pathDisplayMode string, ignores ...string) *DirCounter {
return &DirCounter{
ignoreList: ignores,
dirname: dirname,
fileCounters: []*FileCounter{},
withTotal: false,
pathDisplayMode: pathDisplayMode,
}
}
func (dc *DirCounter) EnableTotal() {
dc.withTotal = true
}
// GetFileCounters returns the slice of FileCounter instances.
// This provides access to individual file counting results.
func (dc *DirCounter) GetFileCounters() []*FileCounter {
return dc.fileCounters
}
// GetIgnoreList returns the current ignore patterns.
// This allows inspection of the configured ignore patterns.
func (dc *DirCounter) GetIgnoreList() []string {
return dc.ignoreList
}
func (dc *DirCounter) Count() error {
absPath := ToAbsolutePath(dc.dirname)
// First pass: collect all files to process
var filePaths []string
err := filepath.Walk(absPath, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
if dc.IsIgnored(path) {
return filepath.SkipDir
}
return nil
}
if !dc.IsIgnored(path) {
filePaths = append(filePaths, path)
}
return nil
})
if err != nil {
return err
}
// Second pass: process files concurrently with worker pool
return dc.processFilesConcurrently(filePaths)
}
// processFilesConcurrently processes files using a worker pool pattern while preserving order
func (dc *DirCounter) processFilesConcurrently(filePaths []string) error {
// Determine optimal number of workers
numWorkers := runtime.NumCPU()
if numWorkers < MinWorkers {
numWorkers = MinWorkers
}
if numWorkers > MaxWorkers {
numWorkers = MaxWorkers
}
if len(filePaths) < numWorkers {
numWorkers = len(filePaths)
}
// Create a job structure that includes index to preserve order
type job struct {
index int
filePath string
}
type result struct {
index int
fc *FileCounter
err error
}
// Create channels for work distribution and result collection
jobs := make(chan job, len(filePaths))
results := make(chan result, len(filePaths))
// Start workers
var wg sync.WaitGroup
for i := 0; i < numWorkers; i++ {
wg.Add(1)
go func() {
defer wg.Done()
for j := range jobs {
var originalPath string
if dc.pathDisplayMode == PathDisplayRelative {
// Calculate relative path from the directory being scanned
relPath, err := filepath.Rel(ToAbsolutePath(dc.dirname), j.filePath)
if err != nil {
originalPath = filepath.Base(j.filePath) // fallback to basename
} else {
originalPath = relPath
}
} else {
originalPath = j.filePath
}
fc := &FileCounter{
Counter: NewCounter(),
FileName: j.filePath,
originalPath: originalPath,
pathDisplayMode: dc.pathDisplayMode,
}
err := fc.Count()
results <- result{index: j.index, fc: fc, err: err}
}
}()
}
// Send jobs to workers
go func() {
defer close(jobs)
for i, filePath := range filePaths {
jobs <- job{index: i, filePath: filePath}
}
}()
// Wait for all workers to complete
go func() {
wg.Wait()
close(results)
}()
// Collect results and preserve order
resultMap := make(map[int]*FileCounter)
for i := 0; i < len(filePaths); i++ {
res := <-results
if res.err != nil {
return res.err
}
resultMap[res.index] = res.fc
}
// Build final slice in correct order
dc.fileCounters = make([]*FileCounter, len(filePaths))
for i := 0; i < len(filePaths); i++ {
dc.fileCounters[i] = resultMap[i]
}
return nil
}
func (dc *DirCounter) IsIgnored(filename string) bool {
for _, pattern := range dc.ignoreList {
if strings.HasPrefix(pattern, "/") {
if pattern[1:] == filename {
return true
}
} else {
match, err := filepath.Match(pattern, filepath.Base(filename))
if err != nil {
// Log the error but don't fail the entire operation
// Invalid patterns are treated as non-matching
continue
}
if match {
return true
}
}
}
return false
}
// IsIgnoredWithError checks if a file should be ignored and returns any pattern matching errors
func (dc *DirCounter) IsIgnoredWithError(filename string) (bool, error) {
for _, pattern := range dc.ignoreList {
if strings.HasPrefix(pattern, "/") {
if pattern[1:] == filename {
return true, nil
}
} else {
match, err := filepath.Match(pattern, filepath.Base(filename))
if err != nil {
return false, NewPatternMatchError(pattern, err)
}
if match {
return true, nil
}
}
}
return false, nil
}
// AddIgnorePattern adds a new ignore pattern (implements IgnoreChecker interface)
func (dc *DirCounter) AddIgnorePattern(pattern string) {
dc.ignoreList = append(dc.ignoreList, pattern)
}
// Ignore is deprecated, use AddIgnorePattern instead
func (dc *DirCounter) Ignore(pattern string) {
dc.AddIgnorePattern(pattern)
}
// GetHeader returns the header row (implements Counter interface)
func (dc *DirCounter) GetHeader() Row {
if len(dc.fileCounters) == 0 {
return Row{"File", "Lines", "ChineseChars", "NonChineseChars", "TotalChars"}
}
return dc.fileCounters[0].GetHeader()
}
func (dc *DirCounter) GetRows() []Row {
data := make([]Row, 0, len(dc.fileCounters))
for _, fc := range dc.fileCounters {
row := fc.GetRow()
data = append(data, row)
}
if dc.withTotal {
data = append(data, getTotal(dc.fileCounters))
}
return data
}
func (dc *DirCounter) GetHeaderAndRows() []Row {
data := make([]Row, 0, len(dc.fileCounters))
header := dc.fileCounters[0].GetHeader()
data = append(data, header)
data = append(data, dc.GetRows()...)
return data
}
func (dc *DirCounter) ExportCSV(filename ...string) (string, error) {
data := dc.GetHeaderAndRows()
return exportToCSV(data, filename...)
}
func (dc *DirCounter) ExportExcel(filename ...string) error {
data := dc.GetHeaderAndRows()
return exportToExcel(data, filename...)
}
func (dc *DirCounter) ExportTable() string {
data := dc.GetHeaderAndRows()
return exportToTable(data)
}