Move time formatting to command reader; use function to calculate the remaining trans...
[xudocci.git] / src / main / java / net / pterodactylus / xdcc / ui / stdin / CommandReader.java
index 46b22ba..01d0051 100644 (file)
@@ -38,6 +38,7 @@ import net.pterodactylus.xdcc.core.event.MessageReceived;
 import net.pterodactylus.xdcc.data.Download;
 
 import com.google.common.base.Joiner;
+import com.google.common.base.Optional;
 import com.google.common.collect.ImmutableList;
 import com.google.common.collect.ImmutableSet;
 import com.google.common.eventbus.Subscribe;
@@ -251,4 +252,21 @@ public class CommandReader extends AbstractExecutionThreadService {
                return String.format("%dB", number);
        }
 
+       /**
+        * Formats the given number of seconds into a more easily readable string.
+        *
+        * @param seconds
+        *              The number of seconds
+        * @return The formatted time, or “unknown” if the time is unknown
+        */
+       static String t(Optional<Long> seconds) {
+               if (!seconds.isPresent()) {
+                       return "unknown";
+               }
+               if (seconds.get() > 3600) {
+                       return String.format("%02d:%02d:%02d", seconds.get() / 3600, (seconds.get() / 60) % 60, seconds.get() % 60);
+               }
+               return String.format("%02d:%02d", (seconds.get() / 60) % 60, seconds.get() % 60);
+       }
+
 }