Improve artc 🤓

This commit is contained in:
Anas Elgarhy 2022-06-15 19:39:00 +02:00
parent 2103e8c4bb
commit 68e4c79239
2 changed files with 13 additions and 10 deletions

View File

@ -17,7 +17,7 @@ public class ColoredString {
}
public ColoredString(String str, TextStyle... styles) {
this(str, null, styles);
this(str, (TextColor) null, styles);
}
public ColoredString(String str, TextColor foregroundColor, TextStyle... styles) {
@ -36,6 +36,10 @@ public class ColoredString {
}
}
public ColoredString(String str, String foregroundColor, TextStyle... styles) {
this(str, foregroundColor, null, styles);
}
public ColoredString(String str, String foregroundColor, String backgroundColor, TextStyle... styles) {
this(str,
TextColor.Factory.fromString(foregroundColor),
@ -76,7 +80,7 @@ public class ColoredString {
if (strBytes == null) {
return null;
}
return toColoredString().getBytes(StandardCharsets.UTF_8);
return toString().getBytes(StandardCharsets.UTF_8);
}
public void setStr(String str) {
@ -98,7 +102,8 @@ public class ColoredString {
}
public String toColoredString() {
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
final String escapeSequenceOpen = "\u001b[";
final String escapeSequenceClose = "\u001b[0m";
@ -120,7 +125,7 @@ public class ColoredString {
sb.append(new String(backgroundColor.getBackgroundSGRSequence(), StandardCharsets.UTF_8));
sb.append("m");
}
sb.append(this);
sb.append(this.toNormalStringString());
if (foregroundColor != null) {
sb.append(escapeSequenceClose);
@ -138,9 +143,7 @@ public class ColoredString {
return sb.toString();
}
@Override
public String toString() {
public String toNormalStringString() {
return strBytes == null || strBytes.length < 1 ?
null : new String(strBytes, StandardCharsets.UTF_8);
}

View File

@ -6,15 +6,15 @@ import com.anas.jcolorfulconsole.lanterna.TextColor;
public class Main {
public static void main(String[] args) {
ColoredString coloredString = new ColoredString("Hello World", "red", "blue", TextStyle.BOLD, TextStyle.ITALIC);
System.out.println(coloredString.toColoredString());
ColoredString coloredString = new ColoredString("Hello World", null, "blue", TextStyle.BOLD, TextStyle.ITALIC);
System.out.println(coloredString);
new Thread(() -> {
while (true) {
System.out.println(new ColoredString("Hi",
new TextColor.RGB((int) (Math.random() * 255), (int) (Math.random() * 255), (int) (Math.random() * 255)),
new TextColor.RGB((int) (Math.random() * 255), (int) (Math.random() * 255), (int) (Math.random() * 255)),
TextStyle.values()[(int) (Math.random() * TextStyle.values().length)]).toColoredString());
TextStyle.values()[(int) (Math.random() * TextStyle.values().length)]));
try {
Thread.sleep(1000);
} catch (InterruptedException e) {