From 971f81a62638c3f0fc8509b212ed230fdf2a849e Mon Sep 17 00:00:00 2001 From: JeremyStarTM Date: Mon, 19 Aug 2024 20:14:55 +0200 Subject: [PATCH] Fix quirk of Jansi in AnsiShortcodeConverter This commit prevents toggling attributes when including them multiple times in a string and instead simply ignores them when specified multiple times without ting them. --- .../ansi/AnsiShortcodeConverter.java | 43 +++++++++++++++---- 1 file changed, 34 insertions(+), 9 deletions(-) diff --git a/ansi/src/main/java/de/staropensource/sosengine/ansi/AnsiShortcodeConverter.java b/ansi/src/main/java/de/staropensource/sosengine/ansi/AnsiShortcodeConverter.java index e28fc5a0..9b202d16 100644 --- a/ansi/src/main/java/de/staropensource/sosengine/ansi/AnsiShortcodeConverter.java +++ b/ansi/src/main/java/de/staropensource/sosengine/ansi/AnsiShortcodeConverter.java @@ -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 } or {@code } 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; }