Skip to content

Commit ea473df

Browse files
committed
Fixed configs so they actually reload when reloaded, made the colors work better
1 parent 6be04a2 commit ea473df

9 files changed

Lines changed: 84 additions & 144 deletions

File tree

src/main/java/simplexity/config/AbstractConfig.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,11 @@ public abstract class AbstractConfig {
1313
private static final Logger logger = LoggerFactory.getLogger(AbstractConfig.class);
1414
protected Config config;
1515

16-
public AbstractConfig(String configPath, String folderPath) {
17-
loadConfig(configPath, folderPath);
16+
public AbstractConfig() {
1817
reloadConfig();
1918
}
2019

21-
private void loadConfig(String configPath, String folderPath) {
20+
public void loadConfig(String configPath, String folderPath) {
2221
Logging.log(logger, "Loading configuration from directory: " + folderPath + ", file:" + configPath, Level.INFO);
2322
File folder = checkForOrCreateFolder(folderPath);
2423
if (folder == null) {

src/main/java/simplexity/config/config/AwsConfig.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public class AwsConfig extends AbstractConfig {
2626
private static AwsConfig instance;
2727

2828
public AwsConfig() {
29-
super("aws-config.conf", "configs"); //todo figure out a better way to handle the config names
29+
super(); //todo figure out a better way to handle the config names
3030
Logging.log(logger, "Initializing AWS config class", Level.INFO);
3131
}
3232

@@ -56,6 +56,7 @@ public void createDefaultConfig(File configFile) {
5656

5757
@Override
5858
public void reloadConfig() {
59+
loadConfig("aws-config.conf", "configs");
5960
Logging.log(logger, "Reloading voice prefixes configuration", Level.INFO);
6061
reloadVoicePrefixes(getConfig());
6162
Logging.log(logger, "Reloading default voice configuration", Level.INFO);

src/main/java/simplexity/config/config/ReplaceTextConfig.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public class ReplaceTextConfig extends AbstractConfig {
1717
private HashMap<String, String> replaceText = new HashMap<>();
1818
private static ReplaceTextConfig instance;
1919
public ReplaceTextConfig() {
20-
super("text-replace.conf", "configs");
20+
super();
2121
Logging.log(logger, "Initializing Replace Text config class", Level.INFO);
2222
}
2323

@@ -41,6 +41,7 @@ public void createDefaultConfig(File configFile) {
4141

4242
@Override
4343
public void reloadConfig() {
44+
loadConfig("text-replace.conf", "configs");
4445
if (replaceText == null) {
4546
replaceText = new HashMap<>();
4647
}

src/main/java/simplexity/config/config/TTSConfig.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public class TTSConfig extends AbstractConfig {
1919
private int port;
2020

2121
public TTSConfig() {
22-
super("tts-config.conf", "configs");
22+
super();
2323
Logging.log(logger, "Initializing TTS config class", Level.INFO);
2424
}
2525

@@ -41,6 +41,7 @@ public void createDefaultConfig(File configFile) {
4141

4242
@Override
4343
public void reloadConfig() {
44+
loadConfig("tts-config.conf", "configs");
4445
reloadInts(getConfig());
4546
Logging.log(logger, "TTS configuration reloaded successfully", Level.INFO);
4647
}

src/main/java/simplexity/config/locale/LocaleConfig.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import org.slf4j.LoggerFactory;
66
import org.slf4j.event.Level;
77
import simplexity.config.AbstractConfig;
8+
import simplexity.util.ColorTags;
89
import simplexity.util.Logging;
910

1011
import java.io.File;
@@ -16,7 +17,7 @@ public class LocaleConfig extends AbstractConfig {
1617
private static LocaleConfig instance;
1718

1819
public LocaleConfig() {
19-
super("locale.conf", "configs");
20+
super();
2021
Logging.log(logger, "Initializing LocaleConfig class", Level.INFO);
2122
}
2223

@@ -43,10 +44,13 @@ public void createDefaultConfig(File configFile) {
4344

4445
@Override
4546
public void reloadConfig() {
47+
Logging.log(logger, "Reloading config file locale.conf", Level.INFO);
48+
loadConfig("locale.conf", "configs");
4649
Config localeConfig = getConfig();
4750
for (Message message : Message.values()) {
4851
String localeMessage = localeConfig.getString(message.getPath());
4952
if (localeMessage == null) continue;
53+
localeMessage = ColorTags.parse(localeMessage);
5054
message.setMessage(localeMessage);
5155
Logging.log(logger, "Setting " + message + " to " + localeMessage, Level.DEBUG);
5256
}

src/main/java/simplexity/config/locale/Message.java

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,31 +6,31 @@ public enum Message {
66

77
INVALID_VOICE("error-invalid-voice",
88
"<br-red>Error: '%voice%' is not a valid voice. " +
9-
"\\nPlease make sure you are only choosing from standard voices.</br-red>" +
9+
"\\nPlease make sure you are only choosing from standard voices.</color>" +
1010
"\\nStandard voices can be found here:" +
11-
"\\n<yellow>https://docs.aws.amazon.com/polly/latest/dg/voicelist.html</yellow>"
11+
"\\n<yellow>https://docs.aws.amazon.com/polly/latest/dg/voicelist.html</color>"
1212
),
1313
INVALID_REGION("error-invalid-region",
14-
"<br-red>Error: '%region%' is not a valid region.</br-red>" +
14+
"<br-red>Error: '%region%' is not a valid region.</color>" +
1515
"\\n<yellow>Regions can be found here:" +
1616
"\\nhttps://aws.amazon.com/about-aws/global-infrastructure/regions_az/" +
1717
"\\nUsing default region of 'US_EAST_1'"
1818
),
1919

20-
UNKNOWN_COMMAND("error-unknown-command","<br-red>ERROR: '%command%' is not a recognized command</br-red>"),
20+
UNKNOWN_COMMAND("error-unknown-command","<br-red>ERROR: '%command%' is not a recognized command</color>"),
2121

22-
NULL_AWS_CREDENTIALS("error-aws-credentials-null", "<br-red>ERROR: AWS credentials are null, please fill them out"),
22+
NULL_AWS_CREDENTIALS("error-aws-credentials-null", "<br-red>ERROR: AWS credentials are null, please fill them out</color>"),
2323

24-
GENERAL_ERROR("error-default", "<br-red>ERROR: %error%</br-red>"),
24+
GENERAL_ERROR("error-default", "<br-red>ERROR: %error%</color>"),
2525

26-
INVALID_INPUT("error-invalid-input", "<br-red>ERROR: Invalid input</br-red>"),
26+
INVALID_INPUT("error-invalid-input", "<br-red>ERROR: Invalid input</color>"),
2727

28-
MESSAGE_NOT_PARSABLE("error-not-parsable", "<br-red>ERROR: Message was not parsable, attempted to send: %message%</br-red>"),
29-
HELP_HEADER("help-header", "<br-cyan>CLI Text To Speech</br-cyan>"),
30-
HELP_COMMAND_MESSAGE("help-command-format", "<blue>%command_name%</blue> <white>-</white> <br-blue>%command_description%</br-blue>"),
31-
RELOAD_MESSAGE("config-reloaded", "<green>Config reloaded</green>"),
32-
PLEASE_SAVE_AWS_INFO_IN_CONFIG("save-aws-info", "<br-yellow>Please save your AWS credentials in tts-config.conf, then click enter to continue</br-yellow>"),
33-
SHUTTING_DOWN("shutting-down", "<br-yellow>CLI Text To Speech is closing...</br-yellow>"),
28+
MESSAGE_NOT_PARSABLE("error-not-parsable", "<br-red>ERROR: Message was not parsable, attempted to send: %message%</color>"),
29+
HELP_HEADER("help-header", "<br-cyan>CLI Text To Speech</color>"),
30+
HELP_COMMAND_MESSAGE("help-command-format", "<blue>%command_name%</color> <white>-</color> <br-blue>%command_description%</color>"),
31+
RELOAD_MESSAGE("config-reloaded", "<green>Config reloaded</color>"),
32+
PLEASE_SAVE_AWS_INFO_IN_CONFIG("save-aws-info", "<br-yellow>Please save your AWS credentials in tts-config.conf, then click enter to continue</color>"),
33+
SHUTTING_DOWN("shutting-down", "<br-yellow>CLI Text To Speech is closing...</color>"),
3434

3535
;
3636
private final String path;
Lines changed: 7 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,16 @@
11
package simplexity.util;
22

3-
import java.util.ArrayList;
4-
53
public class ColorTags {
64

75
public static String parse(String input) {
8-
StringBuilder result = new StringBuilder();
9-
ArrayList<ConsoleColor> activeColors = new ArrayList<>();
10-
int tagStart = -1;
11-
StringBuilder currentText = new StringBuilder();
12-
13-
for (int i = 0; i < input.length(); i++) {
14-
char c = input.charAt(i);
15-
16-
// Detect the start of a tag
17-
if (c == '<') {
18-
result.append(currentText); // Append collected text
19-
currentText.setLength(0); // Clear the buffer
20-
tagStart = i;
21-
}
22-
// Detect the end of a tag
23-
else if (c == '>' && tagStart != -1) {
24-
String tag = input.substring(tagStart, i + 1);
25-
ConsoleColor color = ConsoleColor.fromTag(tag);
26-
27-
if (color != null) {
28-
if (tag.equalsIgnoreCase(color.getStartTag())) {
29-
// Opening tag: Add the ANSI code
30-
activeColors.add(0, color);
31-
result.append(generateAnsiCode(activeColors));
32-
} else if (tag.equalsIgnoreCase(color.getEndTag())) {
33-
// Closing tag: Remove the ANSI code
34-
activeColors.remove(color); // Remove specific color
35-
result.append(generateAnsiCode(activeColors));
36-
}
37-
}
38-
tagStart = -1; // Reset tag tracking
39-
}
40-
// Append characters outside of tags
41-
else if (tagStart == -1) {
42-
currentText.append(c);
43-
}
44-
}
45-
46-
// Append any remaining text
47-
result.append(currentText);
48-
49-
// Reset to default at the end
50-
result.append("\u001B[0m"); // ANSI reset
51-
52-
return result.toString();
53-
}
54-
55-
/**
56-
* Generates the ANSI code sequence for the currently active colors.
57-
*
58-
* @param activeColors Stack of active ConsoleColor enums.
59-
* @return Combined ANSI escape code.
60-
*/
61-
private static String generateAnsiCode(ArrayList<ConsoleColor> activeColors) {
62-
if (activeColors.isEmpty()) {
63-
return "\u001B[0m"; // Reset if no colors are active
64-
}
65-
66-
StringBuilder ansiCode = new StringBuilder("\u001B[");
67-
for (int i = 0; i < activeColors.size(); i++) {
68-
ansiCode.append(activeColors.get(i).getAnsiCode());
69-
if (i < activeColors.size() - 1) {
70-
ansiCode.append(";"); // Separate codes with semicolons
6+
String parsedText = input;
7+
for (ConsoleColor color : ConsoleColor.values()) {
8+
if (input.contains(color.getTag())) {
9+
String ansiCode = "\033[" + color.getAnsiCode() + "m";
10+
parsedText = parsedText.replace(color.getTag(), ansiCode);
7111
}
7212
}
73-
ansiCode.append("m");
74-
return ansiCode.toString();
13+
parsedText = parsedText + "\033[0m";
14+
return parsedText;
7515
}
7616
}

src/main/java/simplexity/util/ConsoleColor.java

Lines changed: 49 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -2,84 +2,79 @@
22

33
public enum ConsoleColor {
44
// Reset
5-
RESET("0", "reset", "<reset>", ""), // Text Reset
6-
BOLD("1", "bold", "<bold>", "</bold>"),
7-
ITALIC("3", "italic", "<i>", "</i>"),
8-
UNDERLINE("4", "underline", "<u>", "</u>"),
9-
STRIKETHROUGH("5", "strikethrough", "<s>", "</s>"),
5+
RESET("0", "<reset>"), // Text Reset
6+
BOLD("1", "<bold>"),
7+
NOT_BOLD("22","</bold>"),
8+
ITALIC("3", "<i>"),
9+
NOT_ITALIC("23","</i>"),
10+
UNDERLINE("4", "<u>"),
11+
NOT_UNDERLINED("24", "</u>"),
12+
STRIKETHROUGH("9", "<s>"),
13+
NOT_STRIKETHROUGH("29", "</s>"),
1014
// Regular Colors
11-
BLACK("30", "black", "<black>", "</black>"), // BLACK
12-
RED("31", "red", "<red>", "</red>"), // RED
13-
GREEN("32", "green", "<green>", "</green>"), // GREEN
14-
YELLOW("33", "yellow", "<yellow>", "</yellow>"), // YELLOW
15-
BLUE("34", "blue", "<blue>", "</blue>"), // BLUE
16-
PURPLE("35", "purple", "<purple>", "</purple>"), // PURPLE
17-
CYAN("36", "cyan", "<cyan>", "</cyan>"), // CYAN
18-
WHITE("37", "white", "<white>", "</white>"), // WHITE
15+
DEFAULT_COLOR("39", "</color>"),
16+
BLACK("30", "<black>"), // BLACK
17+
RED("31", "<red>"), // RED
18+
GREEN("32", "<green>"), // GREEN
19+
YELLOW("33", "<yellow>"), // YELLOW
20+
BLUE("34", "<blue>"), // BLUE
21+
PURPLE("35", "<purple>"), // PURPLE
22+
CYAN("36", "<cyan>"), // CYAN
23+
WHITE("37", "<white>"), // WHITE
1924

2025
// Background
21-
BLACK_BACKGROUND("40", "black-background", "<black-bg>", "</black-bg>"), // BLACK
22-
RED_BACKGROUND("41", "red-background", "<red-bg>", "</red-bg>"), // RED
23-
GREEN_BACKGROUND("42", "green-background", "<green-bg>", "</green-bg>"), // GREEN
24-
YELLOW_BACKGROUND("43", "yellow-background", "<yellow-bg>", "</yellow-bg>"), // YELLOW
25-
BLUE_BACKGROUND("44", "blue-background", "<blue-bg>", "</blue-bg>"), // BLUE
26-
PURPLE_BACKGROUND("45", "purple-background", "<purple-bg>", "</purple-bg>"), // PURPLE
27-
CYAN_BACKGROUND("46", "cyan-background", "<cyan-bg>", "</cyan-bg>"), // CYAN
28-
WHITE_BACKGROUND("47", "white-background", "<white-bg>", "</white-bg>"), // WHITE
26+
DEFAULT_BACKGROUND("48", "</color-bg>"),
27+
BLACK_BACKGROUND("40", "<black-bg>"), // BLACK
28+
RED_BACKGROUND("41", "<red-bg>"), // RED
29+
GREEN_BACKGROUND("42", "<green-bg>"), // GREEN
30+
YELLOW_BACKGROUND("43", "<yellow-bg>"), // YELLOW
31+
BLUE_BACKGROUND("44", "<blue-bg>"), // BLUE
32+
PURPLE_BACKGROUND("45", "<purple-bg>"), // PURPLE
33+
CYAN_BACKGROUND("46", "<cyan-bg>"), // CYAN
34+
WHITE_BACKGROUND("47", "<white-bg>"), // WHITE
2935

3036
// High Intensity
31-
BLACK_BRIGHT("90", "bright-black", "<br-black>", "</br-black>"), // BLACK
32-
RED_BRIGHT("91", "bright-red", "<br-red>", "</br-red>"), // RED
33-
GREEN_BRIGHT("92", "bright-green", "<br-green>", "</br-green>"), // GREEN
34-
YELLOW_BRIGHT("93", "bright-yellow", "<br-yellow>", "</br-yellow>"), // YELLOW
35-
BLUE_BRIGHT("94", "bright-blue", "<br-blue>", "</br-blue>"), // BLUE
36-
PURPLE_BRIGHT("95", "bright-purple", "<br-purple>", "</br-purple>"), // PURPLE
37-
CYAN_BRIGHT("96", "bright-cyan", "<br-cyan>", "</br-cyan>"), // CYAN
38-
WHITE_BRIGHT("97", "bright-white", "<br-white>", "</br-white>"), // WHITE
37+
BLACK_BRIGHT("90", "<br-black>"), // BLACK
38+
RED_BRIGHT("91", "<br-red>"), // RED
39+
GREEN_BRIGHT("92", "<br-green>"), // GREEN
40+
YELLOW_BRIGHT("93", "<br-yellow>"), // YELLOW
41+
BLUE_BRIGHT("94", "<br-blue>"), // BLUE
42+
PURPLE_BRIGHT("95", "<br-purple>"), // PURPLE
43+
CYAN_BRIGHT("96", "<br-cyan>"), // CYAN
44+
WHITE_BRIGHT("97", "<br-white>"), // WHITE
3945

4046
// High Intensity backgrounds
41-
BLACK_BACKGROUND_BRIGHT("100", "bright-black-background", "<br-black-bg>", "</br-black-bg>"),// BLACK
42-
RED_BACKGROUND_BRIGHT("101", "bright-red-background", "<br-red-bg>", "</br-red-bg>"),// RED
43-
GREEN_BACKGROUND_BRIGHT("102", "bright-green-background", "<br-green-bg>", "</br-green-bg>"),// GREEN
44-
YELLOW_BACKGROUND_BRIGHT("103", "bright-yellow-background", "<br-yellow-bg>", "</br-yellow-bg>"),// YELLOW
45-
BLUE_BACKGROUND_BRIGHT("104", "bright-blue-background", "<br-blue-bg>", "</br-blue-bg>"),// BLUE
46-
PURPLE_BACKGROUND_BRIGHT("105", "bright-purple-background", "<br-purple-bg>", "</br-purple-bg>"), // PURPLE
47-
CYAN_BACKGROUND_BRIGHT("106", "bright-cyan-background", "<br-cyan-bg>", "</br-cyan-bg>"), // CYAN
48-
WHITE_BACKGROUND_BRIGHT("107", "bright-white-background", "<br-white-bg>", "</br-white-bg>");
47+
BLACK_BACKGROUND_BRIGHT("100", "<br-black-bg>"),// BLACK
48+
RED_BACKGROUND_BRIGHT("101", "<br-red-bg>"),// RED
49+
GREEN_BACKGROUND_BRIGHT("102", "<br-green-bg>"),// GREEN
50+
YELLOW_BACKGROUND_BRIGHT("103", "<br-yellow-bg>"),// YELLOW
51+
BLUE_BACKGROUND_BRIGHT("104", "<br-blue-bg>"),// BLUE
52+
PURPLE_BACKGROUND_BRIGHT("105", "<br-purple-bg>"), // PURPLE
53+
CYAN_BACKGROUND_BRIGHT("106", "<br-cyan-bg>"), // CYAN
54+
WHITE_BACKGROUND_BRIGHT("107", "<br-white-bg>");
4955

5056
// WHITE
5157

5258
private final String ansiCode;
53-
private final String name;
54-
private final String startTag;
55-
private final String endTag;
59+
private final String tag;
5660

57-
ConsoleColor(String ansiCode, String name, String startTag, String endTag) {
61+
ConsoleColor(String ansiCode, String tag) {
5862
this.ansiCode = ansiCode;
59-
this.name = name;
60-
this.startTag = startTag;
61-
this.endTag = endTag;
63+
this.tag = tag;
6264
}
6365

6466
public String getAnsiCode() {
6567
return ansiCode;
6668
}
6769

68-
public String getName() {
69-
return name;
70+
public String getTag() {
71+
return tag;
7072
}
7173

72-
public String getStartTag() {
73-
return startTag;
74-
}
75-
76-
public String getEndTag() {
77-
return endTag;
78-
}
7974

8075
public static ConsoleColor fromTag(String tag) {
8176
for (ConsoleColor color : values()) {
82-
if (color.getStartTag().equalsIgnoreCase(tag) || color.getEndTag().equalsIgnoreCase(tag)) {
77+
if (color.getTag().equalsIgnoreCase(tag)) {
8378
return color;
8479
}
8580
}

src/main/java/simplexity/util/Logging.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,9 @@ public class Logging {
1010
private static final Pattern ANSI_PATTERN = Pattern.compile("\u001B\\[[;\\d]*m");
1111

1212
public static void logAndPrint(Logger logger, String message, Level level) {
13-
String printMessage = ColorTags.parse(message);
14-
String logMessage = stripAnsiCodes(printMessage);
13+
String logMessage = stripAnsiCodes(message);
1514
logger.atLevel(level).log(logMessage);
16-
System.out.println(printMessage);
15+
System.out.println(message);
1716
}
1817

1918
public static void log(Logger logger, String message, Level level) {

0 commit comments

Comments
 (0)