Flush output after every command.
[xudocci.git] / src / main / java / net / pterodactylus / xdcc / ui / stdin / CommandReader.java
index 87678b5..2d0a352 100644 (file)
@@ -28,6 +28,7 @@ import java.io.BufferedReader;
 import java.io.IOException;
 import java.io.Reader;
 import java.io.Writer;
+import java.util.ArrayList;
 import java.util.Collection;
 import java.util.List;
 
@@ -39,6 +40,7 @@ import net.pterodactylus.xdcc.core.event.DownloadStarted;
 import net.pterodactylus.xdcc.core.event.GenericMessage;
 import net.pterodactylus.xdcc.core.event.MessageReceived;
 import net.pterodactylus.xdcc.data.Download;
+import net.pterodactylus.xdcc.util.io.DuplicateLineSuppressingWriter;
 
 import com.google.common.base.Joiner;
 import com.google.common.base.Optional;
@@ -61,7 +63,7 @@ public class CommandReader extends AbstractExecutionThreadService {
        private final BufferedReader reader;
 
        /** The writer to write the results to. */
-       private final Writer writer;
+       private final DuplicateLineSuppressingWriter writer;
        private final Collection<Download> failedDownloads;
 
        /**
@@ -108,6 +110,7 @@ public class CommandReader extends AbstractExecutionThreadService {
                        if (line.equals("")) {
                                line = lastLine;
                        }
+                       writer.reset();
                        String[] words = line.split(" +");
                        String commandName = words[0];
                        Collection<Command> eligibleCommands = findEligibleCommands(commandName);
@@ -119,6 +122,7 @@ public class CommandReader extends AbstractExecutionThreadService {
                                Command command = eligibleCommands.iterator().next();
                                List<String> parameters = from(asList(words)).skip(1).toList();
                                state = command.execute(state, parameters, writer);
+                               writer.flush();
                        }
 
                        lastLine = line;
@@ -154,6 +158,7 @@ public class CommandReader extends AbstractExecutionThreadService {
        @Subscribe
        public void downloadFinished(DownloadFinished downloadFinished) {
                Download download = downloadFinished.download();
+               removeFailedDownloads(download.pack().name());
                try {
                        writeLine(green(String.format("Download of %s (from %s, %s) has finished, at %s/s.", download.pack().name(), download.bot().name(), download.bot().network().name(), f(download.dccReceiver().overallRate()))));
                } catch (IOException ioe1) {
@@ -161,6 +166,16 @@ public class CommandReader extends AbstractExecutionThreadService {
                }
        }
 
+       private void removeFailedDownloads(String name) {
+               List<Download> failedDownloadsToRemove = new ArrayList<>();
+               for (Download failedDownload : failedDownloads) {
+                       if (failedDownload.pack().name().equals(name)) {
+                               failedDownloadsToRemove.add(failedDownload);
+                       }
+               }
+               failedDownloads.removeAll(failedDownloadsToRemove);
+       }
+
        /**
         * Called when a download fails.
         *