Skip to content

Commit

Permalink
Added scientific notation support for floats in JSON parser, moved An…
Browse files Browse the repository at this point in the history
…siJava class into Ansi.ofObject()
  • Loading branch information
LatvianModder committed Feb 1, 2024
1 parent 66081e1 commit 492a716
Show file tree
Hide file tree
Showing 7 changed files with 169 additions and 105 deletions.
84 changes: 84 additions & 0 deletions src/main/java/dev/latvian/apps/webutils/ansi/Ansi.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package dev.latvian.apps.webutils.ansi;

import dev.latvian.apps.webutils.TimeUtils;
import dev.latvian.apps.webutils.json.JSON;
import org.jetbrains.annotations.Nullable;

import java.util.Date;
import java.util.Map;
import java.util.regex.Pattern;

/**
Expand Down Expand Up @@ -39,6 +42,87 @@ static AnsiComponent of(Object text) {
return text instanceof AnsiComponent ac ? ac : new AnsiComponent(text);
}

static AnsiComponent ofObject(Object object) {
return ofObject0(object, 0);
}

private static AnsiComponent ofObject0(@Nullable Object object, int depth) {
if (object == null || object == JSON.NULL) {
return Ansi.darkRed("null");
} else if (object instanceof Map<?, ?> map) {
var builder = Ansi.of().treeColor(depth);
builder.append('{');
boolean first = true;

for (var entry : map.entrySet()) {
if (first) {
first = false;
} else {
builder.append(',');
}

builder.append(Ansi.cyan("\"" + entry.getKey() + "\""));
builder.append(':');
builder.append(ofObject0(entry.getValue(), depth + 1));
}

builder.append('}');
return builder;
} else if (object instanceof Iterable<?> itr) {
var builder = Ansi.of();
builder.append('[');
boolean first = true;

for (var e : itr) {
if (first) {
first = false;
} else {
builder.append(',');
}

builder.append(ofObject0(e, depth + 1));
}

builder.append(']');
return builder;
} else if (object instanceof CharSequence) {
return Ansi.orange("\"" + object + "\"");
} else if (object instanceof Boolean b) {
return b ? Ansi.green("true") : Ansi.red("false");
} else if (object instanceof Number) {
return Ansi.purple(object);
} else if (object.getClass().isRecord()) {
var builder = Ansi.of().treeColor(depth);
builder.append('{');
boolean first = true;

for (var component : object.getClass().getRecordComponents()) {
if (first) {
first = false;
} else {
builder.append(',');
}

builder.append(Ansi.cyan("\"" + component.getName() + "\""));
builder.append(':');

try {
builder.append(ofObject0(component.getAccessor().invoke(object), depth + 1));
} catch (Exception ex) {
builder.append(darkRed("<error>").whiteBg());
ex.printStackTrace();
}
}

builder.append('}');
return builder;
} else if (object.getClass().isEnum()) {
return Ansi.orange(object);
} else {
return Ansi.lightGray(object.toString());
}
}

static AnsiComponent bold(Object text) {
return of(text).bold();
}
Expand Down
50 changes: 25 additions & 25 deletions src/main/java/dev/latvian/apps/webutils/ansi/AnsiComponent.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,33 @@ public class AnsiComponent implements AnsiAppendable {
public String text;
private int color = -1;
private int bgColor = -1;
private boolean bold = false;
private boolean italic = false;
private boolean underline = false;
private boolean blink = false;
private boolean reverse = false;
private boolean hidden = false;
private boolean strikethrough = false;
private int bold = -1;
private int italic = -1;
private int underline = -1;
private int blink = -1;
private int reverse = -1;
private int hidden = -1;
private int strikethrough = -1;
private List<AnsiAppendable> children;

public AnsiComponent(Object text) {
this.text = String.valueOf(text);
}

private static boolean appendMode(StringBuilder builder, boolean semi, boolean prevMode, boolean mode, char code, char resetCode, AnsiContext ctx, String debug) {
private static boolean appendMode(StringBuilder builder, boolean semi, int prevMode, int mode, char code, char resetCode, AnsiContext ctx, String debug) {
if (prevMode != mode) {
if (semi) {
builder.append(';');
}

if (ctx.debug()) {
if (mode) {
if (mode == 1) {
builder.append(debug);
} else {
builder.append('!');
builder.append(debug);
}
} else if (mode) {
} else if (mode == 1) {
builder.append(code);
} else {
builder.append('2');
Expand Down Expand Up @@ -117,13 +117,13 @@ public void appendAnsi(StringBuilder builder, AnsiContext ctx) {

int pColor = parent == null ? -1 : parent.color;
int pBgColor = parent == null ? -1 : parent.bgColor;
boolean pBold = parent != null && parent.bold;
boolean pItalic = parent != null && parent.italic;
boolean pUnderline = parent != null && parent.underline;
boolean pBlink = parent != null && parent.blink;
boolean pReverse = parent != null && parent.reverse;
boolean pHidden = parent != null && parent.hidden;
boolean pStrikethrough = parent != null && parent.strikethrough;
int pBold = parent == null ? -1 : parent.bold;
int pItalic = parent == null ? -1 : parent.italic;
int pUnderline = parent == null ? -1 : parent.underline;
int pBlink = parent == null ? -1 : parent.blink;
int pReverse = parent == null ? -1 : parent.reverse;
int pHidden = parent == null ? -1 : parent.hidden;
int pStrikethrough = parent == null ? -1 : parent.strikethrough;
boolean changed = color != pColor || bgColor != pBgColor || bold != pBold || italic != pItalic || underline != pUnderline || blink != pBlink || reverse != pReverse || hidden != pHidden || strikethrough != pStrikethrough;

if (changed) {
Expand Down Expand Up @@ -230,37 +230,37 @@ public AnsiComponent append(Object text) {
}

public AnsiComponent bold() {
bold = true;
bold = 1;
return this;
}

public AnsiComponent italic() {
italic = true;
italic = 1;
return this;
}

public AnsiComponent underline() {
underline = true;
underline = 1;
return this;
}

public AnsiComponent blink() {
blink = true;
blink = 1;
return this;
}

public AnsiComponent reverse() {
reverse = true;
reverse = 1;
return this;
}

public AnsiComponent hidden() {
hidden = true;
hidden = 1;
return this;
}

public AnsiComponent strikethrough() {
strikethrough = true;
strikethrough = 1;
return this;
}

Expand Down Expand Up @@ -341,7 +341,7 @@ public AnsiComponent error() {
return white().darkRedBg();
}

public AnsiComponent debugColor(int index) {
public AnsiComponent treeColor(int index) {
return switch (index & 3) {
case 0 -> red();
case 1 -> green();
Expand Down
62 changes: 0 additions & 62 deletions src/main/java/dev/latvian/apps/webutils/ansi/AnsiJava.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@
public class BufferedJSONReader implements JSONReader {
private final JSON json;
private final Reader reader;
private boolean move;
private char peek;

public BufferedJSONReader(JSON json, Reader reader) {
this.json = json;
this.reader = reader;
this.move = true;
this.peek = 0;
}

@Override
Expand All @@ -18,22 +22,22 @@ public JSON json() {

@Override
public char read() {
try {
return (char) reader.read();
} catch (Exception e) {
throw new IllegalStateException(e);
}
move = true;
return peek();
}

@Override
public char peek() {
try {
reader.mark(1);
int c = reader.read();
reader.reset();
return (char) c;
} catch (Exception e) {
throw new IllegalStateException(e);
if (move) {
move = false;

try {
peek = (char) reader.read();
} catch (Exception e) {
throw new IllegalStateException(e);
}
}

return peek;
}
}
35 changes: 30 additions & 5 deletions src/main/java/dev/latvian/apps/webutils/json/JSONReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ default void skipWhitespace() {
}
}

@SuppressWarnings("unchecked")
default <T> T adapt(Class<T> type) {
return json().adapt(readValue(), type);
}
Expand Down Expand Up @@ -115,17 +114,43 @@ default Number readNumber() {

var c = peek();

while (c == '-' || c == '+' || c == '.' || (c >= '0' && c <= '9')) {
if (c == '-' || c == '+') {
builder.append(read());
c = peek();
}

String str = builder.toString();
boolean floating = false;

if (str.contains(".")) {
while (true) {
if (c == '.') {
if (floating) {
throw new IllegalStateException("Invalid number: Multiple '.'");
} else {
floating = true;
}

builder.append(read());
} else if (c == 'E' || c == 'e') {
builder.append(read());

if (peek() == '-') {
builder.append(read());
}
} else if (c >= '0' && c <= '9') {
builder.append(read());
} else {
break;
}

c = peek();
}

var str = builder.toString();

if (floating) {
return Double.parseDouble(str);
} else {
return Long.parseLong(str);
return Long.decode(str);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class AnsiTests {
public void components() {
var c = Ansi.of();
c.append("GET 200 ");
c.append(Ansi.cyan("Test"));
c.append(Ansi.cyan("Test").underline());
c.append(" /test");
var c1 = Ansi.of("WEB ").green().append(c);

Expand Down
Loading

0 comments on commit 492a716

Please sign in to comment.