Fix quirk of Jansi in AnsiShortcodeConverter
All checks were successful
build-and-test / test (push) Successful in 1m47s
build-and-test / generate-javadoc (push) Successful in 1m54s
build-and-test / build (push) Successful in 1m56s

This commit prevents toggling attributes when including them multiple times in a string and instead simply ignores them when specified multiple times without <reset>ting them.
This commit is contained in:
JeremyStar™ 2024-08-19 20:14:55 +02:00
parent 70ff609b77
commit 971f81a626
Signed by: JeremyStarTM
GPG key ID: E366BAEF67E4704D

View file

@ -24,6 +24,9 @@ import de.staropensource.sosengine.base.exceptions.ParserException;
import org.fusesource.jansi.Ansi;
import org.jetbrains.annotations.NotNull;
import java.util.HashSet;
import java.util.Set;
/**
* Converts shortcodes such as {@code <bold>} or {@code <blink>} into a usable {@link Ansi} escape sequence.
*
@ -51,11 +54,13 @@ public final class AnsiShortcodeConverter extends ShortcodeParserSkeleton {
*/
public @NotNull Ansi getAnsi() {
Ansi ansi = Ansi.ansi();
Set<@NotNull String> status = new HashSet<>();
for (String component : components)
if (component.equals("RESET"))
if (component.equals("RESET")) {
ansi.a(Ansi.Attribute.RESET);
else if (component.startsWith("TEXT:"))
status.clear();
} else if (component.startsWith("TEXT:"))
ansi.a(component.substring(5));
else if (component.startsWith("COLOR:"))
if (component.startsWith("COLOR:FOREGROUND:"))
@ -63,18 +68,38 @@ public final class AnsiShortcodeConverter extends ShortcodeParserSkeleton {
else if (component.startsWith("COLOR:BACKGROUND:"))
ansi.bg(Ansi.Color.valueOf(component.substring(17)));
else if (component.startsWith("ATTRIBUTE:"))
if (component.startsWith("ATTRIBUTE:BLINK"))
if (component.startsWith("ATTRIBUTE:BLINK")) {
if (status.contains("ATTRIBUTE:BLINK"))
continue;
ansi.a(Ansi.Attribute.BLINK_SLOW);
else if (component.startsWith("ATTRIBUTE:BOLD"))
status.add("ATTRIBUTE:BLINK");
} else if (component.startsWith("ATTRIBUTE:BOLD")) {
if (status.contains("ATTRIBUTE:BOLD"))
continue;
ansi.a(Ansi.Attribute.INTENSITY_BOLD);
else if (component.startsWith("ATTRIBUTE:ITALIC"))
status.add("ATTRIBUTE:BOLD");
} else if (component.startsWith("ATTRIBUTE:ITALIC")) {
if (status.contains("ATTRIBUTE:ITALIC"))
continue;
ansi.a(Ansi.Attribute.ITALIC);
else if (component.startsWith("ATTRIBUTE:STRIKETHROUGH"))
status.add("ATTRIBUTE:ITALIC");
} else if (component.startsWith("ATTRIBUTE:STRIKETHROUGH")) {
if (status.contains("ATTRIBUTE:STRIKETHROUGH"))
continue;
ansi.a(Ansi.Attribute.STRIKETHROUGH_ON);
else if (component.startsWith("ATTRIBUTE:UNDERLINE"))
status.add("ATTRIBUTE:STRIKETHROUGH");
} else if (component.startsWith("ATTRIBUTE:UNDERLINE")) {
if (status.contains("ATTRIBUTE:UNDERLINE"))
continue;
ansi.a(Ansi.Attribute.UNDERLINE);
else if (component.startsWith("ATTRIBUTE:NEGATIVE"))
ansi.a(Ansi.Attribute.NEGATIVE_ON);
status.add("ATTRIBUTE:UNDERLINE");
}
return ansi;
}