feat(ui): Implemented different text colors and bg

This commit is contained in:
Jakub Žitník 2025-03-31 21:59:11 +02:00
parent 6028b54d10
commit aced312df1
Signed by: jzitnik
GPG Key ID: C577A802A6AF4EF3
2 changed files with 35 additions and 14 deletions

View File

@ -29,14 +29,15 @@ public class Font {
private int width;
}
private String get(int startY, int startX, int height, int width, String color) {
private String get(int startY, int startX, int height, int width, String color, String background) {
StringBuilder stringBuilder = new StringBuilder();
for (int y = startY; y < startY + height; y++) {
String[] line = lines[y].split("\033");
for (int x = startX; x < startX + width - 1; x++) {
stringBuilder.append("\033").append(line[x].replaceAll("\\[(?!49m)[0-9;]+m", color));
stringBuilder.append("\033")
.append(line[x].replaceAll("\\[(?!49m)[0-9;]+m", color).replaceAll("\\[49m", background));
}
stringBuilder.append("\033[0m\n");
}
@ -44,7 +45,7 @@ public class Font {
return stringBuilder.toString();
}
private CharDTO getDigit(char digit, String color) {
private CharDTO getDigit(char digit, String color, String background) {
int num = Integer.valueOf(digit);
int width = 12;
int startY = HEIGHT;
@ -55,16 +56,16 @@ public class Font {
startX = (26 + 9) * width;
}
return new CharDTO(get(startY, startX, HEIGHT, width, color), width);
return new CharDTO(get(startY, startX, HEIGHT, width, color, background), width);
}
public CharDTO getChar(char character, String color) {
public CharDTO getChar(char character, String color, String background) {
if (character == ' ') {
return new CharDTO(("\033[49m ".repeat(12) + "\n").repeat(HEIGHT), 12);
}
if (Character.isDigit(character)) {
return getDigit(character, color);
return getDigit(character, color, background);
}
boolean upper = Character.isUpperCase(character);
@ -72,17 +73,17 @@ public class Font {
int startY = (upper ? 0 : 1) * HEIGHT;
int startX = (upper ? character - 'A' : character - 'a') * width;
return new CharDTO(get(startY, startX, HEIGHT, width, color), width);
return new CharDTO(get(startY, startX, HEIGHT, width, color, background), width);
}
public FontDTO getLine(String text) {
public FontDTO getLine(String text, String color, String background) {
char[] chars = text.toCharArray();
String[][] letters = new String[chars.length][];
int width = 0;
for (int i = 0; i < chars.length; i++) {
var charDto = getChar(chars[i], "[47m");
var charDto = getChar(chars[i], color, background);
letters[i] = charDto.getCharacter().split("\n");
width += charDto.getWidth();
}
@ -129,8 +130,8 @@ public class Font {
return result;
}
public FontDTO getScaledLine(String text, Size size, int termWidth) {
return scale(getLine(text), size.getFunc().apply(termWidth));
public FontDTO getScaledLine(String text, Size size, int termWidth, String color, String background) {
return scale(getLine(text, color, background), size.getFunc().apply(termWidth));
}
// This will probably need little more work
@ -171,6 +172,13 @@ public class Font {
CENTER
}
public record Background(String bg) {
}
public record Color(String color) {
}
private FontDTO applyAlignment(FontDTO data, Align alignment, int termWidth) {
return switch (alignment) {
case LEFT -> data;
@ -209,11 +217,14 @@ public class Font {
public FontDTO line(Terminal terminal, String text, Object... attributes) {
int termWidth = terminal.getWidth();
//int termHeight = terminal.getHeight();
// int termHeight = terminal.getHeight();
Size fontSize = Size.MEDIUM;
Align alignment = Align.LEFT;
String color = "[47m";
String background = "[49m";
for (Object attribute : attributes) {
if (Size.class.isAssignableFrom(attribute.getClass())) {
fontSize = (Size) attribute;
@ -225,9 +236,19 @@ public class Font {
continue;
}
if (Color.class.isAssignableFrom(attribute.getClass())) {
color = ((Color) attribute).color();
continue;
}
if (Background.class.isAssignableFrom(attribute.getClass())) {
background = ((Background) attribute).bg();
continue;
}
log.error("Invalid attribute class {}. Skipping", attribute.getClass().getSimpleName());
}
return applyAlignment(getScaledLine(text, fontSize, termWidth), alignment, termWidth);
return applyAlignment(getScaledLine(text, fontSize, termWidth, color, background), alignment, termWidth);
}
}

View File

@ -16,7 +16,7 @@ public class Escape {
log.debug("Terminal width: {}", width);
log.debug("Terminal height: {}", height);
var twodcraft = font.line(terminal, "2DCraft", Size.LARGE, Align.LEFT);
var twodcraft = font.line(terminal, "2DCraft", Size.LARGE, Align.CENTER);
buffer.append(twodcraft.getData());
}
}