diff --git a/LaunchServer/build.gradle b/LaunchServer/build.gradle index 680d2052..af6dffac 100644 --- a/LaunchServer/build.gradle +++ b/LaunchServer/build.gradle @@ -72,6 +72,7 @@ task cleanjar(type: Jar, dependsOn: jar) { dependencies { pack project(':LauncherAPI') 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.jline', name: 'jline', version: rootProject['verJline'] bundle group: 'org.jline', name: 'jline-reader', version: rootProject['verJline'] diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/command/Command.java b/LaunchServer/src/main/java/pro/gravit/launchserver/command/Command.java index 684d79e8..e7daa403 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/command/Command.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/command/Command.java @@ -1,8 +1,19 @@ 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.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.concurrent.CompletableFuture; +import java.util.concurrent.atomic.AtomicLong; public abstract class Command extends pro.gravit.utils.command.Command { protected final LaunchServer server; @@ -17,4 +28,50 @@ public Command(Map childCommands, Laun super(childCommands); 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 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 future = new CompletableFuture<>(); + bar.close(); + if (e != null) { + future.completeExceptionally(e); + } else { + future.complete(null); + } + return future; + }); + return downloader; + } }