From: David ‘Bombe’ Roden Date: Wed, 2 Oct 2013 18:15:24 +0000 (+0200) Subject: Add progress bar to download listing. X-Git-Url: https://git.pterodactylus.net/?p=xudocci.git;a=commitdiff_plain;h=6df7a0723832a5191296016ecd4c5b64ea881902 Add progress bar to download listing. --- diff --git a/src/main/java/net/pterodactylus/xdcc/ui/stdin/ListDownloadsCommand.java b/src/main/java/net/pterodactylus/xdcc/ui/stdin/ListDownloadsCommand.java index 8a19dff..c352cdf 100644 --- a/src/main/java/net/pterodactylus/xdcc/ui/stdin/ListDownloadsCommand.java +++ b/src/main/java/net/pterodactylus/xdcc/ui/stdin/ListDownloadsCommand.java @@ -35,6 +35,7 @@ import net.pterodactylus.irc.DccReceiver; import net.pterodactylus.xdcc.core.Core; import net.pterodactylus.xdcc.data.Download; +import com.google.common.collect.FluentIterable; import com.google.common.collect.Ordering; /** @@ -44,6 +45,8 @@ import com.google.common.collect.Ordering; */ public class ListDownloadsCommand implements Command { + private static final int PROGRESS_BAR_WIDTH = 10; + /** The core to operate on. */ private final Core core; @@ -82,7 +85,7 @@ public class ListDownloadsCommand implements Command { outputWriter.write(String.format("[%d] %s requested from %s (not started yet)\n", counter++, download.pack().name(), download.bot().name())); continue; } - outputWriter.write(String.format("[%d] %s from %s (%s, ", counter++, dccReceiver.filename(), download.bot().name(), f(dccReceiver.size()))); + outputWriter.write(String.format("[%d] %s %s from %s (%s, ", counter++, getProgressBar(dccReceiver, PROGRESS_BAR_WIDTH), dccReceiver.filename(), download.bot().name(), f(dccReceiver.size()))); if (dccReceiver.isRunning()) { outputWriter.write(String.format("%.1f%%, %s/s, %s", dccReceiver.progress() * 100.0 / dccReceiver.size(), f(dccReceiver.currentRate()), t(SECONDS_LEFT.apply(download)))); } else { @@ -99,4 +102,32 @@ public class ListDownloadsCommand implements Command { return state.setLastDownloads(downloads); } + /** + * Creates a progress bar for the given DCC receiver. + * + * @param dccReceiver + * The DCC receiver to create the progress bar for + * @param progressBarWidth + * The width of the progress bar (in characters) + * @return The progress bar for the given DCC receiver + */ + private static String getProgressBar(DccReceiver dccReceiver, int progressBarWidth) { + FluentIterable partialProgressCharacters = from(asList(' ', '\u258f', '\u258e', '\u258d', '\u258c', '\u258b', '\u258a', '\u2589', '\u2588')); + double progress = dccReceiver.progress() * 100.0 / dccReceiver.size(); + double singleBlockWidth = 100.0 / progressBarWidth; + int fullProgressBlocks = (int) (progress / singleBlockWidth); + double lastBlockProgress = (progress - fullProgressBlocks * singleBlockWidth) / singleBlockWidth; + StringBuilder progressBar = new StringBuilder(progressBarWidth); + for (int i = 0; i < progressBarWidth; ++i) { + if (i < fullProgressBlocks) { + progressBar.append(partialProgressCharacters.last().get()); + } else if (i > fullProgressBlocks) { + progressBar.append(partialProgressCharacters.first().get()); + } else { + progressBar.append(partialProgressCharacters.get((int) (lastBlockProgress * (partialProgressCharacters.size() - 1)))); + } + } + return progressBar.toString(); + } + }