Flush output after every command.
[xudocci.git] / src / main / java / net / pterodactylus / xdcc / ui / stdin / ListDownloadsCommand.java
1 /*
2  * XdccDownloader - ListDownloadsCommand.java - Copyright © 2013 David Roden
3  *
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.
8  *
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.
13  *
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/>.
16  */
17
18 package net.pterodactylus.xdcc.ui.stdin;
19
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;
28
29 import java.io.IOException;
30 import java.io.Writer;
31 import java.util.Collection;
32 import java.util.List;
33
34 import net.pterodactylus.irc.DccReceiver;
35 import net.pterodactylus.xdcc.core.Core;
36 import net.pterodactylus.xdcc.data.Download;
37
38 import com.google.common.collect.FluentIterable;
39 import com.google.common.collect.Ordering;
40
41 /**
42  * Command that will list all current downloads.
43  *
44  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
45  */
46 public class ListDownloadsCommand implements Command {
47
48         private static final int PROGRESS_BAR_WIDTH = 10;
49
50         /** The core to operate on. */
51         private final Core core;
52
53         /**
54          * Creates a new list downloads command.
55          *
56          * @param core
57          *              The core to operate on
58          */
59         public ListDownloadsCommand(Core core) {
60                 this.core = core;
61         }
62
63         //
64         // COMMAND METHODS
65         //
66
67         @Override
68         public String getName() {
69                 return "list";
70         }
71
72         @Override
73         public Collection<String> getAliases() {
74                 return asList("dcc");
75         }
76
77         @Override
78         public State execute(State state, List<String> parameters, Writer outputWriter) throws IOException {
79                 int counter = 0;
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()));
86                                 continue;
87                         }
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))));
91                         } else {
92                                 if (dccReceiver.progress() >= dccReceiver.size()) {
93                                         outputWriter.write(String.format("complete, %s/s", f(dccReceiver.overallRate())));
94                                 } else {
95                                         outputWriter.write(String.format("aborted at %.1f%%, %s/s", dccReceiver.progress() * 100.0 / dccReceiver.size(), f(dccReceiver.currentRate())));
96                                 }
97                         }
98                         outputWriter.write(")\n");
99                 }
100                 outputWriter.write("End of DCCs.\n");
101                 return state.setLastDownloads(downloads);
102         }
103
104         /**
105          * Creates a progress bar for the given DCC receiver.
106          *
107          * @param dccReceiver
108          *              The DCC receiver to create the progress bar for
109          * @param progressBarWidth
110          *              The width of the progress bar (in characters)
111          * @return The progress bar for the given DCC receiver
112          */
113         private static String getProgressBar(DccReceiver dccReceiver, int progressBarWidth) {
114                 FluentIterable<Character> partialProgressCharacters = from(asList(' ', '\u258f', '\u258e', '\u258d', '\u258c', '\u258b', '\u258a', '\u2589', '\u2588'));
115                 double progress = dccReceiver.progress() * 100.0 / dccReceiver.size();
116                 double singleBlockWidth = 100.0 / progressBarWidth;
117                 int fullProgressBlocks = (int) (progress / singleBlockWidth);
118                 double lastBlockProgress = (progress - fullProgressBlocks * singleBlockWidth) / singleBlockWidth;
119                 StringBuilder progressBar = new StringBuilder(progressBarWidth);
120                 for (int i = 0; i < progressBarWidth; ++i) {
121                         if (i < fullProgressBlocks) {
122                                 progressBar.append(partialProgressCharacters.last().get());
123                         } else if (i > fullProgressBlocks) {
124                                 progressBar.append(partialProgressCharacters.first().get());
125                         } else {
126                                 progressBar.append(partialProgressCharacters.get((int) (lastBlockProgress * (partialProgressCharacters.size() - 1))));
127                         }
128                 }
129                 return progressBar.toString();
130         }
131
132 }