All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.github.freya022.botcommands.api.pagination.menu.RowPrefixSupplier.kt Maven / Gradle / Ivy

Go to download

A Kotlin-first (and Java) framework that makes creating Discord bots a piece of cake, using the JDA library.

There is a newer version: 3.0.0-alpha.18
Show newest version
package io.github.freya022.botcommands.api.pagination.menu

import kotlin.math.floor
import kotlin.math.log10

fun interface RowPrefixSupplier {
    fun apply(entryNum: Int, maxEntries: Int): String

    companion object {
        @JvmStatic
        val paddedNumberPrefix = RowPrefixSupplier { entryNum: Int, maxEntry: Int ->
            val spaces = getPadding(entryNum, maxEntry)
            "`" + " ".repeat(spaces) + entryNum + ".` "
        }

        /**
         * Returns the padding needed between this entry number and the maximum entry number
         *
         * @param entryNum The current entry number
         * @param maxEntry The maximum entry number
         *
         * @return The number of padding spaces needed
         */
        @JvmStatic
        fun getPadding(entryNum: Int, maxEntry: Int): Int {
            require(entryNum > 0)

            val entryDigits = floor(log10(entryNum.toDouble()) + 1)
            val maxEntryDigits = floor(log10(maxEntry.toDouble()) + 1)
            return (maxEntryDigits - entryDigits).toInt()
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy