Skip to content

Commit

Permalink
Fix the custom console with printing progress bars
Browse files Browse the repository at this point in the history
  • Loading branch information
FlamingKnight committed Jun 18, 2024
1 parent e670d20 commit d96d776
Showing 1 changed file with 28 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public class ConsolePanel extends JTextPane {

private static Color colorCurrent = ANSIColor.RESET.getColor();
private String remaining = "";
int currentLength = 0; // Used to let ProgressBars work

/**
* Append the given string in the given color to the text pane
Expand All @@ -58,6 +59,33 @@ private void append(Color c, String s) {
AttributeSet aset = sc.addAttribute(SimpleAttributeSet.EMPTY, StyleConstants.Foreground, c);
int len = getDocument().getLength();

if(s.contains("\r")) {
// We have a carriage so we should be at the front of the line
boolean haveNewline = s.contains("\n");
if(haveNewline) {
// We're good to normally add to the document
try {
getDocument().insertString(len, s, aset);
} catch (BadLocationException e) {
log.error("Error while appending text to console", e);
}
currentLength = 0;
return;
}

// There's no newline, we should erase our progress to the start
try {
getDocument().remove(len-currentLength, currentLength);
getDocument().insertString(len-currentLength, s, aset);
currentLength = s.length();
} catch (BadLocationException e) {
log.error("Error while removing text from console, most likely has to do with printing weirdly", e);
}
return;
}

currentLength += s.length();

try {
getDocument().insertString(len, s, aset);
} catch (BadLocationException e) {
Expand Down

1 comment on commit d96d776

@smartcmd
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool!

Please sign in to comment.