Quellcode durchsuchen

Added Command.doTable

/nowplaying --sources now uses a table for its outpt

git-svn-id: http://svn.dmdirc.com/trunk@2761 00569f92-eb28-0410-84fd-f71c24880f
tags/0.5.5
Chris Smith vor 16 Jahren
Ursprung
Commit
f2b300aad5

+ 25
- 9
src/com/dmdirc/addons/nowplaying/NowPlayingCommand.java Datei anzeigen

@@ -24,7 +24,6 @@ package com.dmdirc.addons.nowplaying;
24 24
 
25 25
 import com.dmdirc.MessageTarget;
26 26
 import com.dmdirc.Server;
27
-import com.dmdirc.addons.nowplaying.MediaSource;
28 27
 import com.dmdirc.commandparser.commands.ChatCommand;
29 28
 import com.dmdirc.commandparser.CommandManager;
30 29
 import com.dmdirc.commandparser.commands.IntelligentCommand;
@@ -57,6 +56,7 @@ public final class NowPlayingCommand extends ChatCommand implements IntelligentC
57 56
     }
58 57
     
59 58
     /** {@inheritDoc} */
59
+    @Override
60 60
     public void execute(final InputWindow origin, final Server server,
61 61
             final MessageTarget target, final boolean isSilent, final String ... args) {
62 62
         if (args.length > 0 && args[0].equalsIgnoreCase("--sources")) {
@@ -76,7 +76,8 @@ public final class NowPlayingCommand extends ChatCommand implements IntelligentC
76 76
                     }
77 77
                 }
78 78
             } else {
79
-                sendLine(origin, isSilent, FORMAT_ERROR, "You must specify a source when using --source.");
79
+                sendLine(origin, isSilent, FORMAT_ERROR,
80
+                        "You must specify a source when using --source.");
80 81
             }
81 82
         } else {
82 83
             if (parent.hasRunningSource()) {
@@ -99,23 +100,33 @@ public final class NowPlayingCommand extends ChatCommand implements IntelligentC
99 100
         if (sources.isEmpty()) {
100 101
             sendLine(origin, isSilent, FORMAT_ERROR, "No media sources available.");
101 102
         } else {
102
-            String status;
103
+            final String[] headers = {"Source", "Status", "Information"};
104
+            final String[][] data = new String[sources.size()][3];
105
+            int i = 0;
103 106
             
104 107
             for (MediaSource source : sources) {
108
+                data[i][0] = source.getAppName();
109
+                
105 110
                 if (source.isRunning()) {
106 111
                     if (source.isPlaying()) {
107
-                        status = "playing: " + getInformation(source);
112
+                        data[i][1] = "playing";
108 113
                     } else {
109
-                        status = "paused: " + getInformation(source);
114
+                        data[i][1] = "paused";
110 115
                     }
116
+                    
117
+                    data[i][2] = getInformation(source);
111 118
                 } else {
112
-                    status = "not running";
119
+                    data[i][1] = "not running";
120
+                    data[i][2] = "-";
113 121
                 }
114
-                sendLine(origin, isSilent, FORMAT_OUTPUT, source.getAppName() + ": " + status);
122
+                
123
+                i++;
115 124
             }
125
+            
126
+            sendLine(origin, isSilent, FORMAT_OUTPUT, doTable(headers, data));
116 127
         }
117 128
     }
118
-    
129
+       
119 130
     /**
120 131
      * Returns a formatted information string from the requested soruce.
121 132
      *
@@ -139,21 +150,26 @@ public final class NowPlayingCommand extends ChatCommand implements IntelligentC
139 150
     }
140 151
     
141 152
     /** {@inheritDoc}. */
153
+    @Override
142 154
     public String getName() {
143 155
         return "nowplaying";
144 156
     }
145 157
     
146 158
     /** {@inheritDoc}. */
159
+    @Override
147 160
     public boolean showInHelp() {
148 161
         return true;
149 162
     }
150 163
     
151 164
     /** {@inheritDoc}. */
165
+    @Override
152 166
     public String getHelp() {
153
-        return "nowplaying [--sources|--source <source>] - tells the channel the song you're currently playing";
167
+        return "nowplaying [--sources|--source <source>] - " +
168
+                "tells the channel the song you're currently playing";
154 169
     }
155 170
     
156 171
     /** {@inheritDoc} */
172
+    @Override
157 173
     public AdditionalTabTargets getSuggestions(final int arg, 
158 174
             final List<String> previousArgs) {
159 175
         final AdditionalTabTargets res = new AdditionalTabTargets();

+ 4
- 0
src/com/dmdirc/commandparser/CommandInfo.java Datei anzeigen

@@ -46,6 +46,10 @@ public abstract class CommandInfo {
46 46
      */
47 47
     public abstract String getHelp();
48 48
     
49
+    /**
50
+     * Retrieves the type of this command.
51
+     * @return This command's type
52
+     */
49 53
     public abstract CommandType getType();
50 54
 
51 55
 }

+ 56
- 0
src/com/dmdirc/commandparser/commands/Command.java Datei anzeigen

@@ -26,6 +26,7 @@ import com.dmdirc.commandparser.CommandInfo;
26 26
 import com.dmdirc.commandparser.CommandManager;
27 27
 import com.dmdirc.commandparser.CommandType;
28 28
 import com.dmdirc.ui.interfaces.InputWindow;
29
+import com.dmdirc.ui.messages.Styliser;
29 30
 
30 31
 /**
31 32
  * Represents a generic command.
@@ -94,6 +95,61 @@ public abstract class Command extends CommandInfo implements Comparable<Command>
94 95
                 name, args);
95 96
     }    
96 97
     
98
+    /**
99
+     * Formats the specified data into a table suitable for output in the
100
+     * textpane. It is expected that each String[] in data has the same number
101
+     * of elements as the headers array.
102
+     * 
103
+     * @param headers The headers of the table.
104
+     * @param data The contents of the table.
105
+     * @return A string containing an ASCII table
106
+     */
107
+    protected static String doTable(final String[] headers, final String[][] data) {
108
+        StringBuilder res = new StringBuilder();
109
+        res.append(Styliser.CODE_FIXED);
110
+        res.append(Styliser.CODE_BOLD);
111
+        
112
+        int[] maxsizes = new int[headers.length];
113
+        
114
+        for (int i = 0; i < headers.length; i++) {
115
+            maxsizes[i] = headers[i].length() + 3;
116
+            
117
+            for (int j = 0; j < data.length; j++) {
118
+                maxsizes[i] = Math.max(maxsizes[i], data[j][i].length() + 3);
119
+            }
120
+            
121
+            doPadding(res, headers[i], maxsizes[i]);
122
+        }
123
+                
124
+        for (String[] source : data) {
125
+            res.append('\n');
126
+            res.append(Styliser.CODE_FIXED);
127
+            
128
+            for (int i = 0; i < source.length; i++) {
129
+                doPadding(res, source[i], maxsizes[i]);
130
+            }
131
+        }
132
+        
133
+        return res.toString();
134
+    }
135
+    
136
+    /**
137
+     * Adds the specified data to the stringbuilder, padding with spaces to
138
+     * the specified size.
139
+     * 
140
+     * @param builder The stringbuilder to append data to
141
+     * @param data The data to be added
142
+     * @param size The minimum size that should be used
143
+     */
144
+    private static void doPadding(final StringBuilder builder, final String data,
145
+            final int size) {
146
+        builder.append(data);
147
+        
148
+        for (int i = 0; i < size - data.length(); i++) {
149
+            builder.append(' ');
150
+        }
151
+    }    
152
+    
97 153
     /** {@inheritDoc} */
98 154
     @Override
99 155
     public final int compareTo(final Command o) {

Laden…
Abbrechen
Speichern