Skip to content

Fix issue #8055 missing timestamps on serial monitor #8088

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
fix issue when timestamp is wrongly printed when just a line follow u…
…p should be printed
  • Loading branch information
nitram509 committed Oct 28, 2018
commit cd6331c827d77773f81b89a20c09945fe09d8057
7 changes: 4 additions & 3 deletions app/src/processing/app/AbstractTextMonitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.StringTokenizer;
import java.util.concurrent.atomic.AtomicBoolean;

import javax.swing.Box;
import javax.swing.BoxLayout;
Expand Down Expand Up @@ -181,6 +182,7 @@ public void message(final String msg) {
static class UpdateTextAreaAction implements Runnable {

private static final String LINE_SEPARATOR = "\n";
private static AtomicBoolean isStartingLine = new AtomicBoolean(true);

private String msg;
private boolean addTimeStamp;
Expand Down Expand Up @@ -209,16 +211,15 @@ public void run() {
private String addTimestamps(String text) {
String now = new SimpleDateFormat("HH:mm:ss.SSS -> ").format(new Date());
final StringBuilder sb = new StringBuilder(text.length() + now.length());
boolean isStartingLine = true;
StringTokenizer tokenizer = new StringTokenizer(text, LINE_SEPARATOR, true);
while (tokenizer.hasMoreTokens()) {
if (isStartingLine) {
if (isStartingLine.get()) {
sb.append(now);
}
String token = tokenizer.nextToken();
sb.append(token);
// tokenizer returns "\n" as a single token
isStartingLine = token.equals(LINE_SEPARATOR);
isStartingLine.set(token.equals(LINE_SEPARATOR));
}
return sb.toString();
}
Expand Down
33 changes: 33 additions & 0 deletions app/test/processing/app/UpdateTextAreaActionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public class UpdateTextAreaActionTest {
@Before public void setUp() {
textAreaFIFO = mock(TextAreaFIFO.class);
text = ArgumentCaptor.forClass(String.class);
sendNewLineInOrderToHaveCleanTestStart();
}

@Test
Expand Down Expand Up @@ -64,4 +65,36 @@ public void emptyLinesHaveTimestampToo() {
+ TIMESTAMP_REGEX + " -> line_2");
}

@Test
public void newLinesAreRememberedWhenNewBufferIsUsed() {
// given #1
AbstractTextMonitor.UpdateTextAreaAction action;
action = new AbstractTextMonitor.UpdateTextAreaAction(
textAreaFIFO, true, false, "first line without newline");

// when #1
action.run();

//then #1
verify(textAreaFIFO, atLeastOnce()).append(text.capture());
assertThat(text.getValue()).matches(TIMESTAMP_REGEX + " -> first line without newline");

// given #2
action = new AbstractTextMonitor.UpdateTextAreaAction(
textAreaFIFO, true, false, "more text for first line");

// when #2
action.run();

//then #2
verify(textAreaFIFO, atLeastOnce()).append(text.capture());
assertThat(text.getValue()).matches("more text for first line");
}


private void sendNewLineInOrderToHaveCleanTestStart() {
AbstractTextMonitor.UpdateTextAreaAction action = new AbstractTextMonitor.UpdateTextAreaAction(
textAreaFIFO, true, false, "\n");
action.run();
}
}