Skip to content

Commit

Permalink
Merge #4223 Label spacing and alignment, show zero download counts as…
Browse files Browse the repository at this point in the history
… blank in ConsoleUI
  • Loading branch information
HebaruSan committed Oct 5, 2024
2 parents 2fdb599 + d85f54b commit 8c8a2d0
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 13 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ All notable changes to this project will be documented in this file.

- [Core] Skip corrupted .acf files in Steam library (#4200 by: HebaruSan)
- [GUI] Fix label color blending (#4203 by: HebaruSan)
- [ConsoleUI] Label spacing and alignment, show zero download counts as blank in ConsoleUI (#4223 by: HebaruSan)

### Internal

Expand Down
16 changes: 10 additions & 6 deletions ConsoleUI/ModListScreen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ public ModListScreen(ConsoleTheme theme,
new ConsoleListBoxColumn<CkanModule>(
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),
Expand Down Expand Up @@ -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 ? ""
Expand All @@ -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<ConsoleMenuOption?>() {
new ConsoleMenuOption(Properties.Resources.ModListPlayMenu, "",
Expand Down Expand Up @@ -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;
Expand Down
10 changes: 7 additions & 3 deletions ConsoleUI/Toolkit/ConsoleLabel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,20 @@ public class ConsoleLabel : ScreenObject {
/// <param name="lf">Function returning the text to show in the label</param>
/// <param name="bgFunc">Function returning the background color for the label</param>
/// <param name="fgFunc">Function returning the foreground color for the label</param>
/// <param name="ta">Alignment of text within the label</param>
public ConsoleLabel(int l,
int t,
int r,
Func<string> lf,
Func<ConsoleTheme, ConsoleColor>? bgFunc = null,
Func<ConsoleTheme, ConsoleColor>? fgFunc = null)
Func<ConsoleTheme, ConsoleColor>? fgFunc = null,
TextAlign ta = TextAlign.Left)
: base(l, t, r, t)
{
labelFunc = lf;
getBgColor = bgFunc;
getFgColor = fgFunc;
textAlign = ta;
}

/// <summary>
Expand All @@ -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));
}
}

Expand All @@ -55,6 +58,7 @@ public override void Draw(ConsoleTheme theme, bool focused)
private readonly Func<string> labelFunc;
private readonly Func<ConsoleTheme, ConsoleColor>? getBgColor;
private readonly Func<ConsoleTheme, ConsoleColor>? getFgColor;
private readonly TextAlign textAlign;
}

}
15 changes: 11 additions & 4 deletions ConsoleUI/Toolkit/ScreenObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,20 @@ public static string PadCenter(string s, int w, char pad = ' ')
/// <param name="val">String to process</param>
/// <param name="w">Width to fit</param>
/// <param name="pad">Character to use for padding</param>
/// <param name="ta">Alignment of text within the label</param>
/// <returns>
/// val{padding} or substring of val
/// </returns>
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],
};

/// <summary>
/// Truncate a string if it's longer than the limit
Expand Down

0 comments on commit 8c8a2d0

Please sign in to comment.