Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions RepoDumpKit/AppSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ internal static class AppSettings
public const string PreservedGitIgnoredCommand =
"/d /c \"(git ls-files --cached & git ls-files -o -i --exclude-standard) ^| git check-ignore --no-index --stdin\"";

public const int MaxSavedSolutionPaths = 10;
public const int MaxTreeEntriesPerDirectory = 80;
public const int MaxTreeDepth = 16;
public const int MaxIgnoredPathsPerRenderedSubtree = 250;
Expand Down
107 changes: 84 additions & 23 deletions RepoDumpKit/SolutionPathService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,29 +20,45 @@
return argumentPath;
}

string? existingPath = ReadSavedPath(configFilePath);
IReadOnlyList<string> savedPaths = ReadSavedPaths(configFilePath);

if (!string.IsNullOrWhiteSpace(existingPath))
if (savedPaths.Count > 0)
{
Console.WriteLine($"Saved path: {existingPath}");
Console.Write("Press Enter to use it, or type a new path: ");
string? typed = Console.ReadLine();
Console.WriteLine("Recent paths:");

if (string.IsNullOrWhiteSpace(typed))
for (int index = 0; index < savedPaths.Count; index++)
{
return existingPath;
Console.WriteLine($"{index + 1}. {savedPaths[index]}");
}

string newTyped = typed.Trim();
Console.Write("Choose a number, press Enter for 1, or paste a new path: ");

while (!Directory.Exists(newTyped))
while (true)
{
Console.Write("Invalid path. Enter a valid full path: ");
newTyped = (Console.ReadLine() ?? string.Empty).Trim();
string typed = (Console.ReadLine() ?? string.Empty).Trim();
string selectedPath;

if (string.IsNullOrWhiteSpace(typed))
{
selectedPath = savedPaths[0];
}
else if (TryGetSavedPathByNumber(typed, savedPaths, out string? savedPath))
{
selectedPath = savedPath;

Check warning on line 47 in RepoDumpKit/SolutionPathService.cs

View workflow job for this annotation

GitHub Actions / build

Converting null literal or possible null value to non-nullable type.

Check warning on line 47 in RepoDumpKit/SolutionPathService.cs

View workflow job for this annotation

GitHub Actions / build

Converting null literal or possible null value to non-nullable type.
}
else
{
selectedPath = typed;
}

if (Directory.Exists(selectedPath))
{
SavePath(configFilePath, selectedPath);
return selectedPath;
}

Console.Write("Invalid path. Choose a listed number or enter a valid full path: ");
}

SavePath(configFilePath, newTyped);
return newTyped;
}

while (true)
Expand All @@ -60,37 +76,82 @@
}
}

private static string? ReadSavedPath(string configFilePath)
private static IReadOnlyList<string> ReadSavedPaths(string configFilePath)
{
if (!File.Exists(configFilePath))
{
return null;
return [];
}

try
{
string existingPath = File.ReadAllText(configFilePath).Trim();
string[] storedPaths = File.ReadAllLines(configFilePath);
List<string> validPaths = [];
HashSet<string> seenPaths = new(AppSettings.PathComparer);

if (Directory.Exists(existingPath))
foreach (string storedPath in storedPaths)
{
return existingPath;
string path = storedPath.Trim();

if (string.IsNullOrWhiteSpace(path) || !seenPaths.Add(path))
{
continue;
}

if (Directory.Exists(path))
{
validPaths.Add(path);

if (validPaths.Count == AppSettings.MaxSavedSolutionPaths)
{
break;
}
}
else
{
Console.WriteLine($"Stored path '{path}' not found or is invalid.");
}
}

Console.WriteLine($"Stored path '{existingPath}' not found or is invalid.");
return null;
return validPaths;
}
catch (Exception ex)
{
Console.WriteLine($"Error reading config file: {ex.GetType().Name}: {ex.Message}");
return null;
return [];
}
}

private static bool TryGetSavedPathByNumber(string input, IReadOnlyList<string> savedPaths, out string? savedPath)
{
savedPath = null;

if (!int.TryParse(input, out int selectedNumber))
{
return false;
}

if (selectedNumber < 1 || selectedNumber > savedPaths.Count)
{
return false;
}

savedPath = savedPaths[selectedNumber - 1];
return true;
}

private static void SavePath(string configFilePath, string path)
{
try
{
File.WriteAllText(configFilePath, path);
string fullPath = Path.GetFullPath(path.Trim());
List<string> savedPaths = ReadSavedPaths(configFilePath)
.Where(savedPath => !string.Equals(savedPath, fullPath, AppSettings.PathComparison))
.Prepend(fullPath)
.Take(AppSettings.MaxSavedSolutionPaths)
.ToList();

File.WriteAllLines(configFilePath, savedPaths);
Console.WriteLine($"Path saved to {configFilePath}");
}
catch (Exception ex)
Expand Down
Loading