This repository has been archived by the owner on May 12, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/main'
- Loading branch information
Showing
6 changed files
with
326 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
...rc/main/java/net/raphimc/javadowngrader/standalone/progress/MultiThreadedProgressBar.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
* This file is part of JavaDowngrader - https://github.com/RaphiMC/JavaDowngrader | ||
* Copyright (C) 2023 RK_01/RaphiMC and contributors | ||
* | ||
* This program is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 3 of the License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
package net.raphimc.javadowngrader.standalone.progress; | ||
|
||
import me.tongfei.progressbar.ProgressBarBuilder; | ||
|
||
public interface MultiThreadedProgressBar extends AutoCloseable { | ||
void step(); | ||
|
||
void setThreadTask(String task); | ||
|
||
@Override | ||
void close(); | ||
|
||
static MultiThreadedProgressBar create(ProgressBarBuilder bar) { | ||
return TerminalUtils.hasCursorMovementSupport() | ||
? new ThreadedLineProgressBar(bar) | ||
: new SimpleProgressBar(bar.build()); | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
...alone/src/main/java/net/raphimc/javadowngrader/standalone/progress/SimpleProgressBar.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
* This file is part of JavaDowngrader - https://github.com/RaphiMC/JavaDowngrader | ||
* Copyright (C) 2023 RK_01/RaphiMC and contributors | ||
* | ||
* This program is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 3 of the License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
package net.raphimc.javadowngrader.standalone.progress; | ||
|
||
import me.tongfei.progressbar.ProgressBar; | ||
|
||
class SimpleProgressBar implements MultiThreadedProgressBar { | ||
protected final ProgressBar bar; | ||
|
||
public SimpleProgressBar(ProgressBar bar) { | ||
this.bar = bar; | ||
} | ||
|
||
@Override | ||
public void step() { | ||
bar.step(); | ||
} | ||
|
||
@Override | ||
public void setThreadTask(String task) { | ||
} | ||
|
||
@Override | ||
public void close() { | ||
bar.close(); | ||
} | ||
} |
119 changes: 119 additions & 0 deletions
119
standalone/src/main/java/net/raphimc/javadowngrader/standalone/progress/TerminalUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
/* | ||
* The MIT License (MIT) | ||
* | ||
* Copyright (c) 2015--2020 Tongfei Chen and contributors | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all | ||
* copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
*/ | ||
package net.raphimc.javadowngrader.standalone.progress; | ||
|
||
import me.tongfei.progressbar.ProgressBarConsumer; | ||
import org.jline.terminal.Terminal; | ||
import org.jline.terminal.TerminalBuilder; | ||
import org.jline.utils.InfoCmp; | ||
|
||
import java.io.IOException; | ||
import java.util.Queue; | ||
import java.util.concurrent.ConcurrentLinkedQueue; | ||
import java.util.stream.Stream; | ||
|
||
/** | ||
* @author Martin Vehovsky | ||
* @since 0.9.0 | ||
*/ | ||
class TerminalUtils { | ||
|
||
static final char CARRIAGE_RETURN = '\r'; | ||
static final char ESCAPE_CHAR = '\u001b'; | ||
static final int DEFAULT_TERMINAL_WIDTH = 80; | ||
|
||
private static Terminal terminal = null; | ||
private static boolean cursorMovementSupported = false; | ||
|
||
static Queue<ProgressBarConsumer> activeConsumers = new ConcurrentLinkedQueue<>(); | ||
|
||
synchronized static int getTerminalWidth() { | ||
Terminal terminal = getTerminal(); | ||
int width = terminal.getWidth(); | ||
|
||
// Workaround for issue #23 under IntelliJ | ||
return (width >= 10) ? width : DEFAULT_TERMINAL_WIDTH; | ||
} | ||
|
||
static boolean hasCursorMovementSupport() { | ||
if (terminal == null) | ||
terminal = getTerminal(); | ||
return cursorMovementSupported; | ||
} | ||
|
||
synchronized static void closeTerminal() { | ||
try { | ||
if (terminal != null) { | ||
terminal.close(); | ||
terminal = null; | ||
} | ||
} catch (IOException ignored) { /* noop */ } | ||
} | ||
|
||
static <T extends ProgressBarConsumer> Stream<T> filterActiveConsumers(Class<T> clazz) { | ||
return activeConsumers.stream() | ||
.filter(clazz::isInstance) | ||
.map(clazz::cast); | ||
} | ||
|
||
static String moveCursorUp(int count) { | ||
return ESCAPE_CHAR + "[" + count + "A" + CARRIAGE_RETURN; | ||
} | ||
|
||
static String moveCursorDown(int count) { | ||
return ESCAPE_CHAR + "[" + count + "B" + CARRIAGE_RETURN; | ||
} | ||
|
||
/** | ||
* <ul> | ||
* <li>Creating terminal is relatively expensive, usually takes between 5-10ms. | ||
* <ul> | ||
* <li>If updateInterval is set under 10ms creating new terminal for on every re-render of progress bar could be a problem.</li> | ||
* <li>Especially when multiple progress bars are running in parallel.</li> | ||
* </ul> | ||
* </li> | ||
* <li>Another problem with {@link Terminal} is that once created you can create another instance (say from different thread), but this instance will be | ||
* "dumb". Until previously created terminal will be closed. | ||
* </li> | ||
* </ul> | ||
*/ | ||
static Terminal getTerminal() { | ||
if (terminal == null) { | ||
try { | ||
// Issue #42 | ||
// Defaulting to a dumb terminal when a supported terminal can not be correctly created | ||
// see https://github.com/jline/jline3/issues/291 | ||
terminal = TerminalBuilder.builder().dumb(true).build(); | ||
cursorMovementSupported = ( | ||
terminal.getStringCapability(InfoCmp.Capability.cursor_up) != null && | ||
terminal.getStringCapability(InfoCmp.Capability.cursor_down) != null | ||
); | ||
} catch (IOException e) { | ||
throw new RuntimeException("This should never happen! Dumb terminal should have been created."); | ||
} | ||
} | ||
return terminal; | ||
} | ||
|
||
} |
Oops, something went wrong.