* @brief Appends @a string to the console, optionally prefixing timestamps. */
| 731 | * @brief Appends @a string to the console, optionally prefixing timestamps. |
| 732 | */ |
| 733 | void Console::Handler::append(const QString& string, const bool addTimestamp) |
| 734 | { |
| 735 | if (string.isEmpty()) |
| 736 | return; |
| 737 | |
| 738 | auto data = string; |
| 739 | if (m_lastCharWasCR && data.startsWith('\n')) |
| 740 | data.removeFirst(); |
| 741 | |
| 742 | m_lastCharWasCR = data.endsWith('\r'); |
| 743 | data = data.replace(QStringLiteral("\r\n"), QStringLiteral("\n")); |
| 744 | data = data.replace(QStringLiteral("\r"), QStringLiteral("\n")); |
| 745 | |
| 746 | QString timestamp; |
| 747 | if (addTimestamp) { |
| 748 | const QString& timeStr = cachedTimestampStr(); |
| 749 | |
| 750 | if (ansiColorsEnabled()) { |
| 751 | const QString ansiCyan = QStringLiteral("\033[36m"); |
| 752 | const QString ansiReset = QStringLiteral("\033[0m"); |
| 753 | timestamp = QStringLiteral("%1%2%3").arg(ansiCyan, timeStr, ansiReset); |
| 754 | } |
| 755 | |
| 756 | else { |
| 757 | timestamp = timeStr; |
| 758 | } |
| 759 | } |
| 760 | |
| 761 | QString processedString; |
| 762 | processedString.reserve(data.length() + timestamp.length() * 4); |
| 763 | int pos = 0; |
| 764 | while (pos < data.length()) { |
| 765 | const int nlPos = data.indexOf('\n', pos); |
| 766 | const int end = (nlPos < 0) ? data.length() : nlPos; |
| 767 | |
| 768 | if (end > pos) { |
| 769 | const auto segment = QStringView(data).mid(pos, end - pos); |
| 770 | if (m_isStartingLine && !segment.trimmed().isEmpty()) |
| 771 | processedString.append(timestamp); |
| 772 | |
| 773 | processedString.append(segment); |
| 774 | m_isStartingLine = false; |
| 775 | } |
| 776 | |
| 777 | if (nlPos >= 0) { |
| 778 | if (m_isStartingLine) |
| 779 | processedString.append(timestamp); |
| 780 | |
| 781 | processedString.append('\n'); |
| 782 | m_isStartingLine = true; |
| 783 | pos = nlPos + 1; |
| 784 | } |
| 785 | |
| 786 | else |
| 787 | pos = end; |
| 788 | } |
| 789 | |
| 790 | m_textBuffer.append(processedString.toUtf8()); |
no test coverage detected