diff --git a/app/src/main/kotlin/org/eclipse/kuksa/testapp/databroker/viewmodel/OutputViewModel.kt b/app/src/main/kotlin/org/eclipse/kuksa/testapp/databroker/viewmodel/OutputViewModel.kt index b7a4f961..8bb6c549 100644 --- a/app/src/main/kotlin/org/eclipse/kuksa/testapp/databroker/viewmodel/OutputViewModel.kt +++ b/app/src/main/kotlin/org/eclipse/kuksa/testapp/databroker/viewmodel/OutputViewModel.kt @@ -42,16 +42,32 @@ class OutputViewModel : ViewModel() { fun appendOutput(text: String) { viewModelScope.launch { withContext(Dispatchers.Main) { + val sanitizedText = sanitizeString(text) + val emptyLines = if (logEntries.isEmpty()) "\n" else "\n\n" val dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss:SSS") val date = LocalDateTime.now().format(dateFormatter) - logEntries += "$emptyLines- $date\n $text" + logEntries += "$emptyLines- $date\n $sanitizedText" output = logEntries.toList() } } } + // fixes a crash when outputting VssPath(Vehicle). The ScrollBar can't handle input with more than 3971 line breaks + private fun sanitizeString(text: String): String { + var sanitizedText = text + val isTextTooLong = sanitizedText.length >= MAX_LENGTH_LOG_ENTRY + if (isTextTooLong) { + sanitizedText = sanitizedText.substring(0, MAX_LENGTH_LOG_ENTRY) + "…" + sanitizedText += System.lineSeparator() + sanitizedText += System.lineSeparator() + sanitizedText += "Text is too long and was truncated" + } + + return sanitizedText + } + fun clear() { viewModelScope.launch { withContext(Dispatchers.Main) { @@ -61,4 +77,8 @@ class OutputViewModel : ViewModel() { } } } + + private companion object { + private const val MAX_LENGTH_LOG_ENTRY = 90_000 + } }