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

View file

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