Add client-specific colors (No Mefedroniy support 'cause no parsing :( )

This commit is contained in:
pixtaded 2025-02-09 19:01:10 +03:00
parent 57d5f5ea1e
commit 72074ca117
3 changed files with 39 additions and 3 deletions

View File

@ -0,0 +1,4 @@
package net.pixtaded.crab.client;
public record ClientColor(String regex, String color) {
}

View File

@ -0,0 +1,32 @@
package net.pixtaded.crab.client;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class ClientUtil {
public static final String COLOR_KEY = "\u2550\u2550\u2550";
public static final ClientColor[] colors = {
new ClientColor( COLOR_KEY + "(<.*?>)", "\033[0;31m$1\033[0m"),
new ClientColor("\uB9AC\u3E70(<.*?>)", "\033[0;32m$1\033[0m"),
new ClientColor(" (<.*?>)", " \033[0;34m$1\033[0m")
/*
I'm too lazy to add a message parser, which is absolutely required for Mefedroniy color support. For now, it will be just white.
Also, there is another problem with that - there is no "server log format" in the protocol specification,
and I want my client to be as compatible as possible.
*/
};
public static String clientColors(String s) {
for (ClientColor color : colors) s = matchClientKey(s, color);
return s;
}
private static String matchClientKey(String s, ClientColor color) {
Pattern p = Pattern.compile(color.regex());
Matcher m = p.matcher(s);
return m.replaceAll(color.color());
}
}

View File

@ -30,7 +30,7 @@ public class CrabClient implements Crab {
this.serverAddress = serverAddress; this.serverAddress = serverAddress;
this.port = port; this.port = port;
if (nickname != null) if (nickname != null)
this.nickname = "<" + nickname + "> "; this.nickname = ClientUtil.COLOR_KEY + "<" + nickname + "> ";
else else
this.nickname = ""; this.nickname = "";
} }
@ -71,7 +71,7 @@ public class CrabClient implements Crab {
System.out.print("Enter your nickname (leave empty for no nickname): "); System.out.print("Enter your nickname (leave empty for no nickname): ");
nickname = scanner.nextLine(); nickname = scanner.nextLine();
if (!nickname.isEmpty()) if (!nickname.isEmpty())
nickname = "<" + nickname + "> "; nickname = ClientUtil.COLOR_KEY + "<" + nickname + "> ";
} }
private void connect() throws IOException { private void connect() throws IOException {
@ -109,7 +109,7 @@ public class CrabClient implements Crab {
private void printLogs() { private void printLogs() {
clearScreen(); clearScreen();
System.out.print(Sanitizer.sanitizeString(cache.content(), false)); System.out.print(ClientUtil.clientColors(Sanitizer.sanitizeString(cache.content(), false)));
} }
private void sendMessage(String msg) throws IOException { private void sendMessage(String msg) throws IOException {