Skip to content

Commit

Permalink
Added ANSITable
Browse files Browse the repository at this point in the history
  • Loading branch information
LatvianModder committed Oct 14, 2024
1 parent cbae640 commit ac4a42d
Show file tree
Hide file tree
Showing 7 changed files with 514 additions and 18 deletions.
46 changes: 39 additions & 7 deletions src/main/java/dev/latvian/apps/ansi/ANSI.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ public boolean hasStyle() {
public static final ANSI EMPTY = immutable("");
public static final ANSI SPACE = immutable(" ");
public static final ANSI LINE = immutable("\n");
public static final ANSISymbols SYMBOLS = new ANSISymbols(Style.NONE);

public static ANSI immutable(String content, Style style) {
return new ANSI(content, style, true);
Expand All @@ -75,11 +74,11 @@ public static ANSI empty() {
}

public static ANSI of(Object text, Style style) {
return text instanceof ANSI a ? a.copy().styled(style) : new ANSI(String.valueOf(text), style, false);
return text instanceof ANSISupplier a ? a.toANSI().styled(style) : new ANSI(String.valueOf(text), style, false);
}

public static ANSI of(Object text) {
return text instanceof ANSI a ? a.copy() : new ANSI(String.valueOf(text), Style.NONE, false);
return text instanceof ANSISupplier a ? a.toANSI() : new ANSI(String.valueOf(text), Style.NONE, false);
}

public static ANSI join(@Nullable ANSI delimiter, ANSI... ansi) {
Expand Down Expand Up @@ -206,8 +205,23 @@ private ANSI(String content, Style style, boolean immutable) {
}

@Override
public ANSI getANSI() {
return this;
public ANSI toANSI() {
return copy();
}

@Override
public String toANSIString() {
return build(ANSIContext.NONE);
}

@Override
public String toDebugString() {
return build(new ANSIContext(1));
}

@Override
public String toUnformattedString() {
return build(ANSIContext.UNFORMATTED);
}

public ANSI withStyle(Style style) {
Expand All @@ -228,7 +242,7 @@ public ANSI styled(UnaryOperator<Style> restyle) {
}

public ANSI append(ANSI child) {
if (child == EMPTY) {
if (child.immutable && child.content.isEmpty() && child.children.isEmpty()) {
return this;
} else if (immutable) {
return new ANSI(content, style, false).append(child);
Expand Down Expand Up @@ -353,7 +367,7 @@ private void appendParts(Style s, Part[] parts, int[] index) {

public String build(ANSIContext ctx) {
if (children.isEmpty()) {
if (ctx.unformatted() || style.isDefault()) {
if (content.isEmpty() || ctx.unformatted() || style.isDefault()) {
return content;
} else {
var sb = new StringBuilder(6 + content.length() + 2);
Expand Down Expand Up @@ -492,4 +506,22 @@ public ANSI background(@Nullable ANSIColor value) {
public ANSI colors(@Nullable ANSIColor foregroundValue, @Nullable ANSIColor backgroundValue) {
return withStyle(style.colors(foregroundValue, backgroundValue));
}

public ANSI repeat(int times) {
return times <= 0 && immutable && style.isDefault() ? EMPTY : new ANSI(times <= 0 ? "" : content.repeat(times), style, false);
}

public ANSI flatten() {
if (children.isEmpty()) {
return copy();
}

var result = new StringBuilder();

for (var part : parts()) {
result.append(part.content);
}

return new ANSI(result.toString(), style, false);
}
}
36 changes: 36 additions & 0 deletions src/main/java/dev/latvian/apps/ansi/ANSIBorders.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package dev.latvian.apps.ansi;

import dev.latvian.apps.ansi.style.Style;

public record ANSIBorders(
Style style,
ANSI ud,
ANSI dr,
ANSI udr,
ANSI ur,
ANSI lr,
ANSI dlr,
ANSI udlr,
ANSI ulr,
ANSI dl,
ANSI udl,
ANSI ul
) {
public static final ANSIBorders DEFAULT = new ANSIBorders(Style.NONE);

public ANSIBorders(Style style) {
this(style,
ANSI.immutable("│", style),
ANSI.immutable("┌", style),
ANSI.immutable("├", style),
ANSI.immutable("└", style),
ANSI.immutable("─", style),
ANSI.immutable("┬", style),
ANSI.immutable("┼", style),
ANSI.immutable("┴", style),
ANSI.immutable("┐", style),
ANSI.immutable("┤", style),
ANSI.immutable("┘", style)
);
}
}
8 changes: 4 additions & 4 deletions src/main/java/dev/latvian/apps/ansi/ANSISupplier.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@

@FunctionalInterface
public interface ANSISupplier {
ANSI getANSI();
ANSI toANSI();

default String toANSIString() {
return getANSI().build(ANSIContext.NONE);
return toANSI().build(ANSIContext.NONE);
}

default String toDebugString() {
return getANSI().build(new ANSIContext(1));
return toANSI().build(new ANSIContext(1));
}

default String toUnformattedString() {
return getANSI().build(ANSIContext.UNFORMATTED);
return toANSI().build(ANSIContext.UNFORMATTED);
}
}
1 change: 1 addition & 0 deletions src/main/java/dev/latvian/apps/ansi/ANSISymbols.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public record ANSISymbols(
ANSI col,
ANSI dot
) {
public static final ANSISymbols DEFAULT = new ANSISymbols(Style.NONE);

public ANSISymbols(Style style) {
this(style,
Expand Down
Loading

0 comments on commit ac4a42d

Please sign in to comment.