Skip to content

Commit 754e1cb

Browse files
committed
Feature: Modified Run method to use ProgressToken and process files in parallel.
1 parent 608a06b commit 754e1cb

File tree

2 files changed

+27
-12
lines changed

2 files changed

+27
-12
lines changed

CodeIngest.Desktop/MainViewModel.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,12 @@
88
// about your modifications. Your contributions are valued!
99
//
1010
// THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND.
11+
12+
using System;
1113
using System.Linq;
1214
using System.Threading.Tasks;
1315
using CodeIngestLib;
16+
using CSharp.Core;
1417
using CSharp.Core.Extensions;
1518
using CSharp.Core.UI;
1619
using CSharp.Core.ViewModels;
@@ -166,9 +169,12 @@ public async Task RunIngest()
166169

167170
if (IncludeMarkdown)
168171
options.FilePatterns.Add("*.md");
172+
173+
var progress = new ProgressToken();
174+
progress.ProgressUpdated += (_, _) => Console.Write('.');
169175

170176
var ingester = new Ingester(options);
171-
var result = ingester.Run(selectedFolders, outputFile);
177+
var result = ingester.Run(selectedFolders, outputFile, progress);
172178
if (!result.HasValue)
173179
{
174180
m_dialogService.ShowMessage("Failed to generate code file.", "Please check your file permissions and try again.", MaterialIconKind.Error);

CodeIngestLib/Ingester.cs

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,12 @@ public Ingester(IngestOptions options)
3131
m_options = options;
3232
}
3333

34-
public (int FileCount, long OutputBytes)? Run(IEnumerable<DirectoryInfo> directories, FileInfo outputFile)
34+
public (int FileCount, long OutputBytes)? Run(IEnumerable<DirectoryInfo> directories, FileInfo outputFile, ProgressToken progress = null)
3535
{
3636
var didError = false;
3737
var sourceFiles = directories
3838
.Where(d => d.Exists)
39+
.AsParallel()
3940
.SelectMany(d => m_options.FilePatterns.SelectMany(p =>
4041
{
4142
try
@@ -50,15 +51,15 @@ public Ingester(IngestOptions options)
5051
}
5152
}))
5253
.Where(f => !ShouldSkipFile(f))
53-
.ToDictionary(o => o.FullName, o => File.ReadLines(o.FullName));
54+
.ToArray();
5455

5556
if (didError)
5657
{
5758
Logger.Instance.Error("Error collecting files.");
5859
return null;
5960
}
6061

61-
if (sourceFiles.Count == 0)
62+
if (sourceFiles.Length == 0)
6263
{
6364
Logger.Instance.Warn("No matching files found. Check your filters or directory paths.");
6465
return (0, 0);
@@ -71,28 +72,36 @@ public Ingester(IngestOptions options)
7172
writer.WriteLine("// CodeIngest - A CLI tool that merges and processes code files for GPT reviews.");
7273
writer.WriteLine("// Notes: Some code content may have been removed.");
7374

74-
foreach (var kvp in sourceFiles)
75+
for (var i = 0; i < sourceFiles.Length; i++)
7576
{
76-
var lines = kvp.Value.ToList();
77-
var padWidth = lines.Count.ToString().Length;
77+
var sourceFile = sourceFiles[i];
78+
if (progress != null)
79+
{
80+
if (progress.CancelRequested)
81+
break; // Caller requested cancellation.
82+
progress.Progress = (i + 1.0) / sourceFiles.Length;
83+
}
84+
85+
using var reader = new StreamReader(sourceFile.OpenRead(), Encoding.UTF8);
7886

79-
writer.WriteLine($"// File: {(m_options.UseFullPaths ? kvp.Key : Path.GetFileName(kvp.Key))}");
87+
writer.WriteLine($"// File: {(m_options.UseFullPaths ? sourceFile.FullName : sourceFile.Name)}");
8088

8189
var lineNumber = 1;
82-
foreach (var line in lines)
90+
string line;
91+
while ((line = reader.ReadLine()) != null)
8392
{
8493
if (ShouldIncludeSourceLine(line, m_options))
85-
writer.WriteLine($"{lineNumber.ToString().PadLeft(padWidth)}|{GetCodeLine(line).Trim()}");
94+
writer.WriteLine($"{lineNumber.ToString()}|{GetCodeLine(line).Trim()}");
8695

8796
lineNumber++;
8897
}
8998

9099
if (m_options.Verbose)
91-
Logger.Instance.Warn($"{kvp.Key} ({lines.Sum(o => o.Length):N0} characters -> {lines.Count:N0} lines)");
100+
Logger.Instance.Warn($"{sourceFile.FullName} processed ({lineNumber - 1:N0} lines)");
92101
}
93102
}
94103

95-
return (sourceFiles.Count, outputFile.Length);
104+
return (sourceFiles.Length, outputFile.Length);
96105
}
97106

98107
private static bool ShouldSkipFile(FileInfo f) =>

0 commit comments

Comments
 (0)