Skip to content

Commit

Permalink
add progress indicators
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidVeksler committed Nov 5, 2024
1 parent e003206 commit fdaea52
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 28 deletions.
65 changes: 38 additions & 27 deletions MyAppsContext.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
namespace CodeContext;

public class MyAppsContext
{
private static string _gitRepoRootPath;
public static string GitRepoRoot { get; private set; }

Check warning on line 4 in MyAppsContext.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest)

Non-nullable property 'GitRepoRoot' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.

Check warning on line 4 in MyAppsContext.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest)

Non-nullable property 'GitRepoRoot' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.

Check warning on line 4 in MyAppsContext.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

Non-nullable property 'GitRepoRoot' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.

Check warning on line 4 in MyAppsContext.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

Non-nullable property 'GitRepoRoot' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.

Check warning on line 4 in MyAppsContext.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest)

Non-nullable property 'GitRepoRoot' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.

Check warning on line 4 in MyAppsContext.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest)

Non-nullable property 'GitRepoRoot' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.

Check warning on line 4 in MyAppsContext.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest)

Non-nullable property 'GitRepoRoot' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.

public static string GetUserInput(string prompt)
{
Expand All @@ -12,40 +11,52 @@ public static string GetUserInput(string prompt)

public static string GetProjectStructure(string path, int indent = 0)
{
if (_gitRepoRootPath == null)
_gitRepoRootPath = FindGitRepoRoot(path);
if (GitRepoRoot == null) GitRepoRoot = FindGitRepoRoot(path);
if (indent == 0) Console.WriteLine("📁 Analyzing directory structure...");

return string.Join("\n", Directory.EnumerateFileSystemEntries(path)
var entries = Directory.EnumerateFileSystemEntries(path)
.OrderBy(e => e)
.Where(e => !FileChecker.ShouldSkip(new FileInfo(e), _gitRepoRootPath))
.Select(e =>
{
var info = new FileInfo(e);
var indentation = new string(' ', indent * 2);
var result =
$"{indentation}{info.Name}{(info.Attributes.HasFlag(FileAttributes.Directory) ? "/" : "")}";
return info.Attributes.HasFlag(FileAttributes.Directory)
? result + "\n" + GetProjectStructure(e, indent + 1)
: result;
}));
.Where(e => !FileChecker.ShouldSkip(new FileInfo(e), GitRepoRoot))
.ToList();

return string.Join("\n", entries.Select((e, i) =>
{
if (indent == 0) WriteProgress(i + 1, entries.Count);

var info = new FileInfo(e);
var indentation = new string(' ', indent * 2);
var result = $"{indentation}{info.Name}{(info.Attributes.HasFlag(FileAttributes.Directory) ? "/" : "")}";

return info.Attributes.HasFlag(FileAttributes.Directory)
? result + "\n" + GetProjectStructure(e, indent + 1)
: result;
}));
}

public static string GetFileContents(string path)
{
if (_gitRepoRootPath == null)
_gitRepoRootPath = FindGitRepoRoot(path);
if (GitRepoRoot == null) GitRepoRoot = FindGitRepoRoot(path);
Console.WriteLine("\n📄 Processing files...");

return string.Join("\n\n", Directory.EnumerateFiles(path, "*", SearchOption.AllDirectories)
.Where(f => !FileChecker.ShouldSkip(new FileInfo(f), _gitRepoRootPath))
.Select(f => $"{f}\n{new string('-', 100)}\n{File.ReadAllText(f)}"));
var files = Directory.EnumerateFiles(path, "*", SearchOption.AllDirectories)
.Where(f => !FileChecker.ShouldSkip(new FileInfo(f), GitRepoRoot))
.ToList();

return string.Join("\n\n", files.Select((f, i) =>
{
WriteProgress(i + 1, files.Count);
return $"{f}\n{new string('-', 100)}\n{File.ReadAllText(f)}";
}));
}

private static string FindGitRepoRoot(string path)
{
return Directory.Exists(Path.Combine(path, ".git"))
private static string FindGitRepoRoot(string path) =>
Directory.Exists(Path.Combine(path, ".git"))

Check warning on line 53 in MyAppsContext.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest)

Possible null reference return.

Check warning on line 53 in MyAppsContext.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest)

Possible null reference return.

Check warning on line 53 in MyAppsContext.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest)

Possible null reference return.

Check warning on line 53 in MyAppsContext.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

Possible null reference return.

Check warning on line 53 in MyAppsContext.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

Possible null reference return.

Check warning on line 53 in MyAppsContext.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

Possible null reference return.

Check warning on line 53 in MyAppsContext.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest)

Possible null reference return.

Check warning on line 53 in MyAppsContext.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest)

Possible null reference return.

Check warning on line 53 in MyAppsContext.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest)

Possible null reference return.
? path
: string.IsNullOrEmpty(path)
? null
: FindGitRepoRoot(Path.GetDirectoryName(path));
: string.IsNullOrEmpty(path) ? null : FindGitRepoRoot(Path.GetDirectoryName(path));

Check warning on line 55 in MyAppsContext.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest)

Possible null reference argument for parameter 'path' in 'string MyAppsContext.FindGitRepoRoot(string path)'.

Check warning on line 55 in MyAppsContext.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest)

Possible null reference argument for parameter 'path' in 'string MyAppsContext.FindGitRepoRoot(string path)'.

Check warning on line 55 in MyAppsContext.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest)

Possible null reference argument for parameter 'path' in 'string MyAppsContext.FindGitRepoRoot(string path)'.

Check warning on line 55 in MyAppsContext.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

Possible null reference argument for parameter 'path' in 'string MyAppsContext.FindGitRepoRoot(string path)'.

Check warning on line 55 in MyAppsContext.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

Possible null reference argument for parameter 'path' in 'string MyAppsContext.FindGitRepoRoot(string path)'.

Check warning on line 55 in MyAppsContext.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

Possible null reference argument for parameter 'path' in 'string MyAppsContext.FindGitRepoRoot(string path)'.

Check warning on line 55 in MyAppsContext.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest)

Possible null reference argument for parameter 'path' in 'string MyAppsContext.FindGitRepoRoot(string path)'.

Check warning on line 55 in MyAppsContext.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest)

Possible null reference argument for parameter 'path' in 'string MyAppsContext.FindGitRepoRoot(string path)'.

Check warning on line 55 in MyAppsContext.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest)

Possible null reference argument for parameter 'path' in 'string MyAppsContext.FindGitRepoRoot(string path)'.

private static void WriteProgress(int current, int total)
{
var percent = (int)((current / (double)total) * 100);
Console.Write($"\r⏳ Progress: {percent}% ({current}/{total})");
}
}
4 changes: 3 additions & 1 deletion Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
var stats = CalculateStats(path, content, sw.Elapsed);

WriteOutput(output, content, config.OutputFormat);
Console.WriteLine($"✅ Output written to {output}");
Console.WriteLine($"\n✅ Output written to {output}");
Console.WriteLine(stats);
}
catch (Exception ex)
Expand Down Expand Up @@ -74,6 +74,7 @@ static string BuildContent(string path, Config config)

static string CalculateStats(string path, string content, TimeSpan timeTaken) =>
$"""
📊 Stats:
📁 Files processed: {Directory.GetFiles(path, "*", SearchOption.AllDirectories).Length}
📝 Total lines: {content.Count(c => c == '\n')}
Expand All @@ -83,6 +84,7 @@ static string CalculateStats(string path, string content, TimeSpan timeTaken) =>

static void WriteOutput(string output, string content, string format)
{
Console.WriteLine("\n💾 Writing output...");
try
{
var outputPath = Directory.Exists(output) ? Path.Combine(output, "context.txt") : output;
Expand Down

0 comments on commit fdaea52

Please sign in to comment.