diff options
Diffstat (limited to 'src/main/kotlin/moe/nea/ledger/QueryCommand.kt')
-rw-r--r-- | src/main/kotlin/moe/nea/ledger/QueryCommand.kt | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/src/main/kotlin/moe/nea/ledger/QueryCommand.kt b/src/main/kotlin/moe/nea/ledger/QueryCommand.kt index 305069a..9967a4a 100644 --- a/src/main/kotlin/moe/nea/ledger/QueryCommand.kt +++ b/src/main/kotlin/moe/nea/ledger/QueryCommand.kt @@ -9,6 +9,7 @@ import moe.nea.ledger.database.Database import moe.nea.ledger.utils.di.Inject import net.minecraft.command.CommandBase import net.minecraft.command.ICommandSender +import net.minecraft.util.BlockPos import net.minecraft.util.ChatComponentText import net.minecraft.util.ChatStyle import net.minecraft.util.EnumChatFormatting @@ -68,6 +69,18 @@ class QueryCommand : CommandBase() { } } + override fun addTabCompletionOptions( + sender: ICommandSender, + args: Array<out String>, + pos: BlockPos + ): MutableList<String>? { + when (val p = parseArgs(args)) { + is ParseResult.MissingArg -> return null + is ParseResult.Success -> return p.lastFilterM.tabComplete(args.last()) + is ParseResult.UnknownFilter -> return getListOfStringsMatchingLastWord(args, mFilters.keys) + } + } + @Inject lateinit var database: Database private fun executeQuery(parse: ParseResult.Success) { @@ -79,9 +92,13 @@ class QueryCommand : CommandBase() { query.where(ANDExpression(value)) } query.limit(80u) + val dedup = mutableSetOf<UUIDUtil.ULIDWrapper>() query.forEach { val type = it[DBLogEntry.type] val transactionId = it[DBLogEntry.transactionId] + if (!dedup.add(transactionId)) { + return@forEach + } val timestamp = transactionId.getTimestamp() val items = DBItemEntry.selectAll(database.connection) .where(Clause { column(DBItemEntry.transactionId) eq string(transactionId.wrapped) }) @@ -144,20 +161,35 @@ class QueryCommand : CommandBase() { val preparedText = "%" + text.trim('%') + "%" return Clause { column(DBLogEntry.type) like preparedText } } + + override fun tabComplete(partialArg: String): MutableList<String> { + return TransactionType.entries.asSequence().map { it.name }.filter { partialArg in it }.toMutableList() + } } object ItemFilter : FilterM { override val name: String get() = "withitem" + private val itemIdProvider = Ledger.di.provide<ItemIdProvider>() // TODO: close this escape hatch override fun getFilter(text: String): BooleanExpression { return Clause { column(DBItemEntry.itemId) like text } } + + override fun tabComplete(partialArg: String): MutableList<String>? { + return itemIdProvider.getKnownItemIds() + .asSequence() + .map { it.string } + .filter { partialArg in it } + .take(100) + .toMutableList() + } } interface FilterM { val name: String fun getFilter(text: String): BooleanExpression + fun tabComplete(partialArg: String): MutableList<String>? // fun tabCompleteFilter() TODO } |