Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Label spacing and alignment, show zero download counts as blank in ConsoleUI #4223

Merged
merged 2 commits into from
Oct 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
() => [email protected]() ?? "",
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