Improved helpFormat()

This commit is contained in:
Erik C. Thauvin 2020-12-08 01:34:02 -08:00
parent d5634d7418
commit 7146bfcc60
32 changed files with 59 additions and 70 deletions

View file

@ -66,7 +66,7 @@ class FeedReader(
var i = 0
while (i < items.size && i < maxItems) {
send(sender, items[i].title, false)
send(sender, Utils.helpIndent(Utils.green(items[i].link), false), false)
send(sender, Utils.helpFormat(Utils.green(items[i].link), false), false)
i++
}
}

View file

@ -38,7 +38,7 @@ import net.thauvin.erik.mobibot.Utils.colorize
import net.thauvin.erik.mobibot.Utils.ensureDir
import net.thauvin.erik.mobibot.Utils.getIntProperty
import net.thauvin.erik.mobibot.Utils.helpFormat
import net.thauvin.erik.mobibot.Utils.helpIndent
import net.thauvin.erik.mobibot.Utils.buildCmdSyntax
import net.thauvin.erik.mobibot.Utils.isoLocalDate
import net.thauvin.erik.mobibot.Utils.today
import net.thauvin.erik.mobibot.commands.AddLog
@ -254,7 +254,7 @@ class Mobibot(nickname: String, channel: String, logsDirPath: String, p: Propert
send(sender, "For more information on a specific command, type:", isPrivate)
send(
sender,
helpIndent(helpFormat("%c ${Constants.HELP_CMD} <command>", nick, isPrivate)),
helpFormat(buildCmdSyntax("%c ${Constants.HELP_CMD} <command>", nick, isPrivate)),
isPrivate
)
send(sender, "The commands are:", isPrivate)
@ -468,11 +468,10 @@ class Mobibot(nickname: String, channel: String, logsDirPath: String, p: Propert
while (i < list.size) {
send(
nick,
helpIndent(
helpFormat(
list.subList(i, list.size.coerceAtMost(i + maxPerLine)).joinToString(" ", truncated = ""),
isBold
),
isPrivate
isBold,
isIndent isPrivate
)
i += maxPerLine
}

View file

@ -138,22 +138,12 @@ object Utils {
fun green(s: String?): String = colorize(s, Colors.DARK_GREEN)
/**
* Formats a help command by replacing `%c` with the bot's pub/priv command, and `%n` with the bot's
* nick.
*/
@JvmStatic
fun helpFormat(text: String, botNick: String, isPrivate: Boolean): String {
val replace = arrayOf(if (isPrivate) "/msg $botNick" else "$botNick:", botNick)
return StringUtils.replaceEach(text, searchFlags, replace)
}
/**
* Returns indented help string.
* Returns a formatted help string.
*/
@JvmStatic
@JvmOverloads
fun helpIndent(help: String, isBold: Boolean = true): String {
return " " + if (isBold) bold(help) else help
fun helpFormat(help: String, isBold: Boolean = true, isIndent: Boolean = true): String {
return (if (isIndent) " " else "").plus(if (isBold) bold(help) else help)
}
/**

View file

@ -38,7 +38,7 @@ import net.thauvin.erik.mobibot.Utils
class Cycle(bot: Mobibot) : AbstractCommand(bot) {
private val wait = 10
override val name = "cycle"
override val help = listOf("To have the bot leave the channel and come back:", Utils.helpIndent("%c $name"))
override val help = listOf("To have the bot leave the channel and come back:", Utils.helpFormat("%c $name"))
override val isOp = true
override val isPublic = false
override val isVisible = true

View file

@ -47,17 +47,17 @@ class Ignore(bot: Mobibot) : AbstractCommand(bot) {
override val name = IGNORE_CMD
override val help = listOf(
"To ignore a link posted to the channel:",
Utils.helpIndent("https://www.foo.bar %n"),
Utils.helpFormat("https://www.foo.bar %n"),
"To check your ignore status:",
Utils.helpIndent("%c $name"),
Utils.helpFormat("%c $name"),
"To toggle your ignore status:",
Utils.helpIndent("%c $name $me")
Utils.helpFormat("%c $name $me")
)
private val helpOp = listOf(
"To ignore a link posted to the channel:",
Utils.helpIndent("https://www.foo.bar " + Utils.bold("%n"), false),
Utils.helpFormat("https://www.foo.bar " + Utils.bold("%n"), false),
"To add/remove nicks from the ignored list:",
Utils.helpIndent("%c $name <nick> [<nick> ...]")
Utils.helpFormat("%c $name <nick> [<nick> ...]")
)
override val isOp = false
@ -99,7 +99,7 @@ class Ignore(bot: Mobibot) : AbstractCommand(bot) {
): Boolean {
return if (isOp) {
for (h in helpOp) {
bot.send(sender, Utils.helpFormat(h, bot.nick, isPrivate), isPrivate)
bot.send(sender, Utils.buildCmdSyntax(h, bot.nick, isPrivate), isPrivate)
}
true
} else {

View file

@ -61,7 +61,7 @@ public class Info extends AbstractCommand {
@NotNull
@Override
public List<String> getHelp() {
return List.of("To view information about the bot:", Utils.helpIndent("%c " + getName()));
return List.of("To view information about the bot:", Utils.helpFormat("%c " + getName()));
}
@Override

View file

@ -39,7 +39,7 @@ class Me(bot: Mobibot) : AbstractCommand(bot) {
override val name = "me"
override val help = listOf(
"To have the bot perform an action:",
Utils.helpIndent("%c $name <action>")
Utils.helpFormat("%c $name <action>")
)
override val isOp = true
override val isPublic = false

View file

@ -39,7 +39,7 @@ class Modules(bot: Mobibot) : AbstractCommand(bot) {
override val name = "modules"
override val help = listOf(
"To view a list of enabled modules:",
Utils.helpIndent("%c $name")
Utils.helpFormat("%c $name")
)
override val isOp = true
override val isPublic = false

View file

@ -39,7 +39,7 @@ class Msg(bot: Mobibot) : AbstractCommand(bot) {
override val name = "msg"
override val help = listOf(
"To have the bot send a private message to someone:",
Utils.helpIndent("%c $name <nick> <text>")
Utils.helpFormat("%c $name <nick> <text>")
)
override val isOp = true
override val isPublic = true

View file

@ -39,7 +39,7 @@ class Nick(bot: Mobibot) : AbstractCommand(bot) {
override val name = "nick"
override val help = listOf(
"To change the bot's nickname:",
Utils.helpIndent("%c $name <nick>")
Utils.helpFormat("%c $name <nick>")
)
override val isOp = true
override val isPublic = true

View file

@ -41,7 +41,7 @@ class Recap(bot: Mobibot) : AbstractCommand(bot) {
override val name = "recap"
override val help = listOf(
"To list the last 10 public channel messages:",
Utils.helpIndent("%c $name")
Utils.helpFormat("%c $name")
)
override val isOp = false
override val isPublic = true

View file

@ -39,7 +39,7 @@ class Say(bot: Mobibot) : AbstractCommand(bot) {
override val name = "say"
override val help = listOf(
"To have the bot say something on the channel:",
Utils.helpIndent("%c $name <text>")
Utils.helpFormat("%c $name <text>")
)
override val isOp = true
override val isPublic = false

View file

@ -40,7 +40,7 @@ class Users(bot: Mobibot) : AbstractCommand(bot) {
override val name = "users"
override val help = listOf(
"To list the users present on the channel:",
Utils.helpIndent("%c $name")
Utils.helpFormat("%c $name")
)
override val isOp = false
override val isPublic = true

View file

@ -61,7 +61,7 @@ public class Versions extends AbstractCommand {
@NotNull
@Override
public List<String> getHelp() {
return List.of("To view the versions data (bot, platform, java, etc.):", Utils.helpIndent("%c " + getName()));
return List.of("To view the versions data (bot, platform, java, etc.):", Utils.helpFormat("%c " + getName()));
}
@Override

View file

@ -43,12 +43,12 @@ class Comment(bot: Mobibot) : AbstractCommand(bot) {
override val name = COMMAND
override val help = listOf(
"To add a comment:",
Utils.helpIndent("${Constants.LINK_CMD}1:This is a comment"),
Utils.helpFormat("${Constants.LINK_CMD}1:This is a comment"),
"I will reply with a label, for example: ${Utils.bold(Constants.LINK_CMD)}1.1",
"To edit a comment, use its label: ",
Utils.helpIndent("${Constants.LINK_CMD}1.1:This is an edited comment"),
Utils.helpFormat("${Constants.LINK_CMD}1.1:This is an edited comment"),
"To delete a comment, use its label and a minus sign: ",
Utils.helpIndent("${Constants.LINK_CMD}1.1:-")
Utils.helpFormat("${Constants.LINK_CMD}1.1:-")
)
override val isOp = false
override val isPublic = true
@ -98,7 +98,7 @@ class Comment(bot: Mobibot) : AbstractCommand(bot) {
bot.send(sender, "To change a comment's author:", isPrivate)
bot.send(
sender,
Utils.helpIndent("${Constants.LINK_CMD}1.1:?<nick>"),
Utils.helpFormat("${Constants.LINK_CMD}1.1:?<nick>"),
isPrivate
)
}

View file

@ -144,7 +144,7 @@ class LinksMgr(bot: Mobibot) : AbstractCommand(bot) {
bot.send(sender, "Please specify a title, by typing:", isPrivate)
bot.send(
sender,
Utils.helpIndent("${EntriesUtils.buildLinkCmd(index)}:|This is the title"),
Utils.helpFormat("${EntriesUtils.buildLinkCmd(index)}:|This is the title"),
isPrivate
)
}

View file

@ -44,15 +44,15 @@ class Posting(bot: Mobibot) : AbstractCommand(bot) {
override val name = "posting"
override val help = listOf(
"Post a URL, by saying it on a line on its own:",
Utils.helpIndent("<url> [<title>] ${Tags.COMMAND}: <+tag> [...]]"),
Utils.helpFormat("<url> [<title>] ${Tags.COMMAND}: <+tag> [...]]"),
"I will reply with a label, for example: ${Utils.bold(Constants.LINK_CMD)}1",
"To add a title, use its label and a pipe:",
Utils.helpIndent("${Constants.LINK_CMD}1:|This is the title"),
Utils.helpFormat("${Constants.LINK_CMD}1:|This is the title"),
"To add a comment:",
Utils.helpIndent("${Constants.LINK_CMD}1:This is a comment"),
Utils.helpFormat("${Constants.LINK_CMD}1:This is a comment"),
"I will reply with a label, for example: ${Utils.bold(Constants.LINK_CMD)}1.1",
"To edit a comment, see: ",
Utils.helpIndent("%c ${Constants.HELP_CMD} ${Comment.COMMAND}")
Utils.helpFormat("%c ${Constants.HELP_CMD} ${Comment.COMMAND}")
)
override val isOp = false
override val isPublic = true

View file

@ -43,7 +43,7 @@ class Tags(bot: Mobibot) : AbstractCommand(bot) {
override val name = COMMAND
override val help = listOf(
"To categorize or tag a URL, use its label and a T:",
Utils.helpIndent("${Constants.LINK_CMD}1T:<+tag|-tag> [...]")
Utils.helpFormat("${Constants.LINK_CMD}1T:<+tag|-tag> [...]")
)
override val isOp = false
override val isPublic = true

View file

@ -44,7 +44,7 @@ class View(bot: Mobibot) : AbstractCommand(bot) {
override val name = VIEW_CMD
override val help = listOf(
"To list or search the current URL posts:",
Utils.helpIndent("%c $name [<start>] [<query>]")
Utils.helpFormat("%c $name [<start>] [<query>]")
)
override val isOp = false
override val isPublic = true

View file

@ -118,9 +118,9 @@ class Tell(bot: Mobibot) : AbstractCommand(bot) {
override val help: List<String>
get() = listOf(
"To send a message to someone when they join the channel:",
helpIndent("%c $name <nick> <message>"),
helpFormat("%c $name <nick> <message>"),
"To view queued and sent messages:",
helpIndent("%c $name ${View.VIEW_CMD}"),
helpFormat("%c $name ${View.VIEW_CMD}"),
"Messages are kept for " + bold(maxDays)
+ plural(maxDays.toLong(), " day.", " days.")
)
@ -293,7 +293,7 @@ class Tell(bot: Mobibot) : AbstractCommand(bot) {
isPrivate
)
}
bot.send(sender, helpIndent(message.message), isPrivate)
bot.send(sender, helpFormat(message.message), isPrivate)
}
}
if (!hasMessage) {
@ -302,8 +302,8 @@ class Tell(bot: Mobibot) : AbstractCommand(bot) {
bot.send(sender, "To delete one or all delivered messages:", isPrivate)
bot.send(
sender,
helpIndent(
helpFormat(
buildCmdSyntax(
"%c $name $TELL_DEL_KEYWORD <id|$TELL_ALL_KEYWORD>",
bot.nick,
isPrivate

View file

@ -108,16 +108,16 @@ class CurrencyConverter(bot: Mobibot) : ThreadedModule(bot) {
send(sender, "To convert from one currency to another:", isPrivate)
send(
sender,
Utils.helpIndent(
Utils.helpFormat("%c $CURRENCY_CMD 100 USD to EUR", nick, isPrivateMsgEnabled)
Utils.helpFormat(
Utils.buildCmdSyntax("%c $CURRENCY_CMD 100 USD to EUR", nick, isPrivateMsgEnabled)
),
isPrivate
)
send(sender, "For a listing of current rates:", isPrivate)
send(
sender,
Utils.helpIndent(
Utils.helpFormat("%c $CURRENCY_CMD $CURRENCY_RATES_KEYWORD", nick, isPrivateMsgEnabled)
Utils.helpFormat(
Utils.buildCmdSyntax("%c $CURRENCY_CMD $CURRENCY_RATES_KEYWORD", nick, isPrivateMsgEnabled)
),
isPrivate
)

View file

@ -98,6 +98,6 @@ class Dice(bot: Mobibot) : AbstractModule(bot) {
init {
commands.add(DICE_CMD)
help.add("To roll the dice:")
help.add(Utils.helpIndent("%c $DICE_CMD"))
help.add(Utils.helpFormat("%c $DICE_CMD"))
}
}

View file

@ -101,7 +101,7 @@ class GoogleSearch(bot: Mobibot) : ThreadedModule(bot) {
for (i in 0 until ja.length()) {
val j = ja.getJSONObject(i)
results.add(NoticeMessage(Utils.unescapeXml(j.getString("title"))))
results.add(NoticeMessage(Utils.helpIndent(j.getString("link"), false), Colors.DARK_GREEN))
results.add(NoticeMessage(Utils.helpFormat(j.getString("link"), false), Colors.DARK_GREEN))
}
} catch (e: IOException) {
throw ModuleException("searchGoogle($query)", "An IO error has occurred searching Google.", e)
@ -118,7 +118,7 @@ class GoogleSearch(bot: Mobibot) : ThreadedModule(bot) {
init {
commands.add(GOOGLE_CMD)
help.add("To search Google:")
help.add(Utils.helpIndent("%c $GOOGLE_CMD <query>"))
help.add(Utils.helpFormat("%c $GOOGLE_CMD <query>"))
initProperties(GOOGLE_API_KEY_PROP, GOOGLE_CSE_KEY_PROP)
}
}

View file

@ -97,6 +97,6 @@ class Joke(bot: Mobibot) : ThreadedModule(bot) {
init {
commands.add(JOKE_CMD)
help.add("To retrieve a random joke:")
help.add(Utils.helpIndent("%c $JOKE_CMD"))
help.add(Utils.helpFormat("%c $JOKE_CMD"))
}
}

View file

@ -164,6 +164,6 @@ class Lookup(bot: Mobibot) : AbstractModule(bot) {
init {
commands.add(LOOKUP_CMD)
help.add("To perform a DNS lookup query:")
help.add(Utils.helpIndent("%c $LOOKUP_CMD <ip address or hostname>"))
help.add(Utils.helpFormat("%c $LOOKUP_CMD <ip address or hostname>"))
}
}

View file

@ -85,6 +85,6 @@ class Ping(bot: Mobibot) : AbstractModule(bot) {
init {
commands.add(PING_CMD)
help.add("To ping the bot:")
help.add(Utils.helpIndent("%c $PING_CMD"))
help.add(Utils.helpFormat("%c $PING_CMD"))
}
}

View file

@ -51,7 +51,7 @@ class RockPaperScissors(bot: Mobibot) : AbstractModule(bot) {
with(help) {
add("To play Rock Paper Scissors:")
add(
Utils.helpIndent(
Utils.helpFormat(
"%c ${Hands.ROCK.name.toLowerCase()} | ${Hands.PAPER.name.toLowerCase()}"
+ " | ${Hands.SCISSORS.name.toLowerCase()}"
)

View file

@ -206,7 +206,7 @@ class StockQuote(bot: Mobibot) : ThreadedModule(bot) {
init {
commands.add(STOCK_CMD)
help.add("To retrieve a stock quote:")
help.add(Utils.helpIndent("%c $STOCK_CMD <symbol|keywords>"))
help.add(Utils.helpFormat("%c $STOCK_CMD <symbol|keywords>"))
initProperties(ALPHAVANTAGE_API_KEY_PROP)
}
}

View file

@ -234,7 +234,7 @@ class Twitter(bot: Mobibot) : ThreadedModule(bot) {
init {
commands.add(TWITTER_CMD)
help.add("To post to Twitter:")
help.add(Utils.helpIndent("%c $TWITTER_CMD <message>"))
help.add(Utils.helpFormat("%c $TWITTER_CMD <message>"))
properties[AUTOPOST_PROP] = "false"
initProperties(CONSUMER_KEY_PROP, CONSUMER_SECRET_PROP, HANDLE_PROP, TOKEN_PROP, TOKEN_SECRET_PROP)
}

View file

@ -65,7 +65,7 @@ public final class War extends AbstractModule {
commands.add(WAR_CMD);
help.add("To play war:");
help.add(Utils.helpIndent("%c " + WAR_CMD));
help.add(Utils.helpFormat("%c " + WAR_CMD));
}
/**

View file

@ -191,9 +191,9 @@ class Weather2(bot: Mobibot) : ThreadedModule(bot) {
init {
commands.add(WEATHER_CMD)
help.add("To display weather information:")
help.add(Utils.helpIndent("%c $WEATHER_CMD <city> [, <country code>]"))
help.add(Utils.helpFormat("%c $WEATHER_CMD <city> [, <country code>]"))
help.add("For example:")
help.add(Utils.helpIndent("%c $WEATHER_CMD paris, fr"))
help.add(Utils.helpFormat("%c $WEATHER_CMD paris, fr"))
help.add("The default ISO 3166 country code is ${Utils.bold("US")}. Zip codes supported in most countries.")
initProperties(OWM_API_KEY_PROP)
}

View file

@ -211,9 +211,9 @@ class WorldTime(bot: Mobibot) : AbstractModule(bot) {
init {
help.add("To display a country's current date/time:")
help.add(Utils.helpIndent("%c $TIME_CMD") + " [<country code>]")
help.add(Utils.helpFormat("%c $TIME_CMD") + " [<country code>]")
help.add("For a listing of the supported countries:")
help.add(Utils.helpIndent("%c $TIME_CMD"))
help.add(Utils.helpFormat("%c $TIME_CMD"))
commands.add(TIME_CMD)
}
}