InteractiveMenu.cs small style cleanup

This commit is contained in:
Canon
2026-03-29 08:45:25 +08:00
parent ffb26dbfb9
commit 65893f405b
+23 -36
View File
@@ -126,50 +126,46 @@ public class InteractiveMenu
.Required(false) .Required(false)
.UseConverter(m => $"{m.Name} [grey]({m.CommitsBehind} commits behind)[/]"); .UseConverter(m => $"{m.Name} [grey]({m.CommitsBehind} commits behind)[/]");
foreach (var mod in updateableMods) foreach (InstalledMod mod in updateableMods)
{ {
prompt.AddChoice(mod).Select(); prompt.AddChoice(mod).Select();
} }
var selected = AnsiConsole.Prompt(prompt); List<InstalledMod> selected = AnsiConsole.Prompt(prompt);
if (selected.Count == 0) if (selected.Count == 0)
{
return false; return false;
}
// Show incoming commits for each selected mod // Show incoming commits for each selected mod
AnsiConsole.WriteLine(); AnsiConsole.WriteLine();
AnsiConsole.MarkupLine("[bold]Incoming commits:[/]"); AnsiConsole.MarkupLine("[bold]Incoming commits:[/]");
AnsiConsole.WriteLine(); AnsiConsole.WriteLine();
foreach (var mod in selected) foreach (InstalledMod mod in selected)
{ {
var commits = await _gitService.GetIncomingCommitsAsync(mod.Path, 5, ct); IReadOnlyList<GitCommitInfo> commits = await _gitService.GetIncomingCommitsAsync(mod.Path, 5, ct);
ModTableRenderer.RenderIncomingCommits(mod, commits); ModTableRenderer.RenderIncomingCommits(mod, commits);
} }
if (!AnsiConsole.Confirm("Proceed with update?")) if (!await AnsiConsole.ConfirmAsync("Proceed with update?", true, ct))
{
return false; return false;
}
var results = await ProgressReporter.WithBatchProgressAsync( var results = await ProgressReporter.WithBatchProgressAsync(
"Updating mods", "Updating mods",
selected.ToList(), selected.ToList(),
m => m.Name, mod => mod.Name,
async (mod, progress) => async (mod, progress) =>
{ {
progress.Report(25); progress.Report(25);
var fetchOk = await _gitService.FetchAsync(mod.Path, ct: ct); var fetchOk = await _gitService.FetchAsync(mod.Path, ct: ct);
if (!fetchOk) if (!fetchOk)
return (false, "Fetch failed"); return (Success: false, Error: "Fetch failed");
progress.Report(50); progress.Report(50);
var pullOk = await _gitService.PullAsync(mod.Path, ct: ct); var pullOk = await _gitService.PullAsync(mod.Path, ct: ct);
progress.Report(100); progress.Report(100);
return (pullOk, pullOk ? null : "Pull failed"); return (Success: pullOk, Error: pullOk ? null : "Pull failed");
}); });
ModTableRenderer.RenderUpdateSummary(results); ModTableRenderer.RenderUpdateSummary(results);
@@ -181,14 +177,14 @@ public class InteractiveMenu
private async Task<bool> HandleInstallAsync(CancellationToken ct) private async Task<bool> HandleInstallAsync(CancellationToken ct)
{ {
var orgRepos = await ProgressReporter.WithStatusAsync( IReadOnlyList<RemoteRepo> orgRepos = await ProgressReporter.WithStatusAsync(
"Fetching available mods...", "Fetching available mods...",
async () => await _gitHubApiService.GetOrganizationReposAsync(ct)); async () => await _gitHubApiService.GetOrganizationReposAsync(ct));
var installedNames = _mods.Select(m => m.Name).ToHashSet(StringComparer.OrdinalIgnoreCase); IEnumerable<string> installedNames = _mods.Select(m => m.Name);
var availableRepos = orgRepos List<RemoteRepo> availableRepos = orgRepos
.Where(r => !installedNames.Contains(r.Name)) .Where(repo => !installedNames.Contains(repo.Name))
.OrderBy(r => r.Name) .OrderBy(repo => repo.Name)
.ToList(); .ToList();
if (availableRepos.Count == 0) if (availableRepos.Count == 0)
@@ -198,7 +194,7 @@ public class InteractiveMenu
return false; return false;
} }
var selected = AnsiConsole.Prompt( List<RemoteRepo> selectedRepos = AnsiConsole.Prompt(
new MultiSelectionPrompt<RemoteRepo>() new MultiSelectionPrompt<RemoteRepo>()
.Title("[bold]Select mods to install:[/]") .Title("[bold]Select mods to install:[/]")
.InstructionsText("[grey](Press <space> to mark a mod for install, <enter> to confirm)[/]") .InstructionsText("[grey](Press <space> to mark a mod for install, <enter> to confirm)[/]")
@@ -210,22 +206,20 @@ public class InteractiveMenu
: $"{r.Name} [grey]— {Truncate(r.Description, 50)}[/]") : $"{r.Name} [grey]— {Truncate(r.Description, 50)}[/]")
.AddChoices(availableRepos)); .AddChoices(availableRepos));
if (selected.Count == 0) if (selectedRepos.Count == 0)
{
return false; return false;
}
var results = await ProgressReporter.WithBatchProgressAsync( var results = await ProgressReporter.WithBatchProgressAsync(
"Installing mods", "Installing mods",
selected.ToList(), selectedRepos.ToList(),
r => r.Name, repo => repo.Name,
async (repo, progress) => async (repo, progress) =>
{ {
var targetPath = Path.Combine(_modsDirectory, repo.Name); var targetPath = Path.Combine(_modsDirectory, repo.Name);
var result = await _gitService.CloneAsync(repo.CloneUrl, targetPath, percentProgress: progress, ct: ct); var result = await _gitService.CloneAsync(repo.CloneUrl, targetPath, percentProgress: progress, ct: ct);
return (Success: result.Success, Error: result.Error); return (result.Success, result.Error);
}); });
ModTableRenderer.RenderUpdateSummary(results); ModTableRenderer.RenderUpdateSummary(results);
@@ -237,7 +231,7 @@ public class InteractiveMenu
private async Task<bool> HandleUninstallAsync(CancellationToken ct) private async Task<bool> HandleUninstallAsync(CancellationToken ct)
{ {
var installedMods = _mods.ToList(); List<InstalledMod> installedMods = _mods.ToList();
if (installedMods.Count == 0) if (installedMods.Count == 0)
{ {
@@ -246,7 +240,7 @@ public class InteractiveMenu
return false; return false;
} }
var selected = AnsiConsole.Prompt( List<InstalledMod> selected = AnsiConsole.Prompt(
new MultiSelectionPrompt<InstalledMod>() new MultiSelectionPrompt<InstalledMod>()
.Title("[bold red]Select mods to uninstall:[/]") .Title("[bold red]Select mods to uninstall:[/]")
.InstructionsText("[grey](Press <space> to mark a mod for uninstallation, <enter> to confirm)[/]") .InstructionsText("[grey](Press <space> to mark a mod for uninstallation, <enter> to confirm)[/]")
@@ -257,9 +251,7 @@ public class InteractiveMenu
.AddChoices(installedMods)); .AddChoices(installedMods));
if (selected.Count == 0) if (selected.Count == 0)
{
return false; return false;
}
AnsiConsole.WriteLine(); AnsiConsole.WriteLine();
AnsiConsole.MarkupLine("[bold red]WARNING: This will permanently delete the following mods:[/]"); AnsiConsole.MarkupLine("[bold red]WARNING: This will permanently delete the following mods:[/]");
@@ -539,14 +531,9 @@ public class InteractiveMenu
$"Resetting {currentBranch} to {commit.ShortHash}...", $"Resetting {currentBranch} to {commit.ShortHash}...",
async () => await _gitService.ResetToCommitAsync(mod.Path, commit.Hash, ct)); async () => await _gitService.ResetToCommitAsync(mod.Path, commit.Hash, ct));
if (success) AnsiConsole.MarkupLine(success
{ ? $"[green]Reset branch '{currentBranch}' to commit {commit.ShortHash}[/]"
AnsiConsole.MarkupLine($"[green]Reset branch '{currentBranch}' to commit {commit.ShortHash}[/]"); : $"[red]Failed to reset to commit {commit.ShortHash}[/]");
}
else
{
AnsiConsole.MarkupLine($"[red]Failed to reset to commit {commit.ShortHash}[/]");
}
} }
WaitForKey(); WaitForKey();