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.Ordering;
41 * Command that will list all current downloads.
43 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
45 public class ListDownloadsCommand implements Command {
47 /** The core to operate on. */
48 private final Core core;
51 * Creates a new list downloads command.
54 * The core to operate on
56 public ListDownloadsCommand(Core core) {
65 public String getName() {
70 public Collection<String> getAliases() {
75 public State execute(State state, List<String> parameters, Writer outputWriter) throws IOException {
77 List<Download> downloads = newArrayList(from(core.downloads()).toSortedList(Ordering.from(BY_NAME).compound(BY_RUNNING)));
78 for (Download download : downloads) {
79 DccReceiver dccReceiver = download.dccReceiver();
80 if (dccReceiver == null) {
81 /* download has not even started. */
82 outputWriter.write(String.format("[%d] %s requested from %s (not started yet)\n", counter++, download.pack().name(), download.bot().name()));
85 outputWriter.write(String.format("[%d] %s from %s (%s, ", counter++, dccReceiver.filename(), download.bot().name(), f(dccReceiver.size())));
86 if (dccReceiver.isRunning()) {
87 outputWriter.write(String.format("%.1f%%, %s/s, %s", dccReceiver.progress() * 100.0 / dccReceiver.size(), f(dccReceiver.currentRate()), t(SECONDS_LEFT.apply(download))));
89 if (dccReceiver.progress() >= dccReceiver.size()) {
90 outputWriter.write(String.format("complete, %s/s", f(dccReceiver.overallRate())));
92 outputWriter.write(String.format("aborted at %.1f%%, %s/s", dccReceiver.progress() * 100.0 / dccReceiver.size(), f(dccReceiver.currentRate())));
95 outputWriter.write(")\n");
97 outputWriter.write("End of DCCs.\n");
99 return state.setLastDownloads(downloads);