@@ -54,18 +54,20 @@ public class ConsoleOutputStream extends ByteArrayOutputStream {
5454
5555 private final SimpleAttributeSet attributes ;
5656 private final PrintStream printStream ;
57- private final StringBuilder buffer ;
5857 private final Timer timer ;
58+
5959 private volatile EditorConsole editorConsole ;
60+ private volatile boolean newLinePrinted ;
6061
6162 public ConsoleOutputStream (SimpleAttributeSet attributes , PrintStream printStream ) {
6263 this .attributes = attributes ;
6364 this .printStream = printStream ;
64- this .buffer = new StringBuilder () ;
65+ this .newLinePrinted = false ;
6566
66- this .timer = new Timer (100 , (e ) -> {
67- if (editorConsole != null ) {
67+ this .timer = new Timer (500 , (e ) -> {
68+ if (editorConsole != null && newLinePrinted ) {
6869 editorConsole .scrollDown ();
70+ newLinePrinted = false ;
6971 }
7072 });
7173 timer .setRepeats (false );
@@ -76,41 +78,24 @@ public void setCurrentEditorConsole(EditorConsole console) {
7678 }
7779
7880 public synchronized void flush () {
79- String message = toString ();
81+ String text = toString ();
8082
81- if (message .length () == 0 ) {
83+ if (text .length () == 0 ) {
8284 return ;
8385 }
8486
85- handleAppend (message );
87+ printStream .print (text );
88+ printInConsole (text );
8689
8790 reset ();
8891 }
8992
90- private void handleAppend (String message ) {
91- resetBufferIfDocumentEmpty ();
92-
93- buffer .append (message );
94-
95- clearBuffer ();
96- }
97-
98- private void resetBufferIfDocumentEmpty () {
99- if (editorConsole != null && editorConsole .isEmpty ()) {
100- buffer .setLength (0 );
101- }
102- }
103-
104- private void clearBuffer () {
105- String line = buffer .toString ();
106- buffer .setLength (0 );
107-
108- printStream .print (line );
109-
93+ private void printInConsole (String text ) {
94+ newLinePrinted = newLinePrinted || text .contains ("\n " );
11095 if (editorConsole != null ) {
11196 SwingUtilities .invokeLater (() -> {
11297 try {
113- editorConsole .insertString (line , attributes );
98+ editorConsole .insertString (text , attributes );
11499 } catch (BadLocationException ble ) {
115100 //ignore
116101 }
0 commit comments