Skip to content

Commit

Permalink
this fixes off-by-one issues that are only visible if there is extra …
Browse files Browse the repository at this point in the history
…room atthe bottom of the terminal
  • Loading branch information
jurgenvinju committed Apr 3, 2024
1 parent 1028f72 commit f26a445
Showing 1 changed file with 45 additions and 3 deletions.
48 changes: 45 additions & 3 deletions src/org/rascalmpl/repl/TerminalProgressBarMonitor.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.rascalmpl.repl;

import java.io.FilterOutputStream;
import java.io.FilterWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
Expand Down Expand Up @@ -49,13 +50,53 @@ public class TerminalProgressBarMonitor extends FilterOutputStream implements IR
*/
private final String encoding;

/**
* Will make everything slow, but easier to spot mistakes
*/
private final boolean debug = false;

@SuppressWarnings("resource")
public TerminalProgressBarMonitor(OutputStream out, Terminal tm) {
super(out);
this.encoding = tm.getOutputEncoding() != null ? tm.getOutputEncoding() : "UTF8";
this.writer = new PrintWriter(out, true, Charset.forName(encoding));
PrintWriter theWriter = new PrintWriter(out, true, Charset.forName(encoding));
this.writer = debug ? new PrintWriter(new AlwaysFlushAlwaysShowCursor(theWriter)) : theWriter;
this.lineWidth = tm.getWidth();
}

/**
* Use this for debugging terminal cursor movements, step by step.
*/
private static class AlwaysFlushAlwaysShowCursor extends FilterWriter {

public AlwaysFlushAlwaysShowCursor(PrintWriter out) {
super(out);
}

Check warning on line 74 in src/org/rascalmpl/repl/TerminalProgressBarMonitor.java

View check run for this annotation

Codecov / codecov/patch

src/org/rascalmpl/repl/TerminalProgressBarMonitor.java#L73-L74

Added lines #L73 - L74 were not covered by tests

@Override
public void write(int c) throws IOException {
out.write(c);
out.write(ANSI.showCursor());
out.flush();
}

Check warning on line 81 in src/org/rascalmpl/repl/TerminalProgressBarMonitor.java

View check run for this annotation

Codecov / codecov/patch

src/org/rascalmpl/repl/TerminalProgressBarMonitor.java#L78-L81

Added lines #L78 - L81 were not covered by tests

@Override
public void write(char[] cbuf, int off, int len) throws IOException {
out.write(cbuf, off, len);
out.write(ANSI.showCursor());
out.flush();
}

Check warning on line 88 in src/org/rascalmpl/repl/TerminalProgressBarMonitor.java

View check run for this annotation

Codecov / codecov/patch

src/org/rascalmpl/repl/TerminalProgressBarMonitor.java#L85-L88

Added lines #L85 - L88 were not covered by tests

@Override
public void write(String str, int off, int len) throws IOException {
out.write(str, off, len);
out.write(ANSI.showCursor());
out.flush();
}

Check warning on line 95 in src/org/rascalmpl/repl/TerminalProgressBarMonitor.java

View check run for this annotation

Codecov / codecov/patch

src/org/rascalmpl/repl/TerminalProgressBarMonitor.java#L92-L95

Added lines #L92 - L95 were not covered by tests


}

/**
* Represents one currently running progress bar
*/
Expand Down Expand Up @@ -102,7 +143,7 @@ void update() {
writer.write(ANSI.hideCursor());
writer.write(ANSI.moveUp(bars.size() - bars.indexOf(this)));
write();
writer.write(ANSI.moveDown(bars.size() - bars.indexOf(this)));
writer.write(ANSI.moveDown(bars.size() - bars.indexOf(this) - 1 /* already wrote a \n */));
writer.write(ANSI.showCursor());
writer.flush();
}
Expand Down Expand Up @@ -203,7 +244,8 @@ public void done() {
*/
private void eraseBars() {
if (bars.isEmpty()) {
writer.println();
// writer.println();
return;
}

writer.write(ANSI.hideCursor());
Expand Down

0 comments on commit f26a445

Please sign in to comment.