From 6d49961daf671509a7929d0d9accc0429ee8837e Mon Sep 17 00:00:00 2001 From: alyssa Date: Sun, 19 May 2024 21:31:26 +0900 Subject: [PATCH] feat(bot): config setting to pad 5-character ids in lists --- PluralKit.Bot/CommandMeta/CommandTree.cs | 2 + PluralKit.Bot/Commands/Config.cs | 51 +++++++++++++++++++ .../Commands/Lists/ContextListExt.cs | 8 ++- PluralKit.Core/Database/Migrations/43.sql | 6 +++ .../Database/Utils/DatabaseMigrator.cs | 2 +- .../Models/Patch/SystemConfigPatch.cs | 5 ++ PluralKit.Core/Models/SystemConfig.cs | 15 ++++++ 7 files changed, 86 insertions(+), 3 deletions(-) create mode 100644 PluralKit.Core/Database/Migrations/43.sql diff --git a/PluralKit.Bot/CommandMeta/CommandTree.cs b/PluralKit.Bot/CommandMeta/CommandTree.cs index b4a9845b..3b5df558 100644 --- a/PluralKit.Bot/CommandMeta/CommandTree.cs +++ b/PluralKit.Bot/CommandMeta/CommandTree.cs @@ -542,6 +542,8 @@ public partial class CommandTree return ctx.Execute(null, m => m.HidDisplaySplit(ctx)); if (ctx.MatchMultiple(new[] { "cap", "caps", "capitalize", "capitalise" }, new[] { "id", "ids" }) || ctx.Match("capid", "capids")) return ctx.Execute(null, m => m.HidDisplayCaps(ctx)); + if (ctx.MatchMultiple(new[] { "pad" }, new[] { "id", "ids" }) || ctx.MatchMultiple(new[] { "id" }, new[] { "pad", "padding" }) || ctx.Match("idpad", "padid", "padids")) + return ctx.Execute(null, m => m.HidListPadding(ctx)); // todo: maybe add the list of configuration keys here? return ctx.Reply($"{Emojis.Error} Could not find a setting with that name. Please see `pk;commands config` for the list of possible config settings."); diff --git a/PluralKit.Bot/Commands/Config.cs b/PluralKit.Bot/Commands/Config.cs index 08b49693..05ad9aa6 100644 --- a/PluralKit.Bot/Commands/Config.cs +++ b/PluralKit.Bot/Commands/Config.cs @@ -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( 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); + } } \ No newline at end of file diff --git a/PluralKit.Bot/Commands/Lists/ContextListExt.cs b/PluralKit.Bot/Commands/Lists/ContextListExt.cs index dd641cab..e6e44cfb 100644 --- a/PluralKit.Bot/Commands/Lists/ContextListExt.cs +++ b/PluralKit.Bot/Commands/Lists/ContextListExt.cs @@ -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) { diff --git a/PluralKit.Core/Database/Migrations/43.sql b/PluralKit.Core/Database/Migrations/43.sql new file mode 100644 index 00000000..583bf9c9 --- /dev/null +++ b/PluralKit.Core/Database/Migrations/43.sql @@ -0,0 +1,6 @@ +-- database version 43 +-- add config setting for padding 5-character IDs in lists + +alter table system_config add column hid_list_padding int not null default 0; + +update info set schema_version = 43; diff --git a/PluralKit.Core/Database/Utils/DatabaseMigrator.cs b/PluralKit.Core/Database/Utils/DatabaseMigrator.cs index 1456d373..432e99c3 100644 --- a/PluralKit.Core/Database/Utils/DatabaseMigrator.cs +++ b/PluralKit.Core/Database/Utils/DatabaseMigrator.cs @@ -9,7 +9,7 @@ namespace PluralKit.Core; internal class DatabaseMigrator { private const string RootPath = "PluralKit.Core.Database"; // "resource path" root for SQL files - private const int TargetSchemaVersion = 42; + private const int TargetSchemaVersion = 43; private readonly ILogger _logger; public DatabaseMigrator(ILogger logger) diff --git a/PluralKit.Core/Models/Patch/SystemConfigPatch.cs b/PluralKit.Core/Models/Patch/SystemConfigPatch.cs index 482b5a51..26e5b13d 100644 --- a/PluralKit.Core/Models/Patch/SystemConfigPatch.cs +++ b/PluralKit.Core/Models/Patch/SystemConfigPatch.cs @@ -21,6 +21,7 @@ public class SystemConfigPatch: PatchObject public Partial ProxyErrorMessageEnabled { get; set; } public Partial HidDisplaySplit { get; set; } public Partial HidDisplayCaps { get; set; } + public Partial HidListPadding { get; set; } public override Query Apply(Query q) => q.ApplyPatch(wrapper => wrapper @@ -37,6 +38,7 @@ public class SystemConfigPatch: PatchObject .With("proxy_error_message_enabled", ProxyErrorMessageEnabled) .With("hid_display_split", HidDisplaySplit) .With("hid_display_caps", HidDisplayCaps) + .With("hid_list_padding", HidListPadding) ); public new void AssertIsValid() @@ -98,6 +100,9 @@ public class SystemConfigPatch: PatchObject if (HidDisplayCaps.IsPresent) o.Add("hid_display_caps", HidDisplayCaps.Value); + if (HidListPadding.IsPresent) + o.Add("hid_list_padding", HidListPadding.Value.ToUserString()); + return o; } diff --git a/PluralKit.Core/Models/SystemConfig.cs b/PluralKit.Core/Models/SystemConfig.cs index ed7f3dbd..dfc6ba9b 100644 --- a/PluralKit.Core/Models/SystemConfig.cs +++ b/PluralKit.Core/Models/SystemConfig.cs @@ -23,6 +23,14 @@ public class SystemConfig public bool ProxyErrorMessageEnabled { get; } public bool HidDisplaySplit { get; } public bool HidDisplayCaps { get; } + public HidPadFormat HidListPadding { get; } + + public enum HidPadFormat + { + None = 0, + Left = 1, + Right = 2, + } } public static class SystemConfigExt @@ -43,9 +51,16 @@ public static class SystemConfigExt o.Add("proxy_error_message_enabled", cfg.ProxyErrorMessageEnabled); o.Add("hid_display_split", cfg.HidDisplaySplit); o.Add("hid_display_caps", cfg.HidDisplayCaps); + o.Add("hid_list_padding", cfg.HidListPadding.ToUserString()); o.Add("description_templates", JArray.FromObject(cfg.DescriptionTemplates)); return o; } + + public static string ToUserString(this SystemConfig.HidPadFormat val) + { + if (val == SystemConfig.HidPadFormat.None) return "off"; + return val.ToString().ToLower(); + } } \ No newline at end of file