mirror of
https://github.com/GravitLauncher/Launcher
synced 2024-11-15 03:31:15 +03:00
[FEATURE] Downloader ProgressBar
This commit is contained in:
parent
7f1583c0a3
commit
afa0cd0366
2 changed files with 58 additions and 0 deletions
|
@ -72,6 +72,7 @@ task cleanjar(type: Jar, dependsOn: jar) {
|
||||||
dependencies {
|
dependencies {
|
||||||
pack project(':LauncherAPI')
|
pack project(':LauncherAPI')
|
||||||
pack group: 'org.apache.maven', name: 'maven-artifact', version: '3.8.1'
|
pack group: 'org.apache.maven', name: 'maven-artifact', version: '3.8.1'
|
||||||
|
bundle group: 'me.tongfei', name: 'progressbar', version: '0.9.2'
|
||||||
bundle group: 'org.fusesource.jansi', name: 'jansi', version: rootProject['verJansi']
|
bundle group: 'org.fusesource.jansi', name: 'jansi', version: rootProject['verJansi']
|
||||||
bundle group: 'org.jline', name: 'jline', version: rootProject['verJline']
|
bundle group: 'org.jline', name: 'jline', version: rootProject['verJline']
|
||||||
bundle group: 'org.jline', name: 'jline-reader', version: rootProject['verJline']
|
bundle group: 'org.jline', name: 'jline-reader', version: rootProject['verJline']
|
||||||
|
|
|
@ -1,8 +1,19 @@
|
||||||
package pro.gravit.launchserver.command;
|
package pro.gravit.launchserver.command;
|
||||||
|
|
||||||
|
import me.tongfei.progressbar.ProgressBar;
|
||||||
|
import me.tongfei.progressbar.ProgressBarBuilder;
|
||||||
|
import me.tongfei.progressbar.ProgressBarStyle;
|
||||||
|
import pro.gravit.launcher.AsyncDownloader;
|
||||||
import pro.gravit.launchserver.LaunchServer;
|
import pro.gravit.launchserver.LaunchServer;
|
||||||
|
import pro.gravit.utils.Downloader;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Locale;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.concurrent.CompletableFuture;
|
||||||
|
import java.util.concurrent.atomic.AtomicLong;
|
||||||
|
|
||||||
public abstract class Command extends pro.gravit.utils.command.Command {
|
public abstract class Command extends pro.gravit.utils.command.Command {
|
||||||
protected final LaunchServer server;
|
protected final LaunchServer server;
|
||||||
|
@ -17,4 +28,50 @@ public Command(Map<String, pro.gravit.utils.command.Command> childCommands, Laun
|
||||||
super(childCommands);
|
super(childCommands);
|
||||||
this.server = server;
|
this.server = server;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected boolean showApplyDialog(String text) throws IOException {
|
||||||
|
System.out.printf("%s [Y/N]:", text);
|
||||||
|
String response = server.commandHandler.readLine().toLowerCase(Locale.ROOT);
|
||||||
|
return response.equals("y");
|
||||||
|
}
|
||||||
|
|
||||||
|
protected Downloader downloadWithProgressBar(String taskName, List<AsyncDownloader.SizedFile> list, String baseUrl, Path targetDir) throws Exception {
|
||||||
|
long total = 0;
|
||||||
|
for (AsyncDownloader.SizedFile file : list) {
|
||||||
|
total += file.size;
|
||||||
|
}
|
||||||
|
long totalFiles = list.size();
|
||||||
|
AtomicLong current = new AtomicLong(0);
|
||||||
|
AtomicLong currentFiles = new AtomicLong(0);
|
||||||
|
ProgressBar bar = (new ProgressBarBuilder()).setTaskName(taskName)
|
||||||
|
.setInitialMax(total)
|
||||||
|
.showSpeed()
|
||||||
|
.setStyle(ProgressBarStyle.COLORFUL_UNICODE_BLOCK)
|
||||||
|
.setUnit("MB", 1024 * 1024)
|
||||||
|
.build();
|
||||||
|
bar.setExtraMessage(String.format(" [0/%d]", totalFiles));
|
||||||
|
Downloader downloader = Downloader.downloadList(list, baseUrl, targetDir, new Downloader.DownloadCallback() {
|
||||||
|
@Override
|
||||||
|
public void apply(long fullDiff) {
|
||||||
|
current.addAndGet(fullDiff);
|
||||||
|
bar.stepBy(fullDiff);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onComplete(Path path) {
|
||||||
|
bar.setExtraMessage(String.format(" [%d/%d]", currentFiles.incrementAndGet(), totalFiles));
|
||||||
|
}
|
||||||
|
}, null, 4);
|
||||||
|
downloader.getFuture().handle((v, e) -> {
|
||||||
|
CompletableFuture<Void> future = new CompletableFuture<>();
|
||||||
|
bar.close();
|
||||||
|
if (e != null) {
|
||||||
|
future.completeExceptionally(e);
|
||||||
|
} else {
|
||||||
|
future.complete(null);
|
||||||
|
}
|
||||||
|
return future;
|
||||||
|
});
|
||||||
|
return downloader;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue