This repository has been archived by the owner on Oct 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #315 from TheFruxz/311-the-dsl-text-component-buil…
…der-is-inconvenient feature: upgrade unfold to new context based structure
- Loading branch information
Showing
8 changed files
with
99 additions
and
106 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 0 additions & 12 deletions
12
MoltenKT-Unfold/src/main/kotlin/de/moltenKt/unfold/MoltenContext.kt
This file was deleted.
Oops, something went wrong.
128 changes: 45 additions & 83 deletions
128
MoltenKT-Unfold/src/main/kotlin/de/moltenKt/unfold/Unfold.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,83 +1,45 @@ | ||
@file:Suppress("unused") // TODO use kotlin context API to avoid 'useless' seeming object extensions | ||
|
||
package de.moltenKt.unfold | ||
|
||
import de.moltenKt.unfold.extension.asStyledComponent | ||
import io.ktor.http.* | ||
import net.kyori.adventure.text.Component | ||
import net.kyori.adventure.text.ComponentLike | ||
import net.kyori.adventure.text.TextComponent | ||
import net.kyori.adventure.text.event.ClickEvent | ||
import net.kyori.adventure.text.event.HoverEventSource | ||
import java.io.File | ||
import java.net.URL | ||
import java.nio.file.Path | ||
|
||
fun text(build: TextComponent.Builder.() -> Unit) = | ||
Component.text().apply(build).build() | ||
|
||
fun text(componentContent: String) = | ||
componentContent.asStyledComponent | ||
|
||
fun spaceText() = Component.space() | ||
|
||
fun emptyText() = Component.empty() | ||
|
||
fun newline() = Component.newline() | ||
|
||
fun MoltenContext<HoverEventSource<*>>.text(componentContent: String) = | ||
text { text(componentContent) } | ||
|
||
fun MoltenContext<HoverEventSource<*>>.space() = | ||
text { text(" ") } | ||
|
||
fun MoltenContext<HoverEventSource<*>>.empty() = | ||
text { text("") } | ||
|
||
fun MoltenContext<HoverEventSource<*>>.newline() = | ||
text { text("\n") } | ||
|
||
operator fun TextComponent.Builder.plus(component: ComponentLike) = | ||
append(component.asComponent()) | ||
|
||
fun TextComponent.Builder.text(componentContent: String) = | ||
this + componentContent.asStyledComponent | ||
|
||
fun TextComponent.Builder.text(component: TextComponent) = | ||
this + component | ||
|
||
fun TextComponent.Builder.text(componentContent: String, modify: TextComponent.Builder.() -> Unit) = | ||
this.append(componentContent.asStyledComponent.toBuilder().apply(modify).build()) | ||
|
||
fun TextComponent.Builder.text(component: TextComponent, modify: TextComponent.Builder.() -> Unit) = | ||
this.append(component.toBuilder().apply(modify).build()) | ||
|
||
fun TextComponent.Builder.hover(eventSource: MoltenContext<HoverEventSource<*>>.() -> HoverEventSource<*>) = | ||
hoverEvent(eventSource(MoltenContext.contextOf())) | ||
|
||
fun TextComponent.Builder.click(click: MoltenContext<ClickEvent>.() -> ClickEvent) = | ||
clickEvent(click(MoltenContext.contextOf())) | ||
|
||
fun MoltenContext<ClickEvent>.url(url: String) = ClickEvent.openUrl(url) | ||
|
||
fun MoltenContext<ClickEvent>.url(url: URL) = ClickEvent.openUrl(url) | ||
|
||
fun MoltenContext<ClickEvent>.url(url: Url) = ClickEvent.openUrl(url.toString()) | ||
|
||
fun MoltenContext<ClickEvent>.file(file: String) = ClickEvent.openFile(file) | ||
|
||
fun MoltenContext<ClickEvent>.file(path: Path) = file("$path") | ||
|
||
fun MoltenContext<ClickEvent>.file(file: File) = file(file.toPath()) | ||
|
||
fun MoltenContext<ClickEvent>.run(command: String) = ClickEvent.runCommand(command) | ||
|
||
fun MoltenContext<ClickEvent>.suggest(command: String) = ClickEvent.suggestCommand(command) | ||
|
||
fun MoltenContext<ClickEvent>.toPage(page: Int) = ClickEvent.changePage(page) | ||
|
||
fun MoltenContext<ClickEvent>.toPage(page: String) = ClickEvent.changePage(page) | ||
|
||
fun MoltenContext<ClickEvent>.copy(text: String) = ClickEvent.copyToClipboard(text) | ||
|
||
fun MoltenContext<ClickEvent>.click(action: ClickEvent.Action, string: String) = ClickEvent.clickEvent(action, string) | ||
package de.moltenKt.unfold | ||
|
||
import de.moltenKt.core.extension.dump | ||
import de.moltenKt.unfold.extension.asStyledComponent | ||
import net.kyori.adventure.text.Component | ||
import net.kyori.adventure.text.ComponentLike | ||
import net.kyori.adventure.text.TextComponent | ||
import net.kyori.adventure.text.TextComponent.Builder | ||
import net.kyori.adventure.text.event.ClickEvent | ||
import net.kyori.adventure.text.event.HoverEventSource | ||
|
||
@Unfold fun buildComponent(base: TextComponent = Component.empty(), builder: Builder.() -> Unit): TextComponent = | ||
base.toBuilder().apply(builder).build() | ||
|
||
context(Builder) | ||
@Unfold operator fun String.unaryPlus(): Unit = | ||
append(this.asStyledComponent).dump() | ||
|
||
context(Builder) | ||
@Unfold operator fun ComponentLike.unaryPlus(): Builder = | ||
append(this) | ||
|
||
context(Builder) | ||
@Unfold operator fun <T : Iterable<ComponentLike>> T.unaryPlus(): Builder = | ||
append(this) | ||
|
||
context(Builder) | ||
@Unfold operator fun ClickEvent.unaryPlus(): Builder = | ||
clickEvent(this) | ||
|
||
@Unfold | ||
fun text(content: String, builder: Builder.() -> Unit = { }) = content.asStyledComponent(builder) | ||
|
||
@Unfold | ||
fun text(builder: Builder.() -> Unit) = text("", builder) | ||
|
||
@Unfold | ||
fun TextComponent.hover(process: () -> HoverEventSource<*>) = this.hoverEvent(process()) | ||
|
||
@Unfold | ||
fun Builder.hover(process: () -> HoverEventSource<*>) = this.hoverEvent(process()) | ||
|
||
@DslMarker | ||
@PublishedApi | ||
internal annotation class Unfold |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters