-
-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathHighlightHelper.java
More file actions
46 lines (35 loc) · 1.55 KB
/
HighlightHelper.java
File metadata and controls
46 lines (35 loc) · 1.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package com.github.stickerifier.stickerify.logger;
import static ch.qos.logback.core.pattern.color.ANSIConstants.BOLD;
import static ch.qos.logback.core.pattern.color.ANSIConstants.ESC_END;
import static ch.qos.logback.core.pattern.color.ANSIConstants.ESC_START;
import static ch.qos.logback.core.pattern.color.ANSIConstants.GREEN_FG;
import org.jspecify.annotations.Nullable;
import java.util.regex.Pattern;
public final class HighlightHelper {
static final String START_GREEN = changeColorTo(BOLD + GREEN_FG);
private static final Pattern MIME_TYPE_PATTERN = Pattern.compile(" (\\w+/[-+.\\w]+) ");
static String changeColorTo(final String color) {
return ESC_START + color + ESC_END;
}
/**
* Enriches the {@code message} string with ANSI color codes to highlight it in green.
* Then, the string continues with the color specified by {@code previousColor}.
*
* @param message the message to be highlighted
* @param previousColor the color to use after the highlighted text
* @return the highlighted text
*/
static String greenHighlight(final String message, String previousColor) {
return START_GREEN + message + previousColor;
}
static @Nullable String retrieveMimeType(final String message) {
var matcher = MIME_TYPE_PATTERN.matcher(message);
return matcher.find() ? matcher.group(1) : null;
}
static String replaceFirst(String message, String textToReplace, String replacement) {
return message.replaceFirst(Pattern.quote(textToReplace), replacement);
}
private HighlightHelper() {
throw new UnsupportedOperationException();
}
}