here is my code. It appears that selectedTextColor and selectedBgColor do not have any value because it does not change the color of the text or the background of the terminal. Im trying not to use outside library since im doing a bare metal programming in C in RPI3. Here is the link for references of the terminal color :
typedef struct color {
const char *colorName;
const char *textColor;
const char *bgColor;
} color;
color color_values[] = {
{"black", "30", "40"},
{"red", "31", "41"},
{"green", "32", "42"},
{"yellow", "33", "43"},
{"blue", "34", "44"},
{"purple", "35", "45"},
{"cyan", "36", "46"},
{"white", "37", "47"},
{"none","hello","hi"}
};
// color code to color name
const char* get_color_code(const char* color, int is_background) {
for (int i = 0; my_strcmp(color_values[i].colorName, "none") != 0; i++) {
if (my_strcmp(color_values[i].colorName, color) == 0) {
return is_background ? color_values[i].bgColor : color_values[i].textColor;
}
}
return "0"; // Return default "reset" color if no match found
}
void selectColor() {
char colorInput[MAX_CMD_SIZE];
int inputIndex;
char command;
const char *selectedTextColor;
const char *selectedBgColor;
char c;
while ((c = uart_getc()) != 'n') {
uart_sendc(c); // Echo the character back to the terminal
if (c == ' ') {
if (command == 't') {
colorInput[inputIndex] = '';
selectedTextColor = get_color_code(colorInput, 0);
} else if (command == 'b') {
colorInput[inputIndex] = '';
selectedBgColor = get_color_code(colorInput, 1);
}
command = 0;
inputIndex = 0;
} else if (c == '-') {
c = uart_getc(); // Read the next character to check if it's 't' or 'b'
uart_sendc(c); // Echo it
if (c == 't' || c == 'b') {
command = c;
}
continue; // Skip storing '-' and the following character
} else if (command) {
colorInput[inputIndex++] = c;
}
}
if (my_strcmp(selectedTextColor, "0") != 0) {
uart_puts("33[");
uart_puts(selectedTextColor);
uart_puts("m");
}
if (my_strcmp(selectedBgColor, "0") != 0) {
uart_puts("33[");
uart_puts(selectedBgColor);
uart_puts("m");
}
}