Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,11 @@ I got tired of using Minecraft's `Text` objects like huge builders (e.g `Text.li

There are a few placeholders that you need to know about:

- `{}` - Resets the current color and style to the default one.
- `${}` - Sets the text color to the given variable color (e.g `${red}`, `${cyan}`, check [Minecraft Color Codes](https://minecraft.tools/en/color-code.php)).
- `@{}` - Alternative for `${}`. Use this one if you're on Kotlin.
- `#{}` - Sets the text color to the given hex value (e.g `#{fff}`, `#{ff00ff}`).
- `{}` - Resets the current color and style to the default one.
- `{var}` - Sets the text color to the given variable color (e.g `{red}`, `{cyan}`, check [Minecraft Color Codes](https://minecraft.tools/en/color-code.php)).
- `{#hex}` - Sets the text color to the given hex value (e.g `{#fff}`, `{#ff00ff}`).
- `+obius` modifiers - Sets the modifier of the text. You can also combine modifiers,
e.g `${+bi}` - ***bold + italic***.
e.g `{+bi}` - ***bold + italic***.
- o - [obfuscated text](images/obfuscated.gif)
- b - **bold text**
- i - *italic text*
Expand Down Expand Up @@ -53,10 +52,10 @@ import dev.cattyn.catformat.fabric.FabricCatFormat;
import java.awt.Color;
import java.util.Random;

public static final FabricCatFormat formatter = // you can create only one static instance
new FabricCatFormat().addVanilla(); // of the Formatter and use it everywhere!
// you can create only one static instance of the Formatter and use it everywhere!
FabricCatFormat formatter = new FabricCatFormat();

void func() {
void main() {
formatter.format("${red} Hello world!"); // red colored 'Hello world!'
formatter.format("#{f0f} Hello world!"); // magenta colored 'Hello world!'
formatter.format("#{0000ff} Hello world!"); // blue colored 'Hello world!'
Expand Down
58 changes: 18 additions & 40 deletions catformat-core/src/main/java/dev/cattyn/catformat/CatFormat.java
Original file line number Diff line number Diff line change
@@ -1,62 +1,40 @@
package dev.cattyn.catformat;

import dev.cattyn.catformat.formatter.FormatEntry;
import dev.cattyn.catformat.formatter.Formatter;
import dev.cattyn.catformat.stylist.Stylist;
import dev.cattyn.catformat.stylist.impl.ClassStylist;
import dev.cattyn.catformat.entry.EntryContainer;
import dev.cattyn.catformat.entry.FormatEntry;
import dev.cattyn.catformat.text.TextWrapper;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Supplier;

public class CatFormat<T> {
private static final FormatEntry NULL_FORMAT = new FormatEntry("null", () -> -1);
private final List<FormatEntry> formats = new ArrayList<>();
private final TextWrapper<T> wrapper;
public interface CatFormat<T> {
EntryContainer entries();

private Stylist<?> stylist = new ClassStylist();
TextWrapper<T> wrapper();

public CatFormat(TextWrapper<T> wrapper) {
this.wrapper = wrapper;
}
T format(String s);

public FormatEntry getEntry(String name) {
for (FormatEntry format : formats) {
if (format.name().equalsIgnoreCase(name))
return format;
}
return NULL_FORMAT;
default T format(String s, Object... o) {
return format(s.formatted(o));
}

public CatFormat<T> add(Object o) {
formats.addAll(stylist.getEntries0(o));
default CatFormat<T> styled(Consumer<EntryContainer> consumer) {
consumer.accept(entries());
return this;
}

public CatFormat<T> add(String name, int color) {
return add(name, () -> color);
}

public CatFormat<T> add(String name, Supplier<Integer> color) {
formats.add(new FormatEntry(name, color));
default CatFormat<T> add(String name, Supplier<Integer> color) {
entries().add(new FormatEntry(name, color));
return this;
}

public T format(String s, Object... objects) {
return format(s.formatted(objects));
}

public T format(String s) {
return new Formatter<>(this, s).handle();
}

public CatFormat<T> stylist(Stylist<?> stylist) {
this.stylist = stylist;
default CatFormat<T> add(Object o) {
EntryContainer entries = entries();
entries.stylist().getEntries0(o).forEach(entries::add);
return this;
}

public TextWrapper<T> getWrapper() {
return wrapper;
default CatFormat<T> add(String name, int color) {
return add(name, () -> color);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package dev.cattyn.catformat;

import dev.cattyn.catformat.entry.EntryContainer;
import dev.cattyn.catformat.entry.EntryContainerImpl;
import dev.cattyn.catformat.text.TextWrapper;

public class CatFormatImpl<T> implements CatFormat<T> {
private final EntryContainer container = new EntryContainerImpl();
private final TextWrapper<T> wrapper;

public CatFormatImpl(TextWrapper<T> wrapper) {
this.wrapper = wrapper;
}

@Override
public EntryContainer entries() {
return container;
}

@Override
public TextWrapper<T> wrapper() {
return wrapper;
}

public T format(String s) {
return new Formatter<>(this, s).handle();
}
}
Original file line number Diff line number Diff line change
@@ -1,46 +1,39 @@
package dev.cattyn.catformat.formatter;
package dev.cattyn.catformat;

import dev.cattyn.catformat.CatFormat;
import dev.cattyn.catformat.parser.HexParser;
import dev.cattyn.catformat.parser.Parser;
import dev.cattyn.catformat.parser.NameParser;
import dev.cattyn.catformat.text.Modifier;
import dev.cattyn.catformat.text.TextStyle;
import dev.cattyn.catformat.text.TextWrapper;
import dev.cattyn.catformat.utils.ChunkType;
import dev.cattyn.catformat.utils.StringUtils;

import java.util.EnumSet;
import java.util.Map;
import java.util.Set;
import java.util.Stack;

import static dev.cattyn.catformat.utils.Constants.*;

public class Formatter<T> {
private static final Map<Character, Parser> PARSER_MAP = Map.of(
HEX_TYPE, new HexParser(),
NAME_TYPE, new NameParser(),
NAME_TYPE_ALT, new NameParser()
);

private final Set<Modifier> modifiers = EnumSet.noneOf(Modifier.class);

private final StringBuilder expr = new StringBuilder();
private final StringBuilder chunk = new StringBuilder();

private final CatFormat<T> catFormat;
private final Stack<TextStyle> styles = new Stack<>();

private final CatFormatImpl<T> catFormat;
private final String target;
private final TextWrapper<T> wrapper;
private ChunkType type = ChunkType.TEXT;
private T core;

private Parser parser = null;
private int color = 0xFFFFFF;
private int modifiers;

private char lastOpcode;

public Formatter(CatFormat<T> catFormat, String target) {
public Formatter(CatFormatImpl<T> catFormat, String target) {
this.catFormat = catFormat;
this.target = target;
this.wrapper = catFormat.getWrapper();
this.wrapper = catFormat.wrapper();
this.core = this.wrapper.newText();
}

Expand All @@ -65,19 +58,13 @@ private void handleText(char opcode) {
return;
}

// garbage begin
if (opcode == BEGIN_EXPR) {
Parser parser = PARSER_MAP.get(lastOpcode);
type = ChunkType.EXPR;
if (parser != null) {
StringUtils.shrink(chunk);
}
concat();
modifiers.clear();
this.parser = parser;
modifiers = 0;
parser = null;
return;
}
// garbage end

chunk.append(opcode);
}
Expand All @@ -92,6 +79,16 @@ private void handleExpr(char opcode) {
return;
}

if (lastOpcode == BEGIN_EXPR
&& opcode == HEX_TYPE) {
parser = new HexParser();
return;
}

if (parser == null) {
parser = new NameParser();
}

expr.append(opcode);
}

Expand All @@ -100,7 +97,7 @@ private void handleMod(char opcode) {
return;
}

Modifier.from(opcode).ifPresent(modifiers::add);
Modifier.from(opcode).ifPresent(mod -> modifiers = mod.with(modifiers));
}

private void handleEscape(char opcode) {
Expand All @@ -127,19 +124,23 @@ private boolean endExpr(char c) {
if (c != END_EXPR) return false;
type = ChunkType.ESCAPE;

if (colored()) {
color = parser.getColor(catFormat, expr.toString());
if (parser != null) {
int color = parser.getColor(catFormat.entries(), expr.toString());
styles.push(new TextStyle(color, modifiers));
} else {
styles.pop();
}
StringUtils.clear(expr);
return true;
}

private T build(StringBuilder chunk) {
T built = wrapper.newText(chunk.toString());
if (colored()) {
built = wrapper.colored(built, color);
if (!styles.isEmpty()) {
TextStyle style = styles.peek();
built = wrapper.colored(built, style.color());
built = wrapper.modify(built, style.modifiers());
}
built = wrapper.modify(built, EnumSet.copyOf(modifiers));
StringUtils.clear(chunk);
return built;
}
Expand All @@ -150,7 +151,4 @@ private void concat() {
}
}

private boolean colored() {
return parser != null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package dev.cattyn.catformat.entry;

import dev.cattyn.catformat.stylist.Stylist;

public interface EntryContainer {
FormatEntry NULL_FORMAT = new FormatEntry("null", () -> -1);

void add(FormatEntry entry);

FormatEntry get(String name);

Stylist<?> stylist();

void stylist(Stylist<?> stylist);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package dev.cattyn.catformat.entry;

import dev.cattyn.catformat.stylist.Stylist;
import dev.cattyn.catformat.stylist.impl.ClassStylist;

import java.util.ArrayList;
import java.util.List;

public class EntryContainerImpl implements EntryContainer{
private final List<FormatEntry> formats = new ArrayList<>();
private Stylist<?> stylist = new ClassStylist();

@Override
public void add(FormatEntry entry) {
formats.add(entry);
}

@Override
public FormatEntry get(String name) {
for (FormatEntry format : formats) {
if (format.name().equalsIgnoreCase(name))
return format;
}
return NULL_FORMAT;
}

@Override
public Stylist<?> stylist() {
return stylist;
}

@Override
public void stylist(Stylist<?> stylist) {
this.stylist = stylist;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package dev.cattyn.catformat.formatter;
package dev.cattyn.catformat.entry;

import java.util.function.Supplier;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package dev.cattyn.catformat.parser;

import dev.cattyn.catformat.CatFormat;
import dev.cattyn.catformat.entry.EntryContainer;

public class HexParser implements Parser {
@Override
public int getColor(CatFormat<?> format, String expr) {
public int getColor(EntryContainer entries, String expr) {
if (expr.length() == 3) {
// css color support
char[] c = expr.toCharArray();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package dev.cattyn.catformat.parser;

import dev.cattyn.catformat.CatFormat;
import dev.cattyn.catformat.entry.EntryContainer;

public class NameParser implements Parser {
@Override
public int getColor(CatFormat<?> format, String expr) {
return format.getEntry(expr).getColor();
public int getColor(EntryContainer entries, String expr) {
return entries.get(expr).getColor();
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package dev.cattyn.catformat.parser;

import dev.cattyn.catformat.CatFormat;
import dev.cattyn.catformat.entry.EntryContainer;

public interface Parser {
int getColor(CatFormat<?> format, String expr);
int getColor(EntryContainer entries, String expr);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package dev.cattyn.catformat.stylist;

import dev.cattyn.catformat.stylist.wrappers.ColorWrapper;

public interface ColorStylist<T> extends Stylist<T> {
void addColor(ColorWrapper<?> color);

ColorWrapper<?> getColor(Class<?> klass);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package dev.cattyn.catformat.stylist;

import dev.cattyn.catformat.formatter.FormatEntry;
import dev.cattyn.catformat.entry.FormatEntry;

import java.util.List;

Expand Down
Loading