Skip to content

Commit

Permalink
Color version number choices
Browse files Browse the repository at this point in the history
  • Loading branch information
ThomasArdal committed Feb 27, 2024
1 parent d4224a4 commit d6b51f3
Showing 1 changed file with 21 additions and 5 deletions.
26 changes: 21 additions & 5 deletions src/NuPU/UpdateCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using System.Xml;
Expand Down Expand Up @@ -137,8 +138,8 @@ public override async Task<int> ExecuteAsync(CommandContext context, UpdateComma
var choices = new List<string>();
var currentVersionString = $"{nugetVersion.OriginalVersion} (current)";
choices.Add(currentVersionString);
choices.AddRange(versionsToShow.OrderBy(v => v).Select(v => v.ToString()));
var skipString = "[invert]Skip project[/]";
choices.AddRange(versionsToShow.OrderBy(v => v).Select(v => Colored(nugetVersion, v)));
var skipString = "[grey]Skip project[/]";
choices.Add(skipString);

showUpToDate = false;
Expand All @@ -152,7 +153,7 @@ public override async Task<int> ExecuteAsync(CommandContext context, UpdateComma
break;
}

var dotnet = new ProcessStartInfo("dotnet", $"add package {package.Id} -v {choice} -s {source.SourceUri}")
var dotnet = new ProcessStartInfo("dotnet", $"add package {package.Id} -v {Uncolored(choice)} -s {source.SourceUri}")
{
WorkingDirectory = csProjFile.Directory.FullName,
CreateNoWindow = true,
Expand All @@ -176,10 +177,12 @@ public override async Task<int> ExecuteAsync(CommandContext context, UpdateComma
if (!string.IsNullOrWhiteSpace(outputAndError[0]))
{
var lines = outputAndError[0].Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries);
#pragma warning disable S6608 // Prefer indexing instead of "Enumerable" methods on types implementing "IList"
if (lines.Length > 0 && lines.Last().StartsWith("error"))
{
AnsiConsole.MarkupLine($"[red]{lines.Last()}[/]");
}
#pragma warning restore S6608 // Prefer indexing instead of "Enumerable" methods on types implementing "IList"
}
}
catch (FatalProtocolException ex) when (IsAuthenticationError(ex))
Expand All @@ -206,13 +209,26 @@ public override async Task<int> ExecuteAsync(CommandContext context, UpdateComma
return 0;
}

public static string Uncolored(string input)
{
var pattern = @"\[(.*?)\](.*?)\[\/\]";
return Regex.Replace(input, pattern, "$2");
}

private static string Colored(NuGetVersion currentVersion, NuGetVersion newVersion)
{
if (currentVersion.Major != newVersion.Major) return $"[red]{newVersion}[/]";
else if (currentVersion.Minor != newVersion.Minor) return $"[yellow]{newVersion}[/]";
else return $"[green]{newVersion}[/]";
}

private static bool IsAuthenticationError(FatalProtocolException ex)
{
var baseException = ex.GetBaseException() as HttpRequestException;
return baseException?.StatusCode == System.Net.HttpStatusCode.Unauthorized;
}

private bool Ignored(FileInfo fileInfo, List<string> ignoreDirs)
private static bool Ignored(FileInfo fileInfo, List<string> ignoreDirs)
{
if (ignoreDirs.Count == 0) return false;

Expand All @@ -226,7 +242,7 @@ private bool Ignored(FileInfo fileInfo, List<string> ignoreDirs)
return false;
}

private List<string> ResolveIgnoreDirs(string rootPath)
private static List<string> ResolveIgnoreDirs(string rootPath)
{
var ignoreDirs = new List<string> { ".git", ".github", ".vs", ".vscode", "bin", "obj", "packages", "node_modules" };

Expand Down

0 comments on commit d6b51f3

Please sign in to comment.