From 58d1d686b9950f21766f68208ee5b1b5a0644b22 Mon Sep 17 00:00:00 2001 From: Anton Tarasenko Date: Mon, 13 Mar 2023 15:33:46 +0700 Subject: [PATCH] Add auto-highlighting of commands' descriptions --- .../Features/Commands/CommandDataBuilder.uc | 39 +++++++++++++++++-- sources/Users/ACommandUserGroups.uc | 10 ++--- 2 files changed, 41 insertions(+), 8 deletions(-) diff --git a/sources/LevelAPI/Features/Commands/CommandDataBuilder.uc b/sources/LevelAPI/Features/Commands/CommandDataBuilder.uc index 03fd804..b06c8c0 100644 --- a/sources/LevelAPI/Features/Commands/CommandDataBuilder.uc +++ b/sources/LevelAPI/Features/Commands/CommandDataBuilder.uc @@ -372,12 +372,45 @@ public final function Option(BaseText longName, optional BaseText shortName) { /// Adds description to the selected sub-command / option. /// +/// Highlights parts of the description in-between "`" characters. +/// /// Does nothing if nothing is yet selected. public final function Describe(BaseText description) { - _.memory.Free(selectedDescription); - if (description != none) { - selectedDescription = description.Copy(); + local int fromIndex, toIndex; + local BaseText.Formatting keyWordFormatting; + local bool lookingForEnd; + local MutableText coloredDescription; + + if (description == none) { + return; + } + keyWordFormatting = _.text.FormattingFromColor(_.color.TextEmphasis); + coloredDescription = description.MutableCopy(); + while (true) { + if (lookingForEnd) { + toIndex = coloredDescription.IndexOf(P("`"), fromIndex + 1); + } else { + fromIndex = coloredDescription.IndexOf(P("`"), toIndex + 1); + } + if (toIndex < 0 || fromIndex < 0) { + break; + } + if (lookingForEnd) { + coloredDescription.ChangeFormatting( + keyWordFormatting, + fromIndex, + toIndex - fromIndex + 1); + lookingForEnd = false; + } else { + lookingForEnd = true; + } } + coloredDescription.Replace(P("`"), P("")); + if (lookingForEnd) { + coloredDescription.ChangeFormatting(keyWordFormatting, fromIndex); + } + _.memory.Free(selectedDescription); + selectedDescription = coloredDescription.IntoText(); } /// Sets new name of the command that caller [`CommandDataBuilder`] constructs. diff --git a/sources/Users/ACommandUserGroups.uc b/sources/Users/ACommandUserGroups.uc index 62d18ba..77c0e22 100644 --- a/sources/Users/ACommandUserGroups.uc +++ b/sources/Users/ACommandUserGroups.uc @@ -44,10 +44,10 @@ protected function BuildData(CommandDataBuilder builder) builder.ParamText(P("group_name")); builder.SubCommand(P("addplayer")); - builder.Describe(F("Adds new user to the group, specified by the player selector. Can add" + builder.Describe(P("Adds new user to the group, specified by the player selector. Can add" @ "several players at once. Allows to also optionally specify annotation (human-readable" - @ "name) that can be thought of as a {$TextEmphasis comment}. If annotation isn't" - @ "specified current nickname will be used as one.")); + @ "name) that can be thought of as a `comment`. If annotation isn't specified current" + @ "nickname will be used as one.")); builder.ParamText(P("group_name")); builder.ParamPlayers(P("player_selector")); builder.OptionalParams(); @@ -60,8 +60,8 @@ protected function BuildData(CommandDataBuilder builder) builder.ParamPlayers(P("player_selector")); builder.SubCommand(P("adduser")); - builder.Describe(F("Adds new user to the group. Allows to also optionally specify annotation" - @ "(human-readable name) that can be thought of as a {$TextEmphasis comment}.")); + builder.Describe(P("Adds new user to the group. Allows to also optionally specify annotation" + @ "(human-readable name) that can be thought of as a `comment`.")); builder.ParamText(P("group_name")); builder.ParamText(P("user_id")); builder.OptionalParams();