Skip to content

Commit 4b24c45

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 a1e43ce commit 4b24c45

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
private static synchronized void init(SimpleAttributeSet outStyle, PrintStream outStream, SimpleAttributeSet errStyle, PrintStream errStream) {
4246
if (out != null) {
@@ -164,6 +168,8 @@ public void applyPreferences() {
164168
public void clear() {
165169
try {
166170
document.remove(0, document.getLength());
171+
startOfLine = 0;
172+
insertPosition = 0;
167173
} catch (BadLocationException e) {
168174
// ignore the error otherwise this will cause an infinite loop
169175
// maybe not a good idea in the long run?
@@ -179,10 +185,48 @@ public boolean isEmpty() {
179185
return document.getLength() == 0;
180186
}
181187

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

188232
public String getText() {

0 commit comments

Comments
 (0)