Skip to content

Commit f915b67

Browse files
committed
fixed filecount when filtering by line
1 parent b6e37fc commit f915b67

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

src/LineCount.cs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using LineCount.Errors;
55
using LineCount.Logging;
66
using LineCount.Result;
7-
using System.Threading.Tasks;
87

98
namespace LineCount;
109

@@ -38,7 +37,7 @@ static async Task<ReportResult> GetLineCount(string path, LineCountData data, Pa
3837

3938
if(!attributes.HasFlag(FileAttributes.Directory))
4039
{
41-
return await GetSingleFileLineCount(path, data, cancellationToken).ConfigureAwait(false);
40+
return await GetSingleFileLineCountReport(path, data, cancellationToken).ConfigureAwait(false);
4241
}
4342

4443
var filesReportResult = await CountInFiles(path, data, excludeFilePatterns, cancellationToken).ConfigureAwait(false);
@@ -211,6 +210,7 @@ static async Task<ReportResult> CountInFiles(string path, LineCountData data, Pa
211210
}
212211

213212
int rootLineCount = 0;
213+
int fileCount = 0;
214214
int index = 0;
215215

216216
await foreach (var result in Task.WhenEach(filetasks))
@@ -234,10 +234,16 @@ static async Task<ReportResult> CountInFiles(string path, LineCountData data, Pa
234234
}
235235

236236
rootLineCount += lines;
237+
238+
if(lines > 0)
239+
{
240+
fileCount++;
241+
}
242+
237243
index++;
238244
}
239245

240-
return new LineCountReport(rootLineCount, index);
246+
return new LineCountReport(rootLineCount, fileCount);
241247
}
242248

243249
static IEnumerable<string> GetFilterFilePaths(string path, LineCountData data)
@@ -259,7 +265,7 @@ static Task<LineCountReport> GetSingleFileLineCountReport(string path, LineCount
259265
FilterType.FilteredExcept => GetFilteredFileLineCount(path, line => !data.ExcludeLineFilter!.IsMatch(line), cancellationToken),
260266
FilterType.FilteredBoth => GetFilteredFileLineCount(path, line => data.LineFilter!.IsMatch(line) && !data.ExcludeLineFilter!.IsMatch(line), cancellationToken),
261267
_ => throw new InvalidOperationException($"CountType.{data.FilterType} not recognized"),
262-
}).ContinueWith(task => new LineCountReport(task.Result), cancellationToken, TaskContinuationOptions.NotOnCanceled, TaskScheduler.Current);
268+
}).ContinueWith(task => LineCountReport.FromLines(task.Result), cancellationToken, TaskContinuationOptions.NotOnCanceled, TaskScheduler.Current);
263269
}
264270

265271
static async Task<ReportResult> GetSingleFileLineCount(string path, LineCountData data, CancellationToken cancellationToken = default)

src/LineCountReport.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
namespace LineCount;
22

3-
public record LineCountReport(int Lines, int Files = 1)
3+
public record LineCountReport(int Lines, int Files)
44
{
55
public static LineCountReport operator +(LineCountReport left, LineCountReport right)
66
{
77
return new LineCountReport(left.Lines + right.Lines, left.Files + right.Files);
88
}
9+
10+
public static LineCountReport FromLines(int lines)
11+
{
12+
return new LineCountReport(lines, lines > 0 ? 1 : 0);
13+
}
914
}

0 commit comments

Comments
 (0)