Skip to content

Commit 8e0a311

Browse files
committed
SerialMonitor: limit buffering without autoscroll
When the "autoscroll" checkbox is deselected the buffer may continue to grow up to twice of the maximum size. This is a compromise to ensure a better user experience and, at the same time, reduce the chance to lose data and get "holes" in the serial stream. See #2491
1 parent 63f5d26 commit 8e0a311

File tree

2 files changed

+25
-11
lines changed

2 files changed

+25
-11
lines changed

Diff for: app/src/processing/app/SerialMonitor.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public void actionPerformed(ActionEvent e) {
6969
Font editorFont = Preferences.getFont("editor.font");
7070
Font font = new Font(consoleFont.getName(), consoleFont.getStyle(), editorFont.getSize());
7171

72-
textArea = new TextAreaFIFO(4000000);
72+
textArea = new TextAreaFIFO(8000000);
7373
textArea.setRows(16);
7474
textArea.setColumns(40);
7575
textArea.setEditable(false);
@@ -244,11 +244,11 @@ public void actionPerformed(ActionEvent e) {
244244
final String s = consumeUpdateBuffer();
245245
if (s.length() > 0) {
246246
//System.out.println("gui append " + s.length());
247-
boolean scroll = autoscrollBox.isSelected();
248-
textArea.allowTrim(scroll);
249-
textArea.append(s);
250-
if (scroll) {
247+
if (autoscrollBox.isSelected()) {
248+
textArea.appendTrim(s);
251249
textArea.setCaretPosition(textArea.getDocument().getLength());
250+
} else {
251+
textArea.appendNoTrim(s);
252252
}
253253
}
254254
}

Diff for: app/src/processing/app/debug/TextAreaFIFO.java

+20-6
Original file line numberDiff line numberDiff line change
@@ -30,22 +30,20 @@
3030

3131
public class TextAreaFIFO extends JTextArea implements DocumentListener {
3232
private int maxChars;
33+
private int trimMaxChars;
3334

3435
private int updateCount; // limit how often we trim the document
3536

3637
private boolean doTrim;
3738

3839
public TextAreaFIFO(int max) {
3940
maxChars = max;
41+
trimMaxChars = max / 2;
4042
updateCount = 0;
4143
doTrim = true;
4244
getDocument().addDocumentListener(this);
4345
}
4446

45-
public void allowTrim(boolean trim) {
46-
doTrim = trim;
47-
}
48-
4947
public void insertUpdate(DocumentEvent e) {
5048
if (++updateCount > 150 && doTrim) {
5149
updateCount = 0;
@@ -66,13 +64,29 @@ public void changedUpdate(DocumentEvent e) {
6664
public void trimDocument() {
6765
int len = 0;
6866
len = getDocument().getLength();
69-
if (len > maxChars) {
70-
int n = len - maxChars;
67+
if (len > trimMaxChars) {
68+
int n = len - trimMaxChars;
7169
//System.out.println("trimDocument: remove " + n + " chars");
7270
try {
7371
getDocument().remove(0, n);
7472
} catch (BadLocationException ble) {
7573
}
7674
}
7775
}
76+
77+
public void appendNoTrim(String s) {
78+
int free = maxChars - getDocument().getLength();
79+
if (free <= 0)
80+
return;
81+
if (s.length() > free)
82+
append(s.substring(0, free));
83+
else
84+
append(s);
85+
doTrim = false;
86+
}
87+
88+
public void appendTrim(String str) {
89+
append(str);
90+
doTrim = true;
91+
}
7892
}

0 commit comments

Comments
 (0)