From 5f1ca51e607be17c4f7dabfc73878949758a8b05 Mon Sep 17 00:00:00 2001 From: Kirill Romanov Date: Sun, 8 Nov 2020 18:19:26 +0300 Subject: [PATCH] Add builder-style dsl for text sources --- CHANGELOG.md | 3 + .../utils/formatting/EntitiesBuilder.kt | 128 ++++++++++++++++++ 2 files changed, 131 insertions(+) create mode 100644 tgbotapi.extensions.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/formatting/EntitiesBuilder.kt diff --git a/CHANGELOG.md b/CHANGELOG.md index 69d0590566..2f56a0ad11 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,9 @@ ## 0.30.1 +* `Utils`: + * Builder-style DSL for text sources - `buildEntities` + ## 0.30.0 Bot API 5.0 **THIS UPDATE CONTAINS A LOT OF BREAKING CHANGES. PLEASE, BE CAREFUL ON UPGRADING OF YOUR PROJECT** diff --git a/tgbotapi.extensions.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/formatting/EntitiesBuilder.kt b/tgbotapi.extensions.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/formatting/EntitiesBuilder.kt new file mode 100644 index 0000000000..c2b2291cd7 --- /dev/null +++ b/tgbotapi.extensions.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/formatting/EntitiesBuilder.kt @@ -0,0 +1,128 @@ +package dev.inmo.tgbotapi.extensions.utils.formatting + +import dev.inmo.tgbotapi.CommonAbstracts.TextSource +import dev.inmo.tgbotapi.types.User + +fun buildEntities(init: EntitiesBuilder.() -> Unit): List { + val builder = EntitiesBuilder() + builder.init() + return builder.build() +} + +class EntitiesBuilder internal constructor( + private val entitiesList: MutableList = mutableListOf() +) { + fun build(): List { + return entitiesList + } + + fun add(source: TextSource) { + entitiesList.add(source) + } + + operator fun String.unaryPlus() { + add(dev.inmo.tgbotapi.types.MessageEntity.textsources.regular(this)) + } +} + +fun EntitiesBuilder.bold(parts: List) = add(dev.inmo.tgbotapi.types.MessageEntity.textsources.bold(parts)) + +fun EntitiesBuilder.bold(vararg parts: TextSource) = + add(dev.inmo.tgbotapi.types.MessageEntity.textsources.bold(*parts)) + +fun EntitiesBuilder.bold(text: String) = add(dev.inmo.tgbotapi.types.MessageEntity.textsources.bold(text)) + +fun EntitiesBuilder.botCommand(command: String) = + add(dev.inmo.tgbotapi.types.MessageEntity.textsources.botCommand(command)) + +fun EntitiesBuilder.cashTag(parts: List) = + add(dev.inmo.tgbotapi.types.MessageEntity.textsources.cashTag(parts)) + +fun EntitiesBuilder.cashTag(vararg parts: TextSource) = + add(dev.inmo.tgbotapi.types.MessageEntity.textsources.cashTag(*parts)) + +fun EntitiesBuilder.cashTag(text: String) = add(dev.inmo.tgbotapi.types.MessageEntity.textsources.cashTag(text)) + +fun EntitiesBuilder.code(code: String) = add(dev.inmo.tgbotapi.types.MessageEntity.textsources.code(code)) + +fun EntitiesBuilder.email(parts: List) = + add(dev.inmo.tgbotapi.types.MessageEntity.textsources.email(parts)) + +fun EntitiesBuilder.email(vararg parts: TextSource) = + add(dev.inmo.tgbotapi.types.MessageEntity.textsources.email(*parts)) + +fun EntitiesBuilder.email(emailAddress: String) = + add(dev.inmo.tgbotapi.types.MessageEntity.textsources.email(emailAddress)) + +fun EntitiesBuilder.hashtag(parts: List) = + add(dev.inmo.tgbotapi.types.MessageEntity.textsources.hashtag(parts)) + +fun EntitiesBuilder.hashtag(vararg parts: TextSource) = + add(dev.inmo.tgbotapi.types.MessageEntity.textsources.hashtag(*parts)) + +fun EntitiesBuilder.hashtag(hashtag: String) = add(dev.inmo.tgbotapi.types.MessageEntity.textsources.hashtag(hashtag)) + +fun EntitiesBuilder.italic(parts: List) = + add(dev.inmo.tgbotapi.types.MessageEntity.textsources.italic(parts)) + +fun EntitiesBuilder.italic(vararg parts: TextSource) = + add(dev.inmo.tgbotapi.types.MessageEntity.textsources.italic(*parts)) + +fun EntitiesBuilder.italic(text: String) = add(dev.inmo.tgbotapi.types.MessageEntity.textsources.italic(text)) + +fun EntitiesBuilder.mention(parts: List) = + add(dev.inmo.tgbotapi.types.MessageEntity.textsources.mention(parts)) + +fun EntitiesBuilder.mention(vararg parts: TextSource) = + add(dev.inmo.tgbotapi.types.MessageEntity.textsources.mention(*parts)) + +fun EntitiesBuilder.mention(whoToMention: String) = + add(dev.inmo.tgbotapi.types.MessageEntity.textsources.mention(whoToMention)) + +fun EntitiesBuilder.mention(parts: List, user: User) = + add(dev.inmo.tgbotapi.types.MessageEntity.textsources.mention(parts, user)) + +fun EntitiesBuilder.mention(user: User, vararg parts: TextSource) = + add(dev.inmo.tgbotapi.types.MessageEntity.textsources.mention(user, *parts)) + +fun EntitiesBuilder.mention(text: String, user: User) = + add(dev.inmo.tgbotapi.types.MessageEntity.textsources.mention(text, user)) + +fun EntitiesBuilder.phone(parts: List) = + add(dev.inmo.tgbotapi.types.MessageEntity.textsources.phone(parts)) + +fun EntitiesBuilder.phone(vararg parts: TextSource) = + add(dev.inmo.tgbotapi.types.MessageEntity.textsources.phone(*parts)) + +fun EntitiesBuilder.phone(number: String) = + add(dev.inmo.tgbotapi.types.MessageEntity.textsources.phone(number)) + +fun EntitiesBuilder.pre(code: String, language: String?) = + add(dev.inmo.tgbotapi.types.MessageEntity.textsources.pre(code, language)) + +fun EntitiesBuilder.regular(text: String) = + add(dev.inmo.tgbotapi.types.MessageEntity.textsources.regular(text)) + +fun EntitiesBuilder.strikethrough(parts: List) = + add(dev.inmo.tgbotapi.types.MessageEntity.textsources.strikethrough(parts)) + +fun EntitiesBuilder.strikethrough(vararg parts: TextSource) = + add(dev.inmo.tgbotapi.types.MessageEntity.textsources.strikethrough(*parts)) + +fun EntitiesBuilder.strikethrough(text: String) = + add(dev.inmo.tgbotapi.types.MessageEntity.textsources.strikethrough(text)) + +fun EntitiesBuilder.link(text: String, url: String) = + add(dev.inmo.tgbotapi.types.MessageEntity.textsources.link(text, url)) + +fun EntitiesBuilder.link(url: String) = + add(dev.inmo.tgbotapi.types.MessageEntity.textsources.link(url)) + +fun EntitiesBuilder.underline(parts: List) = + add(dev.inmo.tgbotapi.types.MessageEntity.textsources.underline(parts)) + +fun EntitiesBuilder.underline(vararg parts: TextSource) = + add(dev.inmo.tgbotapi.types.MessageEntity.textsources.underline(*parts)) + +fun EntitiesBuilder.underline(text: String) = + add(dev.inmo.tgbotapi.types.MessageEntity.textsources.underline(text))