+ /**
+ * Writes a generic message to the console.
+ *
+ * @param genericMessage
+ * The generic message event
+ */
+ @Subscribe
+ public void genericMessage(GenericMessage genericMessage) {
+ try {
+ writeLine(genericMessage.message());
+ } catch (IOException ioe1) {
+ /* ignore. */
+ }
+ }
+
+ //
+ // PRIVATE METHODS
+ //
+
+ private Collection<Command> findEligibleCommands(String name) {
+ ImmutableSet.Builder<Command> eligibleCommands = ImmutableSet.builder();
+ for (Command command : commands) {
+ if (command.getName().toLowerCase().startsWith(name.toLowerCase())) {
+ eligibleCommands.add(command);
+ }
+ for (String alias : command.getAliases()) {
+ if (alias.toLowerCase().startsWith(name.toLowerCase())) {
+ eligibleCommands.add(command);
+ }
+ }
+ }
+ return eligibleCommands.build();
+ }
+
+ /**
+ * Writes the given line followed by an LF to the {@link #writer}.
+ *
+ * @param line
+ * The line to write
+ * @throws IOException
+ * if an I/O error occurs
+ */
+ private void writeLine(String line) throws IOException {
+ writer.write(line + "\n");
+ writer.flush();
+ }
+
+ /**
+ * Converts large numbers into a human-friendly format, by showing SI prefixes
+ * for ×1024 (K), ×1048576 (M), and ×1073741824 (G).
+ *
+ * @param number
+ * The number to convert
+ * @return The converted number
+ */
+ static String f(long number) {
+ if (number >= (1 << 30)) {
+ return String.format("%.1fG", number / (double) (1 << 30));
+ }
+ if (number >= (1 << 20)) {
+ return String.format("%.1fM", number / (double) (1 << 20));
+ }
+ if (number >= (1 << 10)) {
+ return String.format("%.1fK", number / (double) (1 << 10));
+ }
+ 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);