diff --git a/src/TUI/Dashboards/DependencyDashboard.cs b/src/TUI/Dashboards/DependencyDashboard.cs index d3ebd9e..b4c450e 100644 --- a/src/TUI/Dashboards/DependencyDashboard.cs +++ b/src/TUI/Dashboards/DependencyDashboard.cs @@ -49,7 +49,7 @@ public class DependencyDashboard : IControl return string.Join("", conventionDependencies - .Select(package.Dependencies.GetVersion) + .Select(dependency => GetVersion(dependency, package)) .Select(RenderCurrentVersion)); } catch (HttpRequestException exception) @@ -61,7 +61,7 @@ public class DependencyDashboard : IControl case HttpStatusCode.Forbidden: return " Not enough rights.".Pastel(Palette.ErrorColor); case HttpStatusCode.NotFound: - return " Repository not found.".Pastel(Palette.ErrorColor); + return " Repository or branch master not found.".Pastel(Palette.ErrorColor); } throw; @@ -73,6 +73,37 @@ public class DependencyDashboard : IControl } } + + private static string GetVersion(DependencyDto dependency, Package package) + { + var currentVersion = package.ParseVersion(dependency.Name); + + if (currentVersion == null) + { + return "".Hint(); + } + + var conventionVersion = dependency.Version?.ToVersion(); + return PaintingVersion(currentVersion, conventionVersion); + } + + private static string PaintingVersion(Version current, Version? convention) + { + var textVersion = current.ToString(); + + if (current > convention) + { + return textVersion.Info(); + } + + if (current < convention) + { + return current.Major == convention.Major ? textVersion.Warning() : textVersion.Error(); + } + + return textVersion.Primary(); + } + private readonly static Dictionary Packages = new(); private static Package DownloadPackage(SourceDto sourceDto) @@ -86,7 +117,7 @@ public class DependencyDashboard : IControl var endpoint = sourceDto.Tags.Have("gitlab") ? GetGitlabEndpoint(sourceDto) : sourceDto.Repo; var json = client.GetStringAsync(endpoint).GetAwaiter().GetResult(); var package = JsonSerializer.Deserialize(json); - Packages.Add(sourceDto.Repo, package); + Packages.Add(endpoint, package); return package; } @@ -115,30 +146,38 @@ public class DependencyDashboard : IControl private static string GetTitle(SourceDto sourceDto) { - var rowText = new StringBuilder(); + var title = ""; - RenderPadding(rowText); - RenderTags(rowText, sourceDto); - rowText.Append(sourceDto.Name); - RenderPadding(rowText); - var text = rowText.ToString(); - return $"{text}{' '.Repeat(TitleWidth - text.Width())}"; + title += RenderPadding(); + title += RenderTags(sourceDto); + if (title.Width() + sourceDto.Name.Length + Theme.Padding <= TitleWidth) + { + title += sourceDto.Name; + } + else + { + var maxNameWidth = TitleWidth - title.Width() - Theme.Padding; + title += $"{sourceDto.Name[..(maxNameWidth - 1)]}{"#".Hint()}"; + } + + title += RenderPadding(); + return $"{title}{' '.Repeat(TitleWidth - title.Width())}"; } - private static void RenderPadding(StringBuilder rowText) + private static string RenderPadding() { - rowText.Append(new string(' ', Theme.Padding)); + return new string(' ', Theme.Padding); } - private static void RenderTags(StringBuilder rowText, SourceDto sourceDto) + private static string RenderTags(SourceDto sourceDto) { - rowText.Append(GetGitApplication(sourceDto)); - rowText.Append(sourceDto.Tags.Have("public") - ? GetIcon("󰞉", "00FFFF") - : GetIcon("󰕑", "AFE1AF")); - rowText.Append(GetIcon("󰚩", "4285F4", sourceDto.Tags.Have("seo"))); - rowText.Append(GetIcon("", "FFD700", sourceDto.Tags.Have("auth"))); - rowText.Append(GetApplicationType(sourceDto)); + var tags = ""; + tags += GetGitApplication(sourceDto); + tags += sourceDto.Tags.Have("public") ? GetIcon("󰞉", "00FFFF") : GetIcon("󰕑", "AFE1AF"); + tags += GetIcon("󰚩", "4285F4", sourceDto.Tags.Have("seo")); + tags += GetIcon("", "FFD700", sourceDto.Tags.Have("auth")); + tags += GetApplicationType(sourceDto); + return tags; } private static string GetApplicationType(SourceDto sourceDto) diff --git a/src/TUI/Domain/Package.cs b/src/TUI/Domain/Package.cs index edd87c1..b7f7f87 100644 --- a/src/TUI/Domain/Package.cs +++ b/src/TUI/Domain/Package.cs @@ -7,5 +7,36 @@ namespace TUI.Domain; public class Package { [JsonPropertyName("dependencies")] - public JsonObject Dependencies { get; set; } + public JsonObject? Dependencies { get; set; } + + [JsonPropertyName("devDependencies")] + public JsonObject? DevDependencies { get; set; } + + [JsonPropertyName("engines")] + public JsonObject? Engines { get; set; } + + public Version? ParseVersion(string? dependencyName) + { + if (dependencyName == null) + { + return null; + } + + JsonNode? version = null; + + var lowerDependencyName = dependencyName.ToLower(); + Dependencies?.TryGetPropertyValue(lowerDependencyName, out version); + + if (version == null) + { + Engines?.TryGetPropertyValue(lowerDependencyName, out version); + } + + if (version == null) + { + DevDependencies?.TryGetPropertyValue(lowerDependencyName, out version); + } + + return version?.GetValue().ToVersion(); + } } \ No newline at end of file diff --git a/src/TUI/Extensions.cs b/src/TUI/Extensions.cs index ec15125..79f6926 100644 --- a/src/TUI/Extensions.cs +++ b/src/TUI/Extensions.cs @@ -16,7 +16,7 @@ public static class Extensions public static string Repeat(this char symbol, int repeatCount) { - return new string(symbol, repeatCount); + return repeatCount < 0 ? "" : new string(symbol, repeatCount); } public static int Width(this string text) @@ -31,36 +31,7 @@ public static class Extensions return stringInfo.LengthInTextElements; } - public static string GetVersion(this JsonObject dependencies, DependencyDto dependencyDto) - { - dependencies.TryGetPropertyValue(dependencyDto.Name.ToLower(), out var version); - var currentVersion = version?.GetValue().ToVersion(); - if (currentVersion == null) - { - return "".Hint(); - } - - var conventionVersion = dependencyDto.Version.ToVersion(); - - if (currentVersion > conventionVersion) - { - return currentVersion.ToString().Info(); - } - - if (currentVersion < conventionVersion) - { - if (currentVersion.Major == conventionVersion.Major) - { - return currentVersion.ToString().Warning(); - } - - return currentVersion.ToString().Error(); - } - - return currentVersion.ToString().Primary(); - } - - private static Version? ToVersion(this string textVersion) + public static Version? ToVersion(this string textVersion) { var version = textVersion.Replace("^", "").Replace("~", "").Split("."); if (version.Length != 3) diff --git a/src/TUI/UserInterface/Palette.cs b/src/TUI/UserInterface/Palette.cs index 4ec5e56..800b627 100644 --- a/src/TUI/UserInterface/Palette.cs +++ b/src/TUI/UserInterface/Palette.cs @@ -1,4 +1,3 @@ -using System.Drawing; using Pastel;