2 * XdccDownloader - ListDownloadsCommand.java - Copyright © 2013 David Roden
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package net.pterodactylus.xdcc.ui.stdin;
20 import static com.google.common.collect.FluentIterable.from;
21 import static com.google.common.collect.Lists.newArrayList;
22 import static java.util.Arrays.asList;
23 import static net.pterodactylus.xdcc.data.Download.BY_NAME;
24 import static net.pterodactylus.xdcc.data.Download.BY_RUNNING;
25 import static net.pterodactylus.xdcc.data.Download.SECONDS_LEFT;
26 import static net.pterodactylus.xdcc.ui.stdin.CommandReader.f;
27 import static net.pterodactylus.xdcc.ui.stdin.CommandReader.t;
29 import java.io.IOException;
30 import java.io.Writer;
31 import java.util.Collection;
32 import java.util.List;
34 import net.pterodactylus.irc.DccReceiver;
35 import net.pterodactylus.xdcc.core.Core;
36 import net.pterodactylus.xdcc.data.Download;
38 import com.google.common.collect.FluentIterable;
39 import com.google.common.collect.Ordering;
42 * Command that will list all current downloads.
44 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
46 public class ListDownloadsCommand implements Command {
48 private static final int PROGRESS_BAR_WIDTH = 10;
50 /** The core to operate on. */
51 private final Core core;
54 * Creates a new list downloads command.
57 * The core to operate on
59 public ListDownloadsCommand(Core core) {
68 public String getName() {
73 public Collection<String> getAliases() {
78 public State execute(State state, List<String> parameters, Writer outputWriter) throws IOException {
80 List<Download> downloads = newArrayList(from(core.downloads()).toSortedList(Ordering.from(BY_RUNNING).compound(BY_NAME)));
81 for (Download download : downloads) {
82 DccReceiver dccReceiver = download.dccReceiver();
83 if (dccReceiver == null) {
84 /* download has not even started. */
85 outputWriter.write(String.format("[%d] %s requested from %s (not started yet)\n", counter++, download.pack().name(), download.bot().name()));
88 outputWriter.write(String.format("[%d] %s %s from %s (%s, ", counter++, getProgressBar(dccReceiver, PROGRESS_BAR_WIDTH), dccReceiver.filename(), download.bot().name(), f(dccReceiver.size())));
89 if (dccReceiver.isRunning()) {
90 outputWriter.write(String.format("%.1f%%, %s/s, %s", dccReceiver.progress() * 100.0 / dccReceiver.size(), f(dccReceiver.currentRate()), t(SECONDS_LEFT.apply(download))));
92 if (dccReceiver.progress() >= dccReceiver.size()) {
93 outputWriter.write(String.format("complete, %s/s", f(dccReceiver.overallRate())));
95 outputWriter.write(String.format("aborted at %.1f%%, %s/s", dccReceiver.progress() * 100.0 / dccReceiver.size(), f(dccReceiver.currentRate())));
98 outputWriter.write(")\n");
100 outputWriter.write("End of DCCs.\n");
101 outputWriter.flush();
102 return state.setLastDownloads(downloads);
106 * Creates a progress bar for the given DCC receiver.
109 * The DCC receiver to create the progress bar for
110 * @param progressBarWidth
111 * The width of the progress bar (in characters)
112 * @return The progress bar for the given DCC receiver
114 private static String getProgressBar(DccReceiver dccReceiver, int progressBarWidth) {
115 FluentIterable<Character> partialProgressCharacters = from(asList(' ', '\u258f', '\u258e', '\u258d', '\u258c', '\u258b', '\u258a', '\u2589', '\u2588'));
116 double progress = dccReceiver.progress() * 100.0 / dccReceiver.size();
117 double singleBlockWidth = 100.0 / progressBarWidth;
118 int fullProgressBlocks = (int) (progress / singleBlockWidth);
119 double lastBlockProgress = (progress - fullProgressBlocks * singleBlockWidth) / singleBlockWidth;
120 StringBuilder progressBar = new StringBuilder(progressBarWidth);
121 for (int i = 0; i < progressBarWidth; ++i) {
122 if (i < fullProgressBlocks) {
123 progressBar.append(partialProgressCharacters.last().get());
124 } else if (i > fullProgressBlocks) {
125 progressBar.append(partialProgressCharacters.first().get());
127 progressBar.append(partialProgressCharacters.get((int) (lastBlockProgress * (partialProgressCharacters.size() - 1))));
130 return progressBar.toString();