Added lastOrEmpty() extension function.

This commit is contained in:
Erik C. Thauvin 2021-11-28 23:24:14 -08:00
parent 10f1ee8518
commit 97a9fd1597
3 changed files with 15 additions and 16 deletions

View file

@ -36,6 +36,7 @@ import net.thauvin.erik.mobibot.Utils.appendIfMissing
import net.thauvin.erik.mobibot.Utils.bot import net.thauvin.erik.mobibot.Utils.bot
import net.thauvin.erik.mobibot.Utils.getIntProperty import net.thauvin.erik.mobibot.Utils.getIntProperty
import net.thauvin.erik.mobibot.Utils.isChannelOp import net.thauvin.erik.mobibot.Utils.isChannelOp
import net.thauvin.erik.mobibot.Utils.lastOrEmpty
import net.thauvin.erik.mobibot.Utils.sendList import net.thauvin.erik.mobibot.Utils.sendList
import net.thauvin.erik.mobibot.Utils.sendMessage import net.thauvin.erik.mobibot.Utils.sendMessage
import net.thauvin.erik.mobibot.Utils.toIsoLocalDate import net.thauvin.erik.mobibot.Utils.toIsoLocalDate
@ -106,7 +107,7 @@ import java.util.regex.Pattern
import kotlin.system.exitProcess import kotlin.system.exitProcess
@Version(properties = "version.properties", className = "ReleaseInfo", template = "version.mustache", type = "kt") @Version(properties = "version.properties", className = "ReleaseInfo", template = "version.mustache", type = "kt")
class Mobibot(nickname: String, channel: String, logsDirPath: String, p: Properties) : ListenerAdapter() { class Mobibot(nickname: String, val channel: String, logsDirPath: String, p: Properties) : ListenerAdapter() {
// The bot configuration. // The bot configuration.
private val config: Configuration private val config: Configuration
@ -116,9 +117,6 @@ class Mobibot(nickname: String, channel: String, logsDirPath: String, p: Propert
// Tell module // Tell module
private val tell: Tell private val tell: Tell
/** Main channel. */
val channel: String
/** Logger. */ /** Logger. */
val logger: Logger = LoggerFactory.getLogger(Mobibot::class.java) val logger: Logger = LoggerFactory.getLogger(Mobibot::class.java)
@ -185,9 +183,7 @@ class Mobibot(nickname: String, channel: String, logsDirPath: String, p: Propert
if (logger.isTraceEnabled) logger.trace("<<< ${user.nick}: ${event.message}") if (logger.isTraceEnabled) logger.trace("<<< ${user.nick}: ${event.message}")
val cmds = event.message.trim().split(" ".toRegex(), 2) val cmds = event.message.trim().split(" ".toRegex(), 2)
val cmd = cmds[0].lowercase() val cmd = cmds[0].lowercase()
val args = if (cmds.size > 1) { val args = cmds.lastOrEmpty().trim()
cmds[1].trim()
} else ""
if (cmd.startsWith(Constants.HELP_CMD)) { // help if (cmd.startsWith(Constants.HELP_CMD)) { // help
helpResponse(event, args) helpResponse(event, args)
} else if (!addons.exec(channel, cmd, args, event)) { // Execute command or module } else if (!addons.exec(channel, cmd, args, event)) { // Execute command or module
@ -217,9 +213,7 @@ class Mobibot(nickname: String, channel: String, logsDirPath: String, p: Propert
if (logger.isTraceEnabled) logger.trace(">>> $sender: $message") if (logger.isTraceEnabled) logger.trace(">>> $sender: $message")
val cmds = message.substring(message.indexOf(':') + 1).trim().split(" ".toRegex(), 2) val cmds = message.substring(message.indexOf(':') + 1).trim().split(" ".toRegex(), 2)
val cmd = cmds[0].lowercase() val cmd = cmds[0].lowercase()
val args = if (cmds.size > 1) { val args = cmds.lastOrEmpty().trim()
cmds[1].trim()
} else ""
if (cmd.startsWith(Constants.HELP_CMD)) { // mobibot: help if (cmd.startsWith(Constants.HELP_CMD)) { // mobibot: help
helpResponse(event, args) helpResponse(event, args)
} else { } else {
@ -354,7 +348,6 @@ class Mobibot(nickname: String, channel: String, logsDirPath: String, p: Propert
* Initialize the bot. * Initialize the bot.
*/ */
init { init {
this.channel = channel
val ircServer = p.getProperty("server", Constants.DEFAULT_SERVER) val ircServer = p.getProperty("server", Constants.DEFAULT_SERVER)
config = Configuration.Builder().apply { config = Configuration.Builder().apply {
name = nickname name = nickname

View file

@ -35,6 +35,7 @@ package net.thauvin.erik.mobibot.commands.links
import net.thauvin.erik.mobibot.Utils.bot import net.thauvin.erik.mobibot.Utils.bot
import net.thauvin.erik.mobibot.Utils.buildCmdSyntax import net.thauvin.erik.mobibot.Utils.buildCmdSyntax
import net.thauvin.erik.mobibot.Utils.helpFormat import net.thauvin.erik.mobibot.Utils.helpFormat
import net.thauvin.erik.mobibot.Utils.lastOrEmpty
import net.thauvin.erik.mobibot.Utils.sendMessage import net.thauvin.erik.mobibot.Utils.sendMessage
import net.thauvin.erik.mobibot.commands.AbstractCommand import net.thauvin.erik.mobibot.commands.AbstractCommand
import net.thauvin.erik.mobibot.commands.links.LinksMgr.Companion.entries import net.thauvin.erik.mobibot.commands.links.LinksMgr.Companion.entries
@ -77,11 +78,7 @@ class View : AbstractCommand() {
val split = query.split(" ", limit = 2) val split = query.split(" ", limit = 2)
try { try {
start = split[0].toInt() - 1 start = split[0].toInt() - 1
query = if (split.size == 2) { query = split.lastOrEmpty().trim()
split[1].trim()
} else {
""
}
if (start > entries.links.size) { if (start > entries.links.size) {
start = 0 start = 0
} }

View file

@ -46,6 +46,7 @@ import net.thauvin.erik.mobibot.Utils.encodeUrl
import net.thauvin.erik.mobibot.Utils.getIntProperty import net.thauvin.erik.mobibot.Utils.getIntProperty
import net.thauvin.erik.mobibot.Utils.green import net.thauvin.erik.mobibot.Utils.green
import net.thauvin.erik.mobibot.Utils.helpFormat import net.thauvin.erik.mobibot.Utils.helpFormat
import net.thauvin.erik.mobibot.Utils.lastOrEmpty
import net.thauvin.erik.mobibot.Utils.obfuscate import net.thauvin.erik.mobibot.Utils.obfuscate
import net.thauvin.erik.mobibot.Utils.plural import net.thauvin.erik.mobibot.Utils.plural
import net.thauvin.erik.mobibot.Utils.red import net.thauvin.erik.mobibot.Utils.red
@ -187,6 +188,14 @@ class UtilsTest {
assertThat(localDateTime.toIsoLocalDate(), "isoLocalDate(localDate)").isEqualTo("1952-02-17") assertThat(localDateTime.toIsoLocalDate(), "isoLocalDate(localDate)").isEqualTo("1952-02-17")
} }
@Test
fun testLastOrEmpty() {
val two = listOf("1", "2")
assertThat(two.lastOrEmpty(), "two").isEqualTo("2")
val one = listOf("1")
assertThat(one.lastOrEmpty(), "one").isEqualTo("")
}
@Test @Test
fun testObfuscate() { fun testObfuscate() {
assertThat(ascii.obfuscate(), "obfuscate()").all { assertThat(ascii.obfuscate(), "obfuscate()").all {