From 66f82c4c90a1cdae29e2c7620db5a4ca70b8aa51 Mon Sep 17 00:00:00 2001 From: Crementif <26669564+Crementif@users.noreply.github.com> Date: Sat, 29 Jul 2023 19:02:27 +0200 Subject: [PATCH] Support LF line-ending mode and fix split-up line-endings This commit adds LF ("\n") line ending support, completing coverage for all line endings ("\r", "\n", "\r\n"). It also provides a robust solution for line endings split across chunks (the issue from the todo comment that points to https://bitbucket.org/dmitry_cherkas/intellij-serial-monitor/issues/1), replacing the previous temporary fix. Since the bitbucket repo is offline I had to use https://github.com/ribasco/intellij-serial-monitor/commit/1414af04540c3e28019a270aefe89b1e74d3da86 to find the linked issue. --- .../SerialMonitorDuplexConsoleView.java | 22 ++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/serial-monitor/src/main/java/com/intellij/plugins/serialmonitor/ui/console/SerialMonitorDuplexConsoleView.java b/serial-monitor/src/main/java/com/intellij/plugins/serialmonitor/ui/console/SerialMonitorDuplexConsoleView.java index a17396e81b1..0bfff0dfbbd 100644 --- a/serial-monitor/src/main/java/com/intellij/plugins/serialmonitor/ui/console/SerialMonitorDuplexConsoleView.java +++ b/serial-monitor/src/main/java/com/intellij/plugins/serialmonitor/ui/console/SerialMonitorDuplexConsoleView.java @@ -242,10 +242,26 @@ public void actionPerformed(final @NotNull AnActionEvent e) { } } + private byte lastByte = '\0'; + public void append(byte[] dataChunk) { - // todo quick and dirty fix for https://bitbucket.org/dmitry_cherkas/intellij-serial-monitor/issues/1 - //todo crlf - String text = new String(dataChunk, getCharset()).replaceAll("\r", ""); + Charset charset = getCharset(); + StringBuilder sb = new StringBuilder(); + + // handle the case where \r\n is split across chunks + if (lastByte == '\r' && dataChunk.length > 0 && dataChunk[0] == '\n') { + sb.append(new String(dataChunk, 1, dataChunk.length - 1, charset)); + } + else { + sb.append(new String(dataChunk, charset)); + } + + // update the last byte of the chunk + if (dataChunk.length > 0) { + lastByte = dataChunk[dataChunk.length - 1]; + } + + String text = sb.toString().replaceAll("\r\n?", "\n"); getPrimaryConsoleView().print(text, ConsoleViewContentType.NORMAL_OUTPUT); getSecondaryConsoleView().output(dataChunk); }