From d7f561c20fa8961e7babb08d4a42103d574b5d91 Mon Sep 17 00:00:00 2001 From: Paul Hebble Date: Sat, 5 Oct 2024 09:49:12 -0500 Subject: [PATCH 1/2] Fix spacing and alignment of ConsoleUI labels --- ConsoleUI/ModListScreen.cs | 12 ++++++++---- ConsoleUI/Toolkit/ConsoleLabel.cs | 10 +++++++--- ConsoleUI/Toolkit/ScreenObject.cs | 15 +++++++++++---- 3 files changed, 26 insertions(+), 11 deletions(-) diff --git a/ConsoleUI/ModListScreen.cs b/ConsoleUI/ModListScreen.cs index 6713e1580..2f5f1a569 100644 --- a/ConsoleUI/ModListScreen.cs +++ b/ConsoleUI/ModListScreen.cs @@ -276,13 +276,13 @@ public ModListScreen(ConsoleTheme theme, }); // Abstract of currently selected mod under grid - AddObject(new ConsoleLabel(1, -1, -1, + AddObject(new ConsoleLabel(1, -1, -updatedWidth - 1, () => moduleList.Selection?.@abstract.Trim() ?? "", null, th => th.DimLabelFg)); AddObject(new ConsoleLabel( - -searchWidth, -1, -2, + -updatedWidth, -1, -1, () => { var days = Math.Round(timeSinceUpdate.TotalDays); return days < 1 ? "" @@ -294,8 +294,8 @@ public ModListScreen(ConsoleTheme theme, return timeSinceUpdate < RepositoryDataManager.TimeTillStale ? th.RegistryUpToDate : timeSinceUpdate < RepositoryDataManager.TimeTillVeryStale ? th.RegistryStale : th.RegistryVeryStale; - } - )); + }, + TextAlign.Right)); var opts = new List() { new ConsoleMenuOption(Properties.Resources.ModListPlayMenu, "", @@ -766,6 +766,10 @@ private long totalInstalledDownloadSize() Properties.Resources.ModListSearchUnfocusedGhostText.Length )); + private static readonly int updatedWidth = + string.Format(Properties.Resources.ModListUpdatedDaysAgo, 9999) + .Length; + private static readonly string unavailable = "!"; private static readonly string notinstalled = " "; private static readonly string installed = Symbols.checkmark; diff --git a/ConsoleUI/Toolkit/ConsoleLabel.cs b/ConsoleUI/Toolkit/ConsoleLabel.cs index f4131f6f1..dceb4725d 100644 --- a/ConsoleUI/Toolkit/ConsoleLabel.cs +++ b/ConsoleUI/Toolkit/ConsoleLabel.cs @@ -16,17 +16,20 @@ public class ConsoleLabel : ScreenObject { /// Function returning the text to show in the label /// Function returning the background color for the label /// Function returning the foreground color for the label + /// Alignment of text within the label public ConsoleLabel(int l, int t, int r, Func lf, Func? bgFunc = null, - Func? fgFunc = null) + Func? fgFunc = null, + TextAlign ta = TextAlign.Left) : base(l, t, r, t) { labelFunc = lf; getBgColor = bgFunc; getFgColor = fgFunc; + textAlign = ta; } /// @@ -41,9 +44,9 @@ public override void Draw(ConsoleTheme theme, bool focused) Console.BackgroundColor = getBgColor == null ? theme.LabelBg : getBgColor(theme); Console.ForegroundColor = getFgColor == null ? theme.LabelFg : getFgColor(theme); try { - Console.Write(FormatExactWidth(labelFunc(), w)); + Console.Write(FormatExactWidth(labelFunc(), w, ta: textAlign)); } catch (Exception ex) { - Console.Write(FormatExactWidth(ex.Message, w)); + Console.Write(FormatExactWidth(ex.Message, w, ta: textAlign)); } } @@ -55,6 +58,7 @@ public override void Draw(ConsoleTheme theme, bool focused) private readonly Func labelFunc; private readonly Func? getBgColor; private readonly Func? getFgColor; + private readonly TextAlign textAlign; } } diff --git a/ConsoleUI/Toolkit/ScreenObject.cs b/ConsoleUI/Toolkit/ScreenObject.cs index 53067fdb8..fadd89fe2 100644 --- a/ConsoleUI/Toolkit/ScreenObject.cs +++ b/ConsoleUI/Toolkit/ScreenObject.cs @@ -48,13 +48,20 @@ public static string PadCenter(string s, int w, char pad = ' ') /// String to process /// Width to fit /// Character to use for padding + /// Alignment of text within the label /// /// val{padding} or substring of val /// - public static string FormatExactWidth(string val, int w, char pad = ' ') - { - return val.PadRight(w, pad)[..w]; - } + public static string FormatExactWidth(string val, + int w, + char pad = ' ', + TextAlign ta = TextAlign.Left) + => ta switch + { + TextAlign.Right => val.PadLeft(w, pad)[..w], + TextAlign.Center => PadCenter(val, w, pad), + TextAlign.Left or _ => val.PadRight(w, pad)[..w], + }; /// /// Truncate a string if it's longer than the limit From d85f54ba836a6421a5de2d90d2358b37b56b46c6 Mon Sep 17 00:00:00 2001 From: Paul Hebble Date: Sat, 5 Oct 2024 09:49:35 -0500 Subject: [PATCH 2/2] Show zero download counts as blank in ConsoleUI --- ConsoleUI/ModListScreen.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ConsoleUI/ModListScreen.cs b/ConsoleUI/ModListScreen.cs index 2f5f1a569..398c9f8b3 100644 --- a/ConsoleUI/ModListScreen.cs +++ b/ConsoleUI/ModListScreen.cs @@ -71,8 +71,8 @@ public ModListScreen(ConsoleTheme theme, new ConsoleListBoxColumn( Properties.Resources.ModListDownloadsHeader, m => repoData.GetDownloadCount(registry.Repositories.Values, m.identifier) - ?.ToString() - ?? "", + is int i and > 0 + ? i.ToString() : "", (a, b) => (repoData.GetDownloadCount(registry.Repositories.Values, a.identifier) ?? 0) .CompareTo(repoData.GetDownloadCount(registry.Repositories.Values, b.identifier) ?? 0), 12),