feat(bot): config setting to pad 5-character ids in lists

This commit is contained in:
alyssa 2024-05-19 21:31:26 +09:00
parent 0a6d4bcb76
commit 6d49961daf
7 changed files with 86 additions and 3 deletions

View file

@ -116,6 +116,13 @@ public class Config
"disabled"
));
items.Add(new(
"Pad IDs",
"Whether to pad 5-character IDs in lists (left/right)",
ctx.Config.HidListPadding.ToUserString(),
"off"
));
await ctx.Paginate<PaginatedConfigItem>(
items.ToAsyncEnumerable(),
items.Count,
@ -485,4 +492,48 @@ public class Config
await ctx.Repository.UpdateSystemConfig(ctx.System.Id, new() { HidDisplayCaps = newVal });
await ctx.Reply($"Displaying IDs as capital letters is now {EnabledDisabled(newVal)}.");
}
public async Task HidListPadding(Context ctx)
{
if (!ctx.HasNext())
{
string message;
switch (ctx.Config.HidListPadding)
{
case SystemConfig.HidPadFormat.None: message = "Padding 5-character IDs in lists is currently disabled."; break;
case SystemConfig.HidPadFormat.Left: message = "5-character IDs displayed in lists will have a padding space added to the beginning."; break;
case SystemConfig.HidPadFormat.Right: message = "5-character IDs displayed in lists will have a padding space added to the end."; break;
default: throw new Exception("unreachable");
}
await ctx.Reply(message);
return;
}
var badInputError = "Valid padding settings are `left`, `right`, or `off`.";
var toggleOff = ctx.MatchToggleOrNull(false);
switch (toggleOff)
{
case true: throw new PKError(badInputError);
case false:
{
await ctx.Repository.UpdateSystemConfig(ctx.System.Id, new() { HidListPadding = SystemConfig.HidPadFormat.None });
await ctx.Reply("Padding 5-character IDs in lists has been disabled.");
return;
}
}
if (ctx.Match("left", "l"))
{
await ctx.Repository.UpdateSystemConfig(ctx.System.Id, new() { HidListPadding = SystemConfig.HidPadFormat.Left });
await ctx.Reply("5-character IDs displayed in lists will now have a padding space added to the beginning.");
}
else if (ctx.Match("right", "r"))
{
await ctx.Repository.UpdateSystemConfig(ctx.System.Id, new() { HidListPadding = SystemConfig.HidPadFormat.Right });
await ctx.Reply("5-character IDs displayed in lists will now have a padding space added to the end.");
}
else throw new PKError(badInputError);
}
}

View file

@ -128,7 +128,9 @@ public static class ContextListExt
// so run it through a helper that "makes it work" :)
eb.WithSimpleLineContent(page.Select(m =>
{
var ret = $"[`{m.DisplayHid(ctx.Config)}`] **{m.NameFor(ctx)}** ";
var leftpad = m.Hid.Length == 5 && ctx.Config.HidListPadding == SystemConfig.HidPadFormat.Left ? " " : "";
var rightpad = m.Hid.Length == 5 && ctx.Config.HidListPadding == SystemConfig.HidPadFormat.Right ? " " : "";
var ret = $"[`{leftpad}{m.DisplayHid(ctx.Config)}{rightpad}`] **{m.NameFor(ctx)}** ";
if (opts.IncludeMessageCount && m.MessageCountFor(lookupCtx) is { } count)
ret += $"({count} messages)";
@ -238,7 +240,9 @@ public static class ContextListExt
// so run it through a helper that "makes it work" :)
eb.WithSimpleLineContent(page.Select(g =>
{
var ret = $"[`{g.DisplayHid(ctx.Config)}`] **{g.NameFor(ctx)}** ";
var leftpad = g.Hid.Length == 5 && ctx.Config.HidListPadding == SystemConfig.HidPadFormat.Left ? " " : "";
var rightpad = g.Hid.Length == 5 && ctx.Config.HidListPadding == SystemConfig.HidPadFormat.Right ? " " : "";
var ret = $"[`{leftpad}{g.DisplayHid(ctx.Config)}{rightpad}`] **{g.NameFor(ctx)}** ";
switch (opts.SortProperty)
{