From cfc2dd8e983da0a7838902a49125e7c0d7e349f3 Mon Sep 17 00:00:00 2001 From: LatvianModder Date: Tue, 19 Sep 2023 19:39:00 +0300 Subject: [PATCH] Added indents to toAnsi() --- .../latvian/apps/webutils/html/PairedTag.java | 30 +++++++++++++++++-- .../latvian/apps/webutils/html/StringTag.java | 2 +- .../dev/latvian/apps/webutils/html/Tag.java | 6 ++-- .../apps/webutils/html/UnpairedTag.java | 2 +- .../latvian/apps/webutils/test/AnsiTests.java | 4 +-- 5 files changed, 35 insertions(+), 9 deletions(-) diff --git a/src/main/java/dev/latvian/apps/webutils/html/PairedTag.java b/src/main/java/dev/latvian/apps/webutils/html/PairedTag.java index a6ed4fd..5daebff 100644 --- a/src/main/java/dev/latvian/apps/webutils/html/PairedTag.java +++ b/src/main/java/dev/latvian/apps/webutils/html/PairedTag.java @@ -141,7 +141,7 @@ public void write(Writer writer) throws Throwable { } @Override - public void ansi(AnsiComponent component, int depth) { + public void ansi(AnsiComponent component, int depth, int indent) { int col = TagUtils.ANSI_COLORS[depth % TagUtils.ANSI_COLORS.length]; if (!this.name.isEmpty()) { @@ -150,12 +150,38 @@ public void ansi(AnsiComponent component, int depth) { component.append(Ansi.of(">").color(col)); } + boolean shouldIndent = indent >= 0 && !this.name.isEmpty(); + + if (shouldIndent) { + shouldIndent = false; + + if (this.content != null && !this.content.isEmpty()) { + for (var tag : this.content) { + if (tag instanceof UnpairedTag) { + shouldIndent = true; + break; + } + } + } + } + if (this.content != null && !this.content.isEmpty()) { for (var tag : this.content) { - tag.ansi(component, depth + 1); + if (shouldIndent) { + component.append('\n'); + component.append(" ".repeat(indent + 1)); + tag.ansi(component, depth + 1, indent + 1); + } else { + tag.ansi(component, depth + 1, indent); + } } } + if (shouldIndent) { + component.append('\n'); + component.append(" ".repeat(indent)); + } + if (!this.name.isEmpty()) { component.append(Ansi.of("").color(col)); } diff --git a/src/main/java/dev/latvian/apps/webutils/html/StringTag.java b/src/main/java/dev/latvian/apps/webutils/html/StringTag.java index f03ae91..068952e 100644 --- a/src/main/java/dev/latvian/apps/webutils/html/StringTag.java +++ b/src/main/java/dev/latvian/apps/webutils/html/StringTag.java @@ -36,7 +36,7 @@ public void write(Writer writer) throws Throwable { } @Override - public void ansi(AnsiComponent component, int depth) { + public void ansi(AnsiComponent component, int depth, int indent) { component.append(getRawContent()); } } diff --git a/src/main/java/dev/latvian/apps/webutils/html/Tag.java b/src/main/java/dev/latvian/apps/webutils/html/Tag.java index e46acb7..ea67a01 100644 --- a/src/main/java/dev/latvian/apps/webutils/html/Tag.java +++ b/src/main/java/dev/latvian/apps/webutils/html/Tag.java @@ -96,7 +96,7 @@ public void replace(Pattern pattern, BiConsumer replace) { public abstract void write(Writer writer) throws Throwable; - public abstract void ansi(AnsiComponent component, int depth); + public abstract void ansi(AnsiComponent component, int depth, int indent); @Override public String toString() { @@ -114,9 +114,9 @@ public String toString() { return writer.toString(); } - public AnsiComponent toAnsi() { + public AnsiComponent toAnsi(boolean indent) { var component = Ansi.of(); - ansi(component, 0); + ansi(component, 0, indent ? 0 : -1); return component; } diff --git a/src/main/java/dev/latvian/apps/webutils/html/UnpairedTag.java b/src/main/java/dev/latvian/apps/webutils/html/UnpairedTag.java index bf3bf12..6ab236f 100644 --- a/src/main/java/dev/latvian/apps/webutils/html/UnpairedTag.java +++ b/src/main/java/dev/latvian/apps/webutils/html/UnpairedTag.java @@ -46,7 +46,7 @@ public void write(Writer writer) throws Throwable { } @Override - public void ansi(AnsiComponent component, int depth) { + public void ansi(AnsiComponent component, int depth, int indent) { int col = TagUtils.ANSI_COLORS[depth % TagUtils.ANSI_COLORS.length]; component.append(Ansi.of("<" + this.name).color(col)); diff --git a/src/test/java/dev/latvian/apps/webutils/test/AnsiTests.java b/src/test/java/dev/latvian/apps/webutils/test/AnsiTests.java index a895439..a747aab 100644 --- a/src/test/java/dev/latvian/apps/webutils/test/AnsiTests.java +++ b/src/test/java/dev/latvian/apps/webutils/test/AnsiTests.java @@ -21,7 +21,7 @@ public void html() { var section = new PairedTag("section"); section.classes("test test-2"); section.img("img.png"); - section.p().string("Hello!").p().raw("abc"); - Ansi.log(section.toAnsi()); + section.p().string("Hello!").p().raw("abc").spanstr("yo").raw("test"); + Ansi.log("Tag:\n" + section.toAnsi(true)); } }