Skip to content

Commit

Permalink
Added indents to toAnsi()
Browse files Browse the repository at this point in the history
  • Loading branch information
LatvianModder committed Sep 19, 2023
1 parent 3c14883 commit cfc2dd8
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 9 deletions.
30 changes: 28 additions & 2 deletions src/main/java/dev/latvian/apps/webutils/html/PairedTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand All @@ -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("</" + this.name + ">").color(col));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
6 changes: 3 additions & 3 deletions src/main/java/dev/latvian/apps/webutils/html/Tag.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public void replace(Pattern pattern, BiConsumer<Tag, Matcher> 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() {
Expand All @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/dev/latvian/apps/webutils/test/AnsiTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}

0 comments on commit cfc2dd8

Please sign in to comment.