Skip to content

Commit

Permalink
add a Service Provider for ANSIComponentSerializer
Browse files Browse the repository at this point in the history
  • Loading branch information
rymiel committed Apr 10, 2023
1 parent 8844c85 commit b4f56a3
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package net.kyori.adventure.text.serializer;

import net.kyori.adventure.text.Component;
import net.kyori.adventure.util.PlatformAPI;
import net.kyori.ansi.ColorLevel;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;

import java.util.function.Consumer;

public interface ANSIComponentSerializer extends ComponentSerializer<Component, Component, String> {

static @NotNull ANSIComponentSerializer ansi() {
return ANSIComponentSerializerImpl.INSTANCE;
return ANSIComponentSerializerImpl.Instances.INSTANCE;
}

@NotNull String serialize(@NotNull Component component, @NotNull ColorLevel colorLevel);
Expand All @@ -18,4 +22,22 @@ default Component deserialize(@NotNull String input) {
throw new UnsupportedOperationException("AnsiComponentSerializer does not support deserialization");
}

/**
* A {@link ANSIComponentSerializer} service provider.
*
* @since 4.14.0
*/
@ApiStatus.Internal
@PlatformAPI
interface Provider {
/**
* Provides a {@link ANSIComponentSerializer}.
*
* @return a {@link ANSIComponentSerializer}
* @since 4.8.0
*/
@ApiStatus.Internal
@PlatformAPI
@NotNull ANSIComponentSerializer ansi();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,23 @@
import net.kyori.adventure.text.format.Style;
import net.kyori.adventure.text.format.TextColor;
import net.kyori.adventure.text.format.TextDecoration;
import net.kyori.adventure.util.Services;
import net.kyori.ansi.ANSIComponentRenderer;
import net.kyori.ansi.ColorLevel;
import net.kyori.ansi.StyleOps;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.Range;

import java.util.Optional;

public class ANSIComponentSerializerImpl implements ANSIComponentSerializer {
static final ANSIComponentSerializer INSTANCE = new ANSIComponentSerializerImpl();
private static final Optional<Provider> SERVICE = Services.service(Provider.class);
static final class Instances {
static final ANSIComponentSerializer INSTANCE = SERVICE
.map(Provider::ansi)
.orElseGet(ANSIComponentSerializerImpl::new);
}

@Override
public @NotNull String serialize(@NotNull Component component) {
Expand Down

0 comments on commit b4f56a3

Please sign in to comment.