Properly handle nick hilights by only hilighting the first occurance

This commit is contained in:
Dionysus 2024-05-25 23:49:13 -04:00
parent 2cb5b2ccae
commit b5e0585302
Signed by: acidvegas
GPG Key ID: EF4B922DB85DC9DE

View File

@ -89,6 +89,83 @@ void setup() {
nick = "ACID_" + String(randomNum); nick = "ACID_" + String(randomNum);
} }
int renderFormattedMessage(String message, int cursorY, int lineHeight, bool highlightNick = false) {
uint16_t fgColor = TFT_WHITE;
uint16_t bgColor = TFT_BLACK;
bool bold = false;
bool underline = false;
bool nickHighlighted = false; // Track if the nick has been highlighted
for (unsigned int i = 0; i < message.length(); i++) {
char c = message[i];
if (c == '\x02') { // Bold
bold = !bold;
tft.setTextFont(bold ? 2 : 1);
} else if (c == '\x1F') { // Underline
underline = !underline;
// need to add this still
} else if (c == '\x03') { // Color
fgColor = TFT_WHITE;
bgColor = TFT_BLACK;
if (i + 1 < message.length() && (isdigit(message[i + 1]) || message[i + 1] == ',')) {
int colorCode = -1;
if (isdigit(message[i + 1])) {
colorCode = message[++i] - '0';
if (i + 1 < message.length() && isdigit(message[i + 1]))
colorCode = colorCode * 10 + (message[++i] - '0');
}
if (colorCode != -1)
fgColor = getColorFromCode(colorCode);
if (i + 1 < message.length() && message[i + 1] == ',') {
i++;
int bgColorCode = -1;
if (isdigit(message[i + 1])) {
bgColorCode = message[++i] - '0';
if (i + 1 < message.length() && isdigit(message[i + 1]))
bgColorCode = bgColorCode * 10 + (message[++i] - '0');
}
if (bgColorCode != -1)
bgColor = getColorFromCode(bgColorCode);
}
tft.setTextColor(fgColor, bgColor);
}
} else if (c == '\x0F') { // Reset
fgColor = TFT_WHITE;
bgColor = TFT_BLACK;
bold = false;
underline = false;
tft.setTextColor(fgColor, bgColor);
tft.setTextFont(1);
} else {
if (highlightNick && !nickHighlighted && message.substring(i).startsWith(nick)) {
tft.setTextColor(TFT_YELLOW);
for (char nc : nick) {
tft.print(nc);
i++;
}
i--; // Adjust for the loop increment
tft.setTextColor(TFT_WHITE);
nickHighlighted = true;
} else {
if (tft.getCursorX() + tft.textWidth(String(c)) > SCREEN_WIDTH) {
cursorY += lineHeight;
tft.setCursor(0, cursorY);
}
tft.print(c);
}
}
}
cursorY += lineHeight; // Add line height after printing the message
return cursorY; // Return the new cursor Y position for the next line
}
void displayLines() { void displayLines() {
tft.fillRect(0, STATUS_BAR_HEIGHT, SCREEN_WIDTH, SCREEN_HEIGHT - STATUS_BAR_HEIGHT - INPUT_LINE_HEIGHT, TFT_BLACK); tft.fillRect(0, STATUS_BAR_HEIGHT, SCREEN_WIDTH, SCREEN_HEIGHT - STATUS_BAR_HEIGHT - INPUT_LINE_HEIGHT, TFT_BLACK);
@ -179,17 +256,9 @@ void displayLines() {
// Check if the message contains the nick and highlight it // Check if the message contains the nick and highlight it
int nickPos = message.indexOf(nick); int nickPos = message.indexOf(nick);
if (mention && nickPos != -1) { if (mention && nickPos != -1) {
// Print part before the nick cursorY = renderFormattedMessage(message, cursorY, CHAR_HEIGHT, true);
tft.print(message.substring(0, nickPos));
// Print the nick in yellow
tft.setTextColor(TFT_YELLOW);
tft.print(nick);
// Print the part after the nick
tft.setTextColor(TFT_WHITE);
tft.print(message.substring(nickPos + nick.length()));
cursorY += CHAR_HEIGHT; // Ensure cursor moves to the next line after the highlighted message
} else { } else {
cursorY = renderFormattedMessage(message, cursorY, CHAR_HEIGHT); cursorY = renderFormattedMessage(message, cursorY, CHAR_HEIGHT, false);
} }
} }
} }
@ -198,9 +267,6 @@ void displayLines() {
} }
void addLine(String senderNick, String message, String type, bool mention = false) { void addLine(String senderNick, String message, String type, bool mention = false) {
if (nickColors.find(senderNick) == nickColors.end()) if (nickColors.find(senderNick) == nickColors.end())
nickColors[senderNick] = generateRandomColor(); nickColors[senderNick] = generateRandomColor();
@ -455,72 +521,6 @@ void displayCenteredText(String text) {
tft.drawString(text, SCREEN_WIDTH / 2, (SCREEN_HEIGHT + STATUS_BAR_HEIGHT) / 2); tft.drawString(text, SCREEN_WIDTH / 2, (SCREEN_HEIGHT + STATUS_BAR_HEIGHT) / 2);
} }
int renderFormattedMessage(String message, int cursorY, int lineHeight) {
uint16_t fgColor = TFT_WHITE;
uint16_t bgColor = TFT_BLACK;
bool bold = false;
bool underline = false;
for (unsigned int i = 0; i < message.length(); i++) {
char c = message[i];
if (c == '\x02') { // Bold
bold = !bold;
tft.setTextFont(bold ? 2 : 1);
} else if (c == '\x1F') { // Underline
underline = !underline;
// need to add this still
} else if (c == '\x03') { // Color
fgColor = TFT_WHITE;
bgColor = TFT_BLACK;
if (i + 1 < message.length() && (isdigit(message[i + 1]) || message[i + 1] == ',')) {
int colorCode = -1;
if (isdigit(message[i + 1])) {
colorCode = message[++i] - '0';
if (i + 1 < message.length() && isdigit(message[i + 1]))
colorCode = colorCode * 10 + (message[++i] - '0');
}
if (colorCode != -1)
fgColor = getColorFromCode(colorCode);
if (i + 1 < message.length() && message[i + 1] == ',') {
i++;
int bgColorCode = -1;
if (isdigit(message[i + 1])) {
bgColorCode = message[++i] - '0';
if (i + 1 < message.length() && isdigit(message[i + 1]))
bgColorCode = bgColorCode * 10 + (message[++i] - '0');
}
if (bgColorCode != -1)
bgColor = getColorFromCode(bgColorCode);
}
tft.setTextColor(fgColor, bgColor);
}
} else if (c == '\x0F') { // Reset
fgColor = TFT_WHITE;
bgColor = TFT_BLACK;
bold = false;
underline = false;
tft.setTextColor(fgColor, bgColor);
tft.setTextFont(1);
} else {
if (tft.getCursorX() + tft.textWidth(String(c)) > SCREEN_WIDTH) {
cursorY += lineHeight;
tft.setCursor(0, cursorY);
}
tft.print(c);
}
}
cursorY += lineHeight; // Add line height after printing the message
return cursorY; // Return the new cursor Y position for the next line
}
int calculateLinesRequired(String message) { int calculateLinesRequired(String message) {
int linesRequired = 1; int linesRequired = 1;
int lineWidth = 0; int lineWidth = 0;
@ -746,7 +746,7 @@ uint16_t getColorFromPercentage(int rssi) {
void updateTimeFromNTP() { void updateTimeFromNTP() {
configTime(-5 * 3600, 0, "pool.ntp.org", "time.nist.gov"); configTime(-5 * 3600, 0, "pool.ntp.org", "time.nist.gov");
delay(2000); delay(2000); // Wait for NTP to sync
struct tm timeinfo; struct tm timeinfo;
if (getLocalTime(&timeinfo)) { if (getLocalTime(&timeinfo)) {
Serial.println(&timeinfo, "Time synchronized: %A, %B %d %Y %H:%M:%S"); Serial.println(&timeinfo, "Time synchronized: %A, %B %d %Y %H:%M:%S");