Skip to content

Commit 77d9e0a

Browse files
Handle CR without NL printed in EditorConsole
Previously, any CR without NL was treated just like a NL. For tools that used single CRs to update a progress bar (such as dfu-util), this would end up printing the subsequent versions of the progress bar below each other, instead of updating a single line as intended. Additionally, since ConsoleOutputStream only scrolled the view on \n, these updates would end up outside of the main view, making the upload progress quite unclear. This commit makes EditorConsole support lone CRs by resetting the insert position to the start of the current line, so subsequent writes overwrite existing content. If subsequent lines are shorter than an earlier line, only part of the earlier line will be overwritten (this mimics what terminal emulators do).
1 parent cd11c2d commit 77d9e0a

File tree

1 file changed

+48
-4
lines changed

1 file changed

+48
-4
lines changed

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

+48-4
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
import javax.swing.text.*;
2828
import java.awt.*;
2929
import java.io.PrintStream;
30+
import java.util.regex.Matcher;
31+
import java.util.regex.Pattern;
3032

3133
import static processing.app.Theme.scale;
3234

@@ -37,6 +39,8 @@ public class EditorConsole extends JScrollPane {
3739

3840
private static ConsoleOutputStream out;
3941
private static ConsoleOutputStream err;
42+
private static int startOfLine = 0;
43+
private static int insertPosition = 0;
4044

4145
public static synchronized void setCurrentEditorConsole(EditorConsole console) {
4246
if (out == null) {
@@ -161,6 +165,8 @@ public void applyPreferences() {
161165
public void clear() {
162166
try {
163167
document.remove(0, document.getLength());
168+
startOfLine = 0;
169+
insertPosition = 0;
164170
} catch (BadLocationException e) {
165171
// ignore the error otherwise this will cause an infinite loop
166172
// maybe not a good idea in the long run?
@@ -176,10 +182,48 @@ public boolean isEmpty() {
176182
return document.getLength() == 0;
177183
}
178184

179-
public void insertString(String line, SimpleAttributeSet attributes) throws BadLocationException {
180-
line = line.replace("\r\n", "\n").replace("\r", "\n");
181-
int offset = document.getLength();
182-
document.insertString(offset, line, attributes);
185+
public void insertString(String str, SimpleAttributeSet attributes) throws BadLocationException {
186+
// Separate the string into content, newlines and lone carriage
187+
// returns.
188+
//
189+
// Doing so allows lone CRs to return the insertPosition to the
190+
// start of the line to allow overwriting the most recent line (e.g.
191+
// for a progress bar). Any CR or NL that are immediately followed
192+
// by another NL are bunched together for efficiency, since these
193+
// can just be inserted into the document directly and still be
194+
// correct.
195+
//
196+
// This regex is written so it will necessarily match any string
197+
// completely if applied repeatedly. This is important because any
198+
// part not matched would be silently dropped.
199+
Matcher m = Pattern.compile("([^\r\n]*)([\r\n]*\n)?(\r+)?").matcher(str);
200+
while (m.find()) {
201+
String content = m.group(1);
202+
String newlines = m.group(2);
203+
String crs = m.group(3);
204+
205+
// Replace (or append if at end of the document) the content first
206+
int replaceLength = Math.min(content.length(), document.getLength() - insertPosition);
207+
document.replace(insertPosition, replaceLength, content, attributes);
208+
insertPosition += content.length();
209+
210+
// Then insert any newlines, but always at the end of the document
211+
// e.g. if insertPosition is halfway a line, do not delete
212+
// anything, just add the newline(s) at the end).
213+
if (newlines != null) {
214+
document.insertString(document.getLength(), newlines, attributes);
215+
insertPosition = document.getLength();
216+
startOfLine = insertPosition;
217+
}
218+
219+
// Then, for any CRs not followed by newlines, move insertPosition
220+
// to the start of the line. Note that if a newline follows before
221+
// any content in the next call to insertString, it will be added
222+
// at the end of the document anyway, as expected.
223+
if (crs != null) {
224+
insertPosition = startOfLine;
225+
}
226+
}
183227
}
184228

185229
public String getText() {

0 commit comments

Comments
 (0)